sched_mfq.cpp 3.8 KB

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