sched_mfq.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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::tick(int core, const enum Motivo m) {
  38. uint switch_process = 0;
  39. uint cur_pid = current_pid(core);
  40. uint p_q = pid_queue(cur_pid);
  41. process cur_process=v_cola[p_q][cur_pid];
  42. switch (m) {
  43. case TICK:
  44. cur_process.quantum_count++;
  45. if (cur_process.quantum_count >= q_cola[p_q]) {
  46. switch_process = 1;
  47. cur_process.state = READY;
  48. cur_process.quantum_count = 0;
  49. if(p_q < n_colas) { //Hay una cola "peor" para ir; lo llevo
  50. v_cola[p_q+1][cur_pid]=cur_process;
  51. v_cola[p_q].erase(cur_pid);
  52. }
  53. }
  54. break;
  55. case BLOCK:
  56. switch_process = 1;
  57. cur_process.state = BLOCKED;
  58. break;
  59. case EXIT:
  60. switch_process = 1;
  61. v_cola[p_q].erase(cur_pid);
  62. break;
  63. }
  64. return 0;
  65. }