浏览代码

Ejercicio 7

Agustín Cangiani 10 年之前
父节点
当前提交
7ad8246d68
共有 3 个文件被更改,包括 60 次插入8 次删除
  1. 5 0
      Makefile
  2. 38 7
      sched_sjf.cpp
  3. 17 1
      sched_sjf.h

+ 5 - 0
Makefile

@@ -52,8 +52,13 @@ ejercicio5: all makedir
 	./simusched ejer2.tsk 1 2 99 SchedRR 1 05 | ./graphsched.py > informe/imagenes/ejercicio5_1core_05q.png
 	./simusched ejer2.tsk 1 2 99 SchedRR 1 10 | ./graphsched.py > informe/imagenes/ejercicio5_1core_10q.png
 
+<<<<<<< a695ec35076782488d3b470e0a1c48df60f8c8b5
 
 ejercicio9: all makedir
 	./simusched ejer2.tsk 1 2 99 SchedRR 1 02 | ./graphsched.py > informe/imagenes/ejercicio9_1core_02q.png
 	./simusched ejer2.tsk 1 2 99 SchedRR 1 05 | ./graphsched.py > informe/imagenes/ejercicio9_1core_05q.png
 	./simusched ejer2.tsk 1 2 99 SchedRR 1 10 | ./graphsched.py > informe/imagenes/ejercicio9_1core_10q.png
+=======
+ejercicio7: all makedir
+	./simusched ejer7-sjf.tsk 1 2 99 SchedSJF 1 15 10 5
+>>>>>>> Ejercicio 7

+ 38 - 7
sched_sjf.cpp

@@ -6,23 +6,54 @@
 using namespace std;
 
 SchedSJF::SchedSJF(vector<int> argn) {
-        // Recibe la cantidad de cores 
-/* llenar */
+    // Recibe la cantidad de cores 
+    cout << "Constructor SchedSJF \n";
+    cout << "Test \n" ;
+    nucleos = argn[1];
+
+    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() {
-/* llenar */
+    p_map.clear();  // Limpiamos el mapeo de procesos
+    while (!pq.empty()) pq.pop();  // Clean the priority queue
 }
 
 void SchedSJF::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 SchedSJF::unblock(int pid) {
-/* llenar */
+    /* 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) {
-/* llenar */
-	return 0;
+    /* motivo TICK / BLOCK / EXIT / IDLE_TASK */
+    uint cur_pid = current_pid(cpu);
+    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;
+        }
+    }
 }

+ 17 - 1
sched_sjf.h

@@ -1,6 +1,7 @@
 #ifndef __SCHED_SJF__
 #define __SCHED_SJF__
 
+#include <map>
 #include <vector>
 #include <queue>
 #include <algorithm>
@@ -17,7 +18,22 @@ class SchedSJF : public SchedBase {
 		virtual void unblock(int pid);
 		virtual int tick(int cpu, const enum Motivo m);	
 	private:
-/* llenar */
+		uint nucleos;
+
+		struct Process {
+			int pid;
+			uint duration;
+
+			// Esto permite ordenar por la duración mínima
+			int operator()(const Process& me, const Process& other) {
+  				return me.duration > other.duration;
+			}	
+		};
+
+		std::map<int, Process> p_map;  // Mapeo de los procesos
+		typedef std::map<int,Process>::iterator it_type;
+
+		std::priority_queue<Process, std::vector<Process>, Process> pq;  // Procesos en estado READY o RUNNING
 };
 
 #endif