Agustín Cangiani vor 10 Jahren
Ursprung
Commit
21d48e06a0
3 geänderte Dateien mit 89 neuen und 8 gelöschten Zeilen
  1. 2 0
      Makefile
  2. 67 7
      sched_rsjf.cpp
  3. 20 1
      sched_rsjf.h

+ 2 - 0
Makefile

@@ -55,6 +55,8 @@ ejercicio5: all makedir
 ejercicio7: all makedir
 	./simusched ejer7-sjf.tsk 1 2 99 SchedSJF 1 15 10 5 | ./graphsched.py > informe/imagenes/ejercicio7_SJF_1core.png
 	./simusched ejer7-sjf.tsk 2 2 99 SchedSJF 2 15 10 5 | ./graphsched.py > informe/imagenes/ejercicio7_SJF_2core.png
+	./simusched ejer7-rsjf.tsk 1 2 4 SchedRSJF 1 2 20 4 5 | ./graphsched.py > informe/imagenes/ejercicio7_RSJF_1core.png
+	./simusched ejer7-rsjf.tsk 2 2 4 SchedRSJF 2 2 2 20 4 5 | ./graphsched.py > informe/imagenes/ejercicio7_RSJF_2core.png
 
 ejercicio9: all makedir
 	./simusched ejer2.tsk 1 2 99 SchedRR 1 02 | ./graphsched.py > informe/imagenes/ejercicio9_1core_02q.png

+ 67 - 7
sched_rsjf.cpp

@@ -3,23 +3,83 @@
 using namespace std;
 
 SchedRSJF::SchedRSJF(vector<int> argn) {
-        // Recibe la cantidad de cores y sus cpu_quantum por parámetro
-/* llenar */
+    /* Constructor SchedRSJF: Recibe la cantidad de 
+    cores, sus cpu_quantum y el tiempo de los procesos 
+    por parámetro. */
+    nucleos = argn[1];
+    quantums = new uint[nucleos];
+
+    for (uint i = 2; i < argn.size(); i++) {
+        if ((i-2) < nucleos) {
+            // Cargamos los Quantums de cada core
+            quantums[i - 2] = argn[i];
+        } else {
+            // Cargamos los procesos con un id y una duration
+            uint duration = argn[i];
+            p_map[i - 2 - nucleos].duration = duration;
+        }
+	}
 }
 
 SchedRSJF::~SchedRSJF() {
-/* llenar */
+    p_map.clear();  // Limpiamos el mapeo de procesos
+    while (!pq.empty()) pq.pop();  // Clean the priority queue
+	delete[] quantums;
 }
 
 void SchedRSJF::load(int pid) {
-/* llenar */
+    uint duration = p_map[pid].duration;  // Get Process duration
+    pq.push(Process{pid, duration});  // Push the Process to the priority queue
 }
 
 void SchedRSJF::unblock(int pid) {
-/* llenar */
+    /* SchedRSJF solo corre tasks del tipo TaskCPU, entonces no 
+    hay acciones especificadas para este método. */
+    cout << "Unblock process con " << pid;
+}
+
+int SchedRSJF::next_process(int cur_pid) {
+    p_map[cur_pid].quantum_count = 0;  // Limpiamos los quantums usados
+    uint duration = p_map[cur_pid].duration;
+
+    pq.push(Process{cur_pid, duration});  // Encolamos con prioridad
+    
+    int next_pid = pq.top().pid;  // Obtenemos el siguiente proceso
+    pq.pop();
+
+    return next_pid;
 }
 
 int SchedRSJF::tick(int core, const enum Motivo m) {
-/* llenar */
-	return 0;
+    uint cur_pid = current_pid(core);
+
+    if (cur_pid == IDLE_TASK || m == EXIT) {
+        // Caso donde m == EXIT o core IDLE
+        if (!pq.empty()) {
+            // Hay más tareas para ejecutar
+            int next_pid = pq.top().pid;
+            pq.pop();
+            return next_pid;
+        } else {
+            // No hay más tareas encoladas, devolver IDLE
+            return IDLE_TASK;
+        }
+    } else if (m == TICK) {
+        p_map[cur_pid].duration -= 1;  // Decrementamos la duration del proceso
+        p_map[cur_pid].quantum_count++;  // Incrementamos el quantum del proceso
+
+        if (p_map[cur_pid].quantum_count >= quantums[core]) {
+            /* Switch de proceso por desalojo, por otro lado duration
+            siempre va a ser igual o mayor a 1, porque hubiera devuelto
+            EXIT y no TICK */
+            int next_pid = next_process(cur_pid);
+            return next_pid;
+	    } else {
+            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;
+    }
 }

+ 20 - 1
sched_rsjf.h

@@ -1,9 +1,11 @@
 #ifndef __SCHED_RSJF__
 #define __SCHED_RSJF__
 
+#include <map>
 #include <vector>
 #include <queue>
 #include <algorithm>
+#include <iostream>
 #include "basesched.h"
 
 using namespace std;
@@ -15,9 +17,26 @@ class SchedRSJF : public SchedBase {
 		virtual void initialize() {};
 		virtual void load(int pid);
 		virtual void unblock(int pid);
+		virtual int next_process(int pid);
 		virtual int tick(int cpu, const enum Motivo m);	
 	private:
-/* llenar */
+		uint nucleos;
+		uint* quantums;
+
+		struct Process {
+			int pid;
+			uint duration;
+			uint quantum_count;
+
+			// Esto permite ordenar por la duración mínima
+			int operator()(const Process& me, const Process& other) {
+				cout << "Duration: " << me.duration << " < " << other.duration << "\n";
+  				return me.duration > other.duration;
+			}	
+		};
+
+		std::map<int, Process> p_map;  // Mapeo de los procesos
+		std::priority_queue<Process, std::vector<Process>, Process> pq;  // Procesos en estado READY o RUNNING
 	
 };