sched_rsjf.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "sched_rsjf.h"
  2. using namespace std;
  3. SchedRSJF::SchedRSJF(vector<int> argn) {
  4. /* Constructor SchedRSJF: Recibe la cantidad de
  5. cores, sus cpu_quantum y el tiempo de los procesos
  6. por parámetro. */
  7. nucleos = argn[1];
  8. quantums = new uint[nucleos];
  9. for (uint i = 2; i < argn.size(); i++) {
  10. if ((i-2) < nucleos) {
  11. // Cargamos los Quantums de cada core
  12. quantums[i - 2] = argn[i];
  13. } else {
  14. // Cargamos los procesos con un id y una duration
  15. uint duration = argn[i];
  16. p_map[i - 2 - nucleos].duration = duration;
  17. }
  18. }
  19. }
  20. SchedRSJF::~SchedRSJF() {
  21. p_map.clear(); // Limpiamos el mapeo de procesos
  22. while (!pq.empty()) pq.pop(); // Clean the priority queue
  23. delete[] quantums;
  24. }
  25. void SchedRSJF::load(int pid) {
  26. uint duration = p_map[pid].duration; // Get Process duration
  27. pq.push(Process{pid, duration}); // Push the Process to the priority queue
  28. }
  29. void SchedRSJF::unblock(int pid) {
  30. /* SchedRSJF solo corre tasks del tipo TaskCPU, entonces no
  31. hay acciones especificadas para este método. */
  32. cout << "Unblock process con " << pid;
  33. }
  34. int SchedRSJF::next_process(int cur_pid) {
  35. p_map[cur_pid].quantum_count = 0; // Limpiamos los quantums usados
  36. uint duration = p_map[cur_pid].duration;
  37. pq.push(Process{cur_pid, duration}); // Encolamos con prioridad
  38. int next_pid = pq.top().pid; // Obtenemos el siguiente proceso
  39. pq.pop();
  40. return next_pid;
  41. }
  42. int SchedRSJF::tick(int core, const enum Motivo m) {
  43. uint cur_pid = current_pid(core);
  44. if (cur_pid == IDLE_TASK || m == EXIT) {
  45. // Caso donde m == EXIT o core IDLE
  46. if (!pq.empty()) {
  47. // Hay más tareas para ejecutar
  48. int next_pid = pq.top().pid;
  49. pq.pop();
  50. return next_pid;
  51. } else {
  52. // No hay más tareas encoladas, devolver IDLE
  53. return IDLE_TASK;
  54. }
  55. } else if (m == TICK) {
  56. p_map[cur_pid].duration -= 1; // Decrementamos la duration del proceso
  57. p_map[cur_pid].quantum_count++; // Incrementamos el quantum del proceso
  58. if (p_map[cur_pid].quantum_count >= quantums[core]) {
  59. /* Switch de proceso por desalojo, por otro lado duration
  60. siempre va a ser igual o mayor a 1, porque hubiera devuelto
  61. EXIT y no TICK */
  62. int next_pid = next_process(cur_pid);
  63. return next_pid;
  64. } else {
  65. return cur_pid; // El proceso esta corriendo
  66. }
  67. } else if (m ==BLOCK) {
  68. // Proceso con estado BLOCK, no sucede en este scheduler
  69. // pero si pasa lo esperamos hasta que termine
  70. return cur_pid;
  71. }
  72. }