| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- #include <vector>
- #include <queue>
- #include <iostream>
- #include "sched_sjf.h"
- using namespace std;
- SchedSJF::SchedSJF(vector<int> argn) {
- // Recibe la cantidad de cores
- cout << "Constructor SchedSJF \n";
- nucleos = argn[1];
- firstTick=true;
- for (uint i = 2; i < argn.size(); i++) {
- // Cargamos los procesos con un id y una duration
- uint duration = argn[i];
- //cout << "Loadinggg " << i-2 << " " << duration << "\n";
- p_map[i - 2].duration = duration;
- }
- }
- SchedSJF::~SchedSJF() {
- p_map.clear(); // Limpiamos el mapeo de procesos
- while (!pq.empty()) pq.pop(); // Clean the priority queue
- }
- void SchedSJF::load(int pid) {
- uint duration = p_map[pid].duration; // Get Process duration
- pq.push(Process{pid, duration}); // Push the Process to the priority queue
- }
- void SchedSJF::unblock(int pid) {
- /* SchedSJF solo corre tasks del tipo TaskCPU, entonces no
- hay acciones especificadas para este método. */
- cout << "Unblock process with " << pid;
- }
- int SchedSJF::tick(int cpu, const enum Motivo m) {
- /* motivo TICK / BLOCK / EXIT / IDLE_TASK */
- uint cur_pid = current_pid(cpu);
- if (firstTick){ //FIXME
- cur_pid=0;
- firstTick=false;
- }
- cout << "Inicio tick: cpu " << cpu << " motivo " << m << " curr_pid " << cur_pid << "\n";
- if (m == TICK) {
- return cur_pid; // El proceso esta corriendo
- } else if (m ==BLOCK) {
- // Proceso con estado BLOCK, no sucede en este scheduler
- // pero si pasa lo esperamos hasta que termine
- return cur_pid;
- } else {
- // Caso donde m == EXIT
- if (!pq.empty()) {
- int next_pid = pq.top().pid;
- pq.pop();
- return next_pid;
- } else {
- // No hay más tareas encoladas, devolver IDLE
- return IDLE_TASK;
- }
- }
- }
|