sched_mfq.cpp 2.7 KB

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