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. // FIXME Usar colas?
  24. //Hasta el fin de la 'lista', hay alguno listo?
  25. for( it_type it = p_map.find(cur_pid); it != p_map.end(); it++) {
  26. if (it->first==cur_pid || it->first==IDLE_TASK) //Como arranco del sig?
  27. continue;
  28. if (it->second.state == READY){
  29. return it->first;
  30. }
  31. }
  32. //Desde el inicio hasta donde estaba, hay alguno listo?
  33. for( it_type it = p_map.begin(); it != p_map.find(cur_pid); it++) {
  34. if (it->first==IDLE_TASK)
  35. continue;
  36. if (it->second.state == READY)
  37. return it->first;
  38. }
  39. if (p_map[cur_pid].state==READY)
  40. return cur_pid;
  41. return IDLE_TASK;
  42. }
  43. int SchedRR::tick(int cpu, const enum Motivo m) {
  44. uint switch_process=0;
  45. switch(m) {
  46. case TICK:
  47. p_map[cur_pid].quantum_count++;
  48. break;
  49. case BLOCK:
  50. switch_process=1;
  51. p_map[cur_pid].state=BLOCKED;
  52. break;
  53. case EXIT:
  54. switch_process=1;
  55. p_map.erase(cur_pid);
  56. cur_pid=IDLE_TASK;
  57. break;
  58. }
  59. if (cur_pid==IDLE_TASK)
  60. switch_process=1;
  61. if (p_map[cur_pid].quantum_count>quantum) {
  62. switch_process=1;
  63. p_map[cur_pid].state=READY;
  64. p_map[cur_pid].quantum_count=0;
  65. }
  66. if (switch_process) {
  67. cur_pid=next_pid();
  68. p_map[cur_pid].state=RUNNING;
  69. }
  70. return cur_pid;
  71. }