sched_mfq.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. return 65535; // ??
  37. }
  38. int SchedMFQ::ready_at(uint qn){
  39. if (v_cola[qn].size() == 0)
  40. return -1;
  41. for (it_type it = v_cola[qn].begin(); it != v_cola[qn].end(); it++) {
  42. //cout << "Mirando al pid " << it->first << endl;
  43. if (it->second.state == READY){
  44. //cout << "Me gusta " << it->first << endl;
  45. return it->first;
  46. }
  47. }
  48. return -1;
  49. }
  50. uint SchedMFQ::next_pid(){
  51. for( uint q = 0; q < n_colas; q++ ){
  52. //cout << "Mirando en la pila " << q << endl;
  53. int p = ready_at(q);
  54. if (p != -1)
  55. return p; //Algun proceso listo
  56. }
  57. return IDLE_TASK;
  58. }
  59. int SchedMFQ::tick(int core, const enum Motivo m) {
  60. uint switch_process = 0;
  61. int cur_pid = current_pid(core);
  62. if (cur_pid == IDLE_TASK)
  63. return next_pid();
  64. uint p_q = pid_queue(cur_pid);
  65. process* cur_process=&(v_cola[p_q][cur_pid]);
  66. switch (m) {
  67. case TICK:
  68. cur_process->quantum_count++;
  69. if (cur_process->quantum_count >= q_cola[p_q]) {
  70. switch_process = 1;
  71. cur_process->state = READY;
  72. cur_process->quantum_count = 0;
  73. if(p_q < n_colas) { //Hay una cola "peor" para ir; lo llevo
  74. v_cola[p_q+1][cur_pid]=*cur_process;
  75. v_cola[p_q].erase(cur_pid);
  76. }
  77. }
  78. break;
  79. case BLOCK:
  80. switch_process = 1;
  81. cur_process->state = BLOCKED;
  82. break;
  83. case EXIT:
  84. switch_process = 1;
  85. v_cola[p_q].erase(cur_pid);
  86. break;
  87. }
  88. if (switch_process)
  89. return next_pid();
  90. return cur_pid;
  91. }