sched_rr.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. cur_pid = IDLE_TASK;
  10. nucleos = argn[1];
  11. quantums = new uint[nucleos];
  12. for (int i = 0; i < nucleos; i++) {
  13. quantums[i] = argn[i+2];
  14. }
  15. }
  16. SchedRR::~SchedRR() {
  17. p_map.clear();
  18. delete [] quantums;
  19. }
  20. void SchedRR::load(int pid) {
  21. p_map[pid].state=READY;
  22. }
  23. void SchedRR::unblock(int pid) {
  24. p_map[pid].state=READY;
  25. }
  26. int SchedRR::next_pid() {
  27. // Hasta el fin de la 'lista', hay alguno listo?
  28. for(it_type it = ++p_map.find(cur_pid); it != p_map.end(); it++) {
  29. if (it->first == IDLE_TASK)
  30. continue;
  31. if (it->second.state == READY)
  32. return it->first;
  33. }
  34. // Desde el inicio hasta donde estaba, hay alguno listo?
  35. for(it_type it = p_map.begin(); it != p_map.find(cur_pid); it++) {
  36. if (it->first == IDLE_TASK)
  37. continue;
  38. if (it->second.state == READY)
  39. return it->first;
  40. }
  41. if (p_map[cur_pid].state == READY)
  42. return cur_pid;
  43. return IDLE_TASK;
  44. }
  45. int SchedRR::tick(int cpu, const enum Motivo m) {
  46. uint switch_process=0;
  47. for (int i == 0; i<nucleos; i++) {
  48. uint current = current_pid(
  49. }
  50. switch(m) {
  51. case TICK:
  52. p_map[cur_pid].quantum_count++;
  53. break;
  54. case BLOCK:
  55. switch_process=1;
  56. p_map[cur_pid].state=BLOCKED;
  57. break;
  58. case EXIT:
  59. switch_process=1;
  60. p_map.erase(cur_pid);
  61. cur_pid=IDLE_TASK;
  62. break;
  63. }
  64. if (cur_pid==IDLE_TASK)
  65. switch_process=1;
  66. if (p_map[cur_pid].quantum_count>=quantums[cpu]) {
  67. switch_process=1;
  68. p_map[cur_pid].state=READY;
  69. p_map[cur_pid].quantum_count=0;
  70. }
  71. if (switch_process) {
  72. cur_pid=next_pid();
  73. p_map[cur_pid].state=RUNNING;
  74. }
  75. return cur_pid;
  76. }