sched_mfq.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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(uint 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. for( uint q = 0; q < n_colas; q++ ){
  62. cout << "Cola " << q << ", size: " << v_cola[q].size() << endl;
  63. }
  64. }
  65. int SchedMFQ::tick(int core, const enum Motivo m) {
  66. uint switch_process = 0;
  67. int cur_pid = current_pid(core);
  68. cout << "PID at core " << core << " = " << cur_pid << endl;
  69. if (cur_pid == IDLE_TASK)
  70. return next_pid();
  71. uint p_q = pid_queue(cur_pid);
  72. process* cur_process=&(v_cola[p_q][cur_pid]);
  73. switch (m) {
  74. case TICK:
  75. cur_process->quantum_count++;
  76. if (cur_process->quantum_count >= q_cola[p_q]) {
  77. switch_process = 1;
  78. cur_process->state = READY;
  79. cur_process->quantum_count = 0;
  80. if(p_q < n_colas - 1) { //Hay una cola "peor" para ir; lo llevo
  81. v_cola[p_q+1][cur_pid]=*cur_process;
  82. v_cola[p_q].erase(cur_pid);
  83. }
  84. }
  85. break;
  86. case BLOCK:
  87. switch_process = 1;
  88. cur_process->state = BLOCKED;
  89. break;
  90. case EXIT:
  91. switch_process = 1;
  92. //cout << v_cola[p_q].size() << endl;
  93. //cout << "borro pid: " << cur_pid << endl;
  94. v_cola[p_q].erase(cur_pid);
  95. //cout << v_cola[p_q].size() << endl;
  96. break;
  97. }
  98. mostrar_estados();
  99. if (switch_process) {
  100. cur_pid = next_pid();
  101. process* next_process=&(v_cola[p_q][cur_pid]);
  102. next_process->state = RUNNING;
  103. return cur_pid;
  104. }
  105. return cur_pid;
  106. }