sched_sjf.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <vector>
  2. #include <queue>
  3. #include <iostream>
  4. #include "sched_sjf.h"
  5. using namespace std;
  6. SchedSJF::SchedSJF(vector<int> argn) {
  7. /* Constructor SchedSJF: Recibe la cantidad de cores */
  8. nucleos = argn[1];
  9. for (uint i = 2; i < argn.size(); i++) {
  10. // Cargamos los procesos con un id y una duration
  11. uint duration = argn[i];
  12. p_map[i - 2].duration = duration;
  13. }
  14. }
  15. SchedSJF::~SchedSJF() {
  16. p_map.clear(); // Limpiamos el mapeo de procesos
  17. while (!pq.empty()) pq.pop(); // Clean the priority queue
  18. }
  19. void SchedSJF::load(int pid) {
  20. uint duration = p_map[pid].duration; // Get Process duration
  21. pq.push(Process{pid, duration}); // Push the Process to the priority queue
  22. }
  23. void SchedSJF::unblock(int pid) {
  24. /* SchedSJF solo corre tasks del tipo TaskCPU, entonces no
  25. hay acciones especificadas para este método. */
  26. cout << "Unblock process con " << pid;
  27. }
  28. int SchedSJF::tick(int cpu, const enum Motivo m) {
  29. uint cur_pid = current_pid(cpu);
  30. if (cur_pid == IDLE_TASK || m == EXIT) {
  31. // Caso donde m == EXIT o cpu IDLE
  32. if (!pq.empty()) {
  33. // Hay más tareas para ejecutar
  34. int next_pid = pq.top().pid;
  35. pq.pop();
  36. return next_pid;
  37. } else {
  38. // No hay más tareas encoladas, devolver IDLE
  39. return IDLE_TASK;
  40. }
  41. } else if (m == TICK) {
  42. return cur_pid; // El proceso esta corriendo
  43. } else if (m ==BLOCK) {
  44. // Proceso con estado BLOCK, no sucede en este scheduler
  45. // pero si pasa lo esperamos hasta que termine
  46. return cur_pid;
  47. }
  48. }