Browse Source

mfq primer implementacion. falta implementar 'next_pid()'

David 10 năm trước cách đây
mục cha
commit
d3912f6855
2 tập tin đã thay đổi với 70 bổ sung6 xóa
  1. 56 5
      sched_mfq.cpp
  2. 14 1
      sched_mfq.h

+ 56 - 5
sched_mfq.cpp

@@ -1,3 +1,4 @@
+#include <map>
 #include <vector>
 #include <queue>
 #include "sched_mfq.h"
@@ -7,22 +8,72 @@ using namespace std;
 
 SchedMFQ::SchedMFQ(vector<int> argn) {
 	// MFQ recibe los quantums por parámetro
-/* llenar */
+	n_colas = argn[1];
+	q_cola = new uint[n_colas];
+
+	for( uint i = 0; i < n_colas ;i++ ){
+		q_cola[i] = argn[2+i];
+		v_cola.push_back(std::map<int,process>());
+	}
 }
 
 SchedMFQ::~SchedMFQ() {
-/* llenar */
+	delete[] q_cola;
 }
 
 void SchedMFQ::load(int pid) {
-/* llenar */
+	v_cola[0][pid].state=READY; //Cargo PID en la cola de mayor prioridad
 }
 
 void SchedMFQ::unblock(int pid) {
-/* llenar */
+	uint p_q=pid_queue(pid);
+	v_cola[p_q][pid].state = READY;
+	v_cola[p_q][pid].quantum_count = 0;
+
+	if(p_q > 0) { //Hay una cola "peor" para ir; lo llevo
+		v_cola[p_q-1][pid]=v_cola[p_q][pid];
+		v_cola[p_q].erase(pid);
+	}
+}
+
+uint SchedMFQ::pid_queue(uint pid){
+	for( uint i = 0; i < n_colas; i++ )
+		if ( v_cola[i].count(pid) == 1) //Solo hay 0/1 key en un map
+			return i;
+
+	return 65535; // ??
 }
 
+
 int SchedMFQ::tick(int core, const enum Motivo m) {
-/* llenar */
+	uint switch_process = 0;
+	uint cur_pid = current_pid(core);
+
+	uint p_q = pid_queue(cur_pid);
+	switch (m) {
+	case TICK:
+		v_cola[p_q][cur_pid].quantum_count++;
+		if (v_cola[p_q][cur_pid].quantum_count >= q_cola[p_q]) {
+			switch_process = 1;
+			v_cola[p_q][cur_pid].state = READY;
+			v_cola[p_q][cur_pid].quantum_count = 0;
+			if(p_q < n_colas) { //Hay una cola "peor" para ir; lo llevo
+				v_cola[p_q+1][cur_pid]=v_cola[p_q][cur_pid];
+				v_cola[p_q].erase(cur_pid);
+			}
+		}
+		break;
+	case BLOCK:
+		switch_process = 1;
+		v_cola[p_q][cur_pid].state = BLOCKED;
+		break;
+	case EXIT:
+		switch_process = 1;
+		v_cola[p_q].erase(cur_pid);
+		break;
+	}
+
+
+
 	return 0;
 }

+ 14 - 1
sched_mfq.h

@@ -1,5 +1,6 @@
 #ifndef __SCHED_MFQ__
 #define __SCHED_MFQ__
+#define uint unsigned int
 
 #include <vector>
 #include <queue>
@@ -16,7 +17,19 @@ class SchedMFQ : public SchedBase {
 		virtual int tick(int n, const enum Motivo m);
 	
 	private:
-/* llenar */
+		enum state { READY, BLOCKED, RUNNING };
+		struct process {
+			enum state state;
+			uint quantum_count;
+		};
+
+		uint pid_queue(uint);
+		uint n_colas;
+		uint* q_cola;
+		std::vector< std::map<int, process> > v_cola;
+
+		typedef std::map<int,process>::iterator it_type;
+		
 };
 
 #endif