sched_rr.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <map>
  2. #include <queue>
  3. #include "sched_rr.h"
  4. #include "basesched.h"
  5. #include <iostream>
  6. using namespace std;
  7. SchedRR::SchedRR(vector<int> argn) {
  8. // Round robin recibe la cantidad de cores y sus cpu_quantum por parámetro
  9. nucleos = argn[1];
  10. quantum = argn[2];
  11. cur_pid = IDLE_TASK;
  12. }
  13. SchedRR::~SchedRR() {
  14. p_map.clear();
  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() {
  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. switch(m) {
  44. case TICK:
  45. p_map[cur_pid].quantum_count++;
  46. break;
  47. case BLOCK:
  48. switch_process=1;
  49. p_map[cur_pid].state=BLOCKED;
  50. break;
  51. case EXIT:
  52. switch_process=1;
  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>=quantum) {
  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. cur_pid=next_pid();
  66. p_map[cur_pid].state=RUNNING;
  67. }
  68. return cur_pid;
  69. }