sched_mfq.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. switch (m) {
  42. case TICK:
  43. v_cola[p_q][cur_pid].quantum_count++;
  44. if (v_cola[p_q][cur_pid].quantum_count >= q_cola[p_q]) {
  45. switch_process = 1;
  46. v_cola[p_q][cur_pid].state = READY;
  47. v_cola[p_q][cur_pid].quantum_count = 0;
  48. if(p_q < n_colas) { //Hay una cola "peor" para ir; lo llevo
  49. v_cola[p_q+1][cur_pid]=v_cola[p_q][cur_pid];
  50. v_cola[p_q].erase(cur_pid);
  51. }
  52. }
  53. break;
  54. case BLOCK:
  55. switch_process = 1;
  56. v_cola[p_q][cur_pid].state = BLOCKED;
  57. break;
  58. case EXIT:
  59. switch_process = 1;
  60. v_cola[p_q].erase(cur_pid);
  61. break;
  62. }
  63. return 0;
  64. }