sched_rr.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "sched_rr.h"
  2. #include "basesched.h"
  3. using namespace std;
  4. SchedRR::SchedRR(vector<int> argn) {
  5. // Round robin recibe la cantidad de cores y sus cpu_quantum por parámetro
  6. nucleos = argn[1];
  7. quantums = new uint[nucleos];
  8. for (uint i = 0; i < nucleos; i++) {
  9. quantums[i] = argn[i + 2];
  10. }
  11. }
  12. SchedRR::~SchedRR() {
  13. p_map.clear();
  14. delete[] quantums;
  15. }
  16. void SchedRR::load(int pid) {
  17. p_map[pid].state = READY;
  18. }
  19. void SchedRR::unblock(int pid) {
  20. p_map[pid].state = READY;
  21. }
  22. int SchedRR::next_pid(uint cur_pid) {
  23. // Hasta el fin de la 'lista', hay alguno listo?
  24. for (it_type it = ++p_map.find(cur_pid); it != p_map.end(); it++) {
  25. if (it->first == IDLE_TASK)
  26. continue;
  27. if (it->second.state == READY)
  28. return it->first;
  29. }
  30. // Desde el inicio hasta donde estaba, hay alguno listo?
  31. for (it_type it = p_map.begin(); it != p_map.find(cur_pid); it++) {
  32. if (it->first == IDLE_TASK)
  33. continue;
  34. if (it->second.state == READY)
  35. return it->first;
  36. }
  37. if (p_map[cur_pid].state == READY)
  38. return cur_pid;
  39. return IDLE_TASK;
  40. }
  41. int SchedRR::tick(int cpu, const enum Motivo m) {
  42. uint switch_process = 0;
  43. int cur_pid = current_pid(cpu);
  44. switch (m) {
  45. case TICK:
  46. p_map[cur_pid].quantum_count++;
  47. break;
  48. case BLOCK:
  49. switch_process = 1;
  50. p_map[cur_pid].state = BLOCKED;
  51. break;
  52. case EXIT:
  53. p_map.erase(cur_pid);
  54. cur_pid = IDLE_TASK;
  55. break;
  56. }
  57. if (cur_pid == IDLE_TASK)
  58. switch_process = 1;
  59. if (p_map[cur_pid].quantum_count >= quantums[cpu]) {
  60. switch_process = 1;
  61. p_map[cur_pid].state = READY;
  62. p_map[cur_pid].quantum_count = 0;
  63. }
  64. if (switch_process) {
  65. // Implementación simple, se puede usar current_remaining(cpu) para ver
  66. // si conviene cambiar de nucleo, y/o ver cuanto es el precio de migrar
  67. cur_pid = next_pid(cur_pid);
  68. p_map[cur_pid].state = RUNNING;
  69. }
  70. return cur_pid;
  71. }