sched_mfq.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <map>
  2. #include <iostream>
  3. #include "sched_mfq.h"
  4. #include "basesched.h"
  5. using namespace std;
  6. SchedMFQ::SchedMFQ(vector<int> argn) {
  7. // MFQ recibe los quantums por parámetro
  8. n_colas = argn.size()-1;
  9. q_cola = new uint[n_colas];
  10. for( uint i = 0; i < n_colas ;i++ ){
  11. q_cola[i] = argn[1+i];
  12. v_cola.push_back(std::map<int,process>());
  13. }
  14. }
  15. SchedMFQ::~SchedMFQ() {
  16. delete[] q_cola;
  17. }
  18. void SchedMFQ::load(int pid) {
  19. v_cola[0][pid].state=READY; //Cargo PID en la cola de mayor prioridad
  20. }
  21. void SchedMFQ::unblock(int pid) {
  22. uint p_q=pid_queue(pid);
  23. v_cola[p_q][pid].state = READY;
  24. v_cola[p_q][pid].quantum_count = 0;
  25. if(p_q > 0) { //Hay una cola "mejor" para ir; lo llevo
  26. v_cola[p_q-1][pid]=v_cola[p_q][pid];
  27. v_cola[p_q].erase(pid);
  28. }
  29. }
  30. uint SchedMFQ::pid_queue(int pid){
  31. for( uint i = 0; i < n_colas; i++ )
  32. if ( v_cola[i].count(pid) == 1) //Solo hay 0/1 key en un map
  33. return i;
  34. cout << "WHAT THE FUCK " << pid << endl;
  35. for( uint i = 0; i < n_colas; i++ )
  36. cout << "size cola " << i << " = " << v_cola[i].size() << endl;
  37. return 65535; // ??
  38. }
  39. int SchedMFQ::ready_at(uint qn){
  40. if (v_cola[qn].size() == 0)
  41. return -1;
  42. for (it_type it = v_cola[qn].begin(); it != v_cola[qn].end(); it++) {
  43. cout << "Mirando al pid " << it->first << endl;
  44. if (it->second.state == READY){
  45. cout << "Me gusta " << it->first << endl;
  46. return it->first;
  47. }
  48. }
  49. return -1;
  50. }
  51. uint SchedMFQ::next_pid(){
  52. for( uint q = 0; q < n_colas; q++ ){
  53. cout << "Mirando en la pila " << q << endl;
  54. int p = ready_at(q);
  55. if (p != -1)
  56. return p; //Algun proceso listo
  57. }
  58. return IDLE_TASK;
  59. }
  60. void SchedMFQ::mostrar_estados(){
  61. cout << "RDY: " << READY << ", RNING: " << RUNNING << ", BLK: " << BLOCKED << endl;
  62. for( uint q = 0; q < n_colas; q++ ){
  63. cout << "Cola " << q << ", size: " << v_cola[q].size() << endl;
  64. for (it_type it = v_cola[q].begin(); it != v_cola[q].end(); it++) {
  65. cout << "PID: " << it->first << ", STATE: " << it->second.state << endl;
  66. }
  67. }
  68. }
  69. int SchedMFQ::tick(int core, const enum Motivo m) {
  70. uint switch_process = 0;
  71. int cur_pid = current_pid(core);
  72. cout << "PID at core " << core << " = " << cur_pid << endl;
  73. if (cur_pid == IDLE_TASK)
  74. return next_pid();
  75. uint p_q = pid_queue(cur_pid);
  76. if (p_q == 65535) //FIXME
  77. return IDLE_TASK;
  78. process* cur_process=&(v_cola[p_q][cur_pid]);
  79. switch (m) {
  80. case TICK:
  81. cur_process->quantum_count++;
  82. if (cur_process->quantum_count >= q_cola[p_q]) {
  83. switch_process = 1;
  84. cur_process->state = READY;
  85. cur_process->quantum_count = 0;
  86. cout << "Marcando a cur_pid("<< cur_pid<<") como READY (>quantum)" << endl;
  87. if(p_q < n_colas - 1) { //Hay una cola "peor" para ir; lo llevo
  88. v_cola[p_q+1][cur_pid]=*cur_process;
  89. v_cola[p_q].erase(cur_pid);
  90. }
  91. }
  92. break;
  93. case BLOCK:
  94. switch_process = 1;
  95. cur_process->state = BLOCKED;
  96. break;
  97. case EXIT:
  98. switch_process = 1;
  99. //cout << v_cola[p_q].size() << endl;
  100. cout << "borro pid: " << cur_pid << endl;
  101. v_cola[p_q].erase(cur_pid);
  102. //cout << v_cola[p_q].size() << endl;
  103. break;
  104. }
  105. mostrar_estados();
  106. if (switch_process) {
  107. cur_pid = next_pid();
  108. if (cur_pid == IDLE_TASK){
  109. cout << "SWITCHING -1 ??? " << endl;
  110. return IDLE_TASK;
  111. }
  112. p_q = pid_queue(cur_pid);
  113. process* next_process=&(v_cola[p_q][cur_pid]);
  114. next_process->state = RUNNING;
  115. return cur_pid;
  116. }
  117. return cur_pid;
  118. }