sched_mfq.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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[1];
  11. q_cola = new uint[n_colas];
  12. for( uint i = 0; i < n_colas ;i++ ){
  13. q_cola[i] = argn[2+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. if (it->second.state == READY)
  43. return it->first;
  44. }
  45. return -1;
  46. }
  47. uint SchedMFQ::next_pid(){
  48. for( uint q = 0; q < n_colas; q++ ){
  49. int p = ready_at(q);
  50. if (p!=-1){
  51. return p; //Algun proceso listo
  52. }
  53. }
  54. return IDLE_TASK;
  55. //return 65535;
  56. }
  57. int SchedMFQ::tick(int core, const enum Motivo m) {
  58. uint switch_process = 0;
  59. uint cur_pid = current_pid(core);
  60. if (cur_pid == IDLE_TASK)
  61. return next_pid();
  62. uint p_q = pid_queue(cur_pid);
  63. //cout << "curpid " << cur_pid << endl; //-1
  64. //cout << "p_q " << p_q << endl; //65535, segfault
  65. process cur_process=v_cola[p_q][cur_pid];
  66. //cout << "asd4" << endl;
  67. switch (m) {
  68. case TICK:
  69. cur_process.quantum_count++;
  70. if (cur_process.quantum_count >= q_cola[p_q]) {
  71. switch_process = 1;
  72. cur_process.state = READY;
  73. cur_process.quantum_count = 0;
  74. if(p_q < n_colas) { //Hay una cola "peor" para ir; lo llevo
  75. v_cola[p_q+1][cur_pid]=cur_process;
  76. v_cola[p_q].erase(cur_pid);
  77. }
  78. }
  79. break;
  80. case BLOCK:
  81. switch_process = 1;
  82. cur_process.state = BLOCKED;
  83. break;
  84. case EXIT:
  85. switch_process = 1;
  86. v_cola[p_q].erase(cur_pid);
  87. break;
  88. }
  89. if (switch_process)
  90. return next_pid();
  91. return cur_pid;
  92. }