sched_rr.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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[0];
  10. quantum = argn[1];
  11. cur_pid = IDLE_TASK;
  12. }
  13. SchedRR::~SchedRR() {
  14. p_map.clear();
  15. }
  16. void SchedRR::load(int pid) {
  17. cout << "Load: " << pid << endl;
  18. p_map[pid]=READY;
  19. }
  20. void SchedRR::unblock(int pid) {
  21. p_map[pid]=READY;
  22. }
  23. int SchedRR::next_pid(){
  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. cout << it->first << " => " << it->second << endl;
  29. if (it->second == READY){
  30. return it->first;
  31. }
  32. }
  33. //Desde el inicio hasta donde estaba, hay alguno listo?
  34. for( it_type it = p_map.begin(); it != p_map.find(cur_pid); it++) {
  35. if (it->first==IDLE_TASK)
  36. continue;
  37. cout << it->first << " ==> " << it->second << endl;
  38. if (it->second == READY)
  39. return it->first;
  40. }
  41. if (p_map[cur_pid]==READY)
  42. return cur_pid;
  43. return IDLE_TASK;
  44. }
  45. int SchedRR::tick(int cpu, const enum Motivo m) {
  46. switch(m) {
  47. case TICK:
  48. p_map[cur_pid]=READY;
  49. break;
  50. case BLOCK:
  51. p_map[cur_pid]=BLOCKED;
  52. break;
  53. case EXIT:
  54. cout << "exit" << endl;
  55. p_map.erase(cur_pid);
  56. for( it_type it = p_map.begin(); it != p_map.end(); it++) {
  57. cout << "m[" << it->first << "] = " << it->second << endl;
  58. }
  59. cur_pid=IDLE_TASK;
  60. break;
  61. }
  62. cur_pid=next_pid();
  63. cout << cur_pid << endl;
  64. p_map[cur_pid]=RUNNING;
  65. return 0;
  66. }