sched_mfq.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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){
  41. if (v_cola[qn].size() == 0)
  42. return -1;
  43. for (it_type it = v_cola[qn].begin(); it != v_cola[qn].end(); it++) {
  44. //cout << "Mirando al pid " << it->first << endl;
  45. if (it->second.state == READY){
  46. //cout << "Me gusta " << it->first << endl;
  47. return it->first;
  48. }
  49. }
  50. return -1;
  51. }
  52. uint SchedMFQ::next_pid(){
  53. for( uint q = 0; q < n_colas; q++ ){
  54. //cout << "Mirando en la pila " << q << endl;
  55. int p = ready_at(q);
  56. if (p != -1)
  57. return p; //Algun proceso listo
  58. }
  59. return IDLE_TASK;
  60. }
  61. void SchedMFQ::mostrar_estados(){
  62. //cout << "RDY: " << READY << ", RNING: " << RUNNING << ", BLK: " << BLOCKED << endl;
  63. for( uint q = 0; q < n_colas; q++ ){
  64. //cout << "Cola " << q << ", size: " << v_cola[q].size() << endl;
  65. for (it_type it = v_cola[q].begin(); it != v_cola[q].end(); it++) {
  66. cout << "["<< q <<"]PID: " << it->first << ", STATE: " << it->second.state << endl;
  67. }
  68. }
  69. }
  70. int SchedMFQ::tick(int core, const enum Motivo m) {
  71. uint switch_process = 0;
  72. int cur_pid = current_pid(core);
  73. uint p_q;
  74. //cout << "(start tick) PID at core " << core << " = " << cur_pid << endl;
  75. if (cur_pid == IDLE_TASK){
  76. cur_pid = next_pid();
  77. if (cur_pid != IDLE_TASK){
  78. p_q = pid_queue(cur_pid);
  79. v_cola[p_q][cur_pid].state = RUNNING;
  80. }
  81. return cur_pid;
  82. }
  83. p_q = pid_queue(cur_pid);
  84. if (p_q == 65535) //FIXME
  85. return IDLE_TASK;
  86. process* cur_process=&(v_cola[p_q][cur_pid]);
  87. switch (m) {
  88. case TICK:
  89. cur_process->quantum_count++;
  90. if (cur_process->quantum_count >= q_cola[p_q]) {
  91. switch_process = 1;
  92. cur_process->state = READY;
  93. cur_process->quantum_count = 0;
  94. //cout << "Marcando a cur_pid("<< cur_pid<<") como READY (>quantum)" << endl;
  95. if(p_q < n_colas - 1) { //Hay una cola "peor" para ir; lo llevo
  96. v_cola[p_q+1][cur_pid]=*cur_process;
  97. v_cola[p_q].erase(cur_pid);
  98. }
  99. }
  100. break;
  101. case BLOCK:
  102. switch_process = 1;
  103. cur_process->state = BLOCKED;
  104. break;
  105. case EXIT:
  106. switch_process = 1;
  107. //cout << v_cola[p_q].size() << endl;
  108. //cout << "borro pid: " << cur_pid << endl;
  109. v_cola[p_q].erase(cur_pid);
  110. //cout << v_cola[p_q].size() << endl;
  111. break;
  112. }
  113. //mostrar_estados();
  114. if (switch_process) {
  115. cur_pid = next_pid();
  116. if (cur_pid != IDLE_TASK){
  117. p_q = pid_queue(cur_pid);
  118. v_cola[p_q][cur_pid].state = RUNNING;
  119. }
  120. }
  121. return cur_pid;
  122. }