sched_sjf.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. // Recibe la cantidad de cores
  8. cout << "Constructor SchedSJF \n";
  9. nucleos = argn[1];
  10. firstTick=true;
  11. for (uint i = 2; i < argn.size(); i++) {
  12. // Cargamos los procesos con un id y una duration
  13. uint duration = argn[i];
  14. //cout << "Loadinggg " << i-2 << " " << duration << "\n";
  15. p_map[i - 2].duration = duration;
  16. }
  17. }
  18. SchedSJF::~SchedSJF() {
  19. p_map.clear(); // Limpiamos el mapeo de procesos
  20. while (!pq.empty()) pq.pop(); // Clean the priority queue
  21. }
  22. void SchedSJF::load(int pid) {
  23. uint duration = p_map[pid].duration; // Get Process duration
  24. pq.push(Process{pid, duration}); // Push the Process to the priority queue
  25. }
  26. void SchedSJF::unblock(int pid) {
  27. /* SchedSJF solo corre tasks del tipo TaskCPU, entonces no
  28. hay acciones especificadas para este método. */
  29. cout << "Unblock process with " << pid;
  30. }
  31. int SchedSJF::tick(int cpu, const enum Motivo m) {
  32. /* motivo TICK / BLOCK / EXIT / IDLE_TASK */
  33. uint cur_pid = current_pid(cpu);
  34. if (firstTick){ //FIXME
  35. cur_pid=0;
  36. firstTick=false;
  37. }
  38. cout << "Inicio tick: cpu " << cpu << " motivo " << m << " curr_pid " << cur_pid << "\n";
  39. if (m == TICK) {
  40. return cur_pid; // El proceso esta corriendo
  41. } else if (m ==BLOCK) {
  42. // Proceso con estado BLOCK, no sucede en este scheduler
  43. // pero si pasa lo esperamos hasta que termine
  44. return cur_pid;
  45. } else {
  46. // Caso donde m == EXIT
  47. if (!pq.empty()) {
  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. }
  56. }