Prechádzať zdrojové kódy

Merge branch 'ejercicio7' of david/so-tp1 into master

agustin 10 rokov pred
rodič
commit
b1f6ae0be6
4 zmenil súbory, kde vykonal 62 pridanie a 9 odobranie
  1. 4 1
      Makefile
  2. 6 0
      ejer7-sjf.tsk
  3. 35 7
      sched_sjf.cpp
  4. 17 1
      sched_sjf.h

+ 4 - 1
Makefile

@@ -50,8 +50,11 @@ ejercicio3: all makedir
 ejercicio5: all makedir
 	./simusched ejer2.tsk 1 2 99 SchedRR 1 02 | ./graphsched.py > informe/imagenes/ejercicio5_1core_02q.png
 	./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
+	./simusched ejer2.tsk 1 2 99 SchedRR 1 99 | ./graphsched.py > informe/imagenes/ejercicio5_1core_10q.png
 
+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
 
 ejercicio9: all makedir
 	./simusched ejer2.tsk 1 2 99 SchedRR 1 02 | ./graphsched.py > informe/imagenes/ejercicio9_1core_02q.png

+ 6 - 0
ejer7-sjf.tsk

@@ -0,0 +1,6 @@
+TaskCPU 15
+@5:
+TaskCPU 10
+@10:
+TaskCPU 5
+

+ 35 - 7
sched_sjf.cpp

@@ -6,23 +6,51 @@
 using namespace std;
 
 SchedSJF::SchedSJF(vector<int> argn) {
-        // Recibe la cantidad de cores 
-/* llenar */
+    /* Constructor SchedSJF: Recibe la cantidad de cores */
+    nucleos = argn[1];
+
+    for (uint i = 2; i < argn.size(); i++) {
+        // Cargamos los procesos con un id y una duration
+        uint duration = argn[i];
+        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 con " << pid;
 }
 
 int SchedSJF::tick(int cpu, const enum Motivo m) {
-/* llenar */
-	return 0;
+    uint cur_pid = current_pid(cpu);
+
+    if (cur_pid == IDLE_TASK || m == EXIT) {
+        // Caso donde m == EXIT o cpu 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) {
+        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;
+    }
 }

+ 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