Bläddra i källkod

No se si es buena idea que me deje pushear con -f

La cague en algo en el RR pero ya esta
Fabian 10 år sedan
förälder
incheckning
c407e91fdc
2 ändrade filer med 38 tillägg och 44 borttagningar
  1. 34 39
      sched_rr.cpp
  2. 4 5
      sched_rr.h

+ 34 - 39
sched_rr.cpp

@@ -1,38 +1,35 @@
-#include <map>
-#include <queue>
 #include "sched_rr.h"
 #include "basesched.h"
-#include <iostream>
 
 using namespace std;
 
 SchedRR::SchedRR(vector<int> argn) {
 	// Round robin recibe la cantidad de cores y sus cpu_quantum por parámetro
-	cur_pid = IDLE_TASK;
 	nucleos = argn[1];
 	quantums = new uint[nucleos];
 
 	for (int i = 0; i < nucleos; i++) {
-		quantums[i] = argn[i+2];
+		quantums[i] = argn[i + 2];
 	}
+
 }
 
 SchedRR::~SchedRR() {
 	p_map.clear();
-	delete [] quantums;
+	delete[] quantums;
 }
 
 void SchedRR::load(int pid) {
-	p_map[pid].state=READY;
+	p_map[pid].state = READY;
 }
 
 void SchedRR::unblock(int pid) {
-	p_map[pid].state=READY;
+	p_map[pid].state = READY;
 }
 
-int SchedRR::next_pid() {
+int SchedRR::next_pid(uint cur_pid) {
 	// Hasta el fin de la 'lista', hay alguno listo?
-	for(it_type it = ++p_map.find(cur_pid); it != p_map.end(); it++) {
+	for (it_type it = ++p_map.find(cur_pid); it != p_map.end(); it++) {
 		if (it->first == IDLE_TASK)
 			continue;
 
@@ -41,7 +38,7 @@ int SchedRR::next_pid() {
 	}
 
 	// Desde el inicio hasta donde estaba, hay alguno listo?
-	for(it_type it = p_map.begin(); it != p_map.find(cur_pid); it++) {
+	for (it_type it = p_map.begin(); it != p_map.find(cur_pid); it++) {
 		if (it->first == IDLE_TASK)
 			continue;
 
@@ -56,40 +53,38 @@ int SchedRR::next_pid() {
 }
 
 int SchedRR::tick(int cpu, const enum Motivo m) {
-	uint switch_process=0;
-
-	for (int i == 0; i<nucleos; i++) {
-		uint current = current_pid(
+	uint switch_process = 0;
+	uint cur_pid = current_pid(cpu);
+
+	switch (m) {
+	case TICK:
+		p_map[cur_pid].quantum_count++;
+		break;
+	case BLOCK:
+		switch_process = 1;
+		p_map[cur_pid].state = BLOCKED;
+		break;
+	case EXIT:
+		p_map.erase(cur_pid);
+		cur_pid = IDLE_TASK;
+		break;
 	}
 
-	switch(m) {
-		case TICK:
-			p_map[cur_pid].quantum_count++;
-			break;
-		case BLOCK:
-			switch_process=1;
-			p_map[cur_pid].state=BLOCKED;
-			break;
-		case EXIT:
-			switch_process=1;
-			p_map.erase(cur_pid);
-			cur_pid=IDLE_TASK;
-			break;
-	}
+	if (cur_pid == IDLE_TASK)
+		switch_process = 1;
 
-	if (cur_pid==IDLE_TASK)
-		switch_process=1;
-
-	if (p_map[cur_pid].quantum_count>=quantums[cpu]) {
-		switch_process=1;
-		p_map[cur_pid].state=READY;
-		p_map[cur_pid].quantum_count=0;
+	if (p_map[cur_pid].quantum_count >= quantums[cpu]) {
+		switch_process = 1;
+		p_map[cur_pid].state = READY;
+		p_map[cur_pid].quantum_count = 0;
 	}
 
 	if (switch_process) {
-		cur_pid=next_pid();
-		p_map[cur_pid].state=RUNNING;
+		// Implementación simple, se puede usar current_remaining(cpu) para ver
+		// si conviene cambiar de nucleo, y/o ver cuanto es el precio de migrar
+		cur_pid = next_pid(cur_pid);
+		p_map[cur_pid].state = RUNNING;
 	}
-	
+
 	return cur_pid;
 }

+ 4 - 5
sched_rr.h

@@ -1,8 +1,9 @@
 #ifndef __SCHED_RR__
 #define __SCHED_RR__
 
-#include <vector>
+#include <map>
 #include <queue>
+#include <vector>
 #include <algorithm>
 #include "basesched.h"
 
@@ -16,16 +17,14 @@ class SchedRR : public SchedBase {
 		virtual void load(int pid);
 		virtual void unblock(int pid);
 		virtual int tick(int cpu, const enum Motivo m);	
-		virtual int next_pid();
-		virtual int next_free_cpu();
+		virtual int next_pid(uint cur_pid);
 	private:
 		uint nucleos;
 		uint* quantums;
-		int cur_pid;
 		
 		enum state { READY, BLOCKED, RUNNING };
 		
-		struct process{
+		struct process {
 			int pid;
 			enum state state;
 			uint quantum_count;