sched_mfq.cpp 2.0 KB

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