| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #include <map>
- #include <iostream>
- #include "sched_mfq.h"
- #include "basesched.h"
- using namespace std;
- SchedMFQ::SchedMFQ(vector<int> argn) {
- // MFQ recibe los quantums por parámetro
- n_colas = argn.size()-1;
- q_cola = new uint[n_colas];
- for( uint i = 0; i < n_colas ;i++ ){
- q_cola[i] = argn[1+i];
- v_cola.push_back(std::map<int,process>());
- }
- }
- SchedMFQ::~SchedMFQ() {
- delete[] q_cola;
- }
- void SchedMFQ::load(int pid) {
- v_cola[0][pid].state=READY; //Cargo PID en la cola de mayor prioridad
- }
- void SchedMFQ::unblock(int pid) {
- 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 "mejor" 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(int 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;
- cout << "WHAT THE FUCK " << pid << endl;
- for( uint i = 0; i < n_colas; i++ )
- cout << "size cola " << i << " = " << v_cola[i].size() << endl;
- return 65535; // ??
- }
- int SchedMFQ::ready_at(uint qn){
- if (v_cola[qn].size() == 0)
- return -1;
- for (it_type it = v_cola[qn].begin(); it != v_cola[qn].end(); it++) {
- cout << "Mirando al pid " << it->first << endl;
- if (it->second.state == READY){
- cout << "Me gusta " << it->first << endl;
- return it->first;
- }
- }
- return -1;
- }
- uint SchedMFQ::next_pid(){
- for( uint q = 0; q < n_colas; q++ ){
- cout << "Mirando en la pila " << q << endl;
- int p = ready_at(q);
- if (p != -1)
- return p; //Algun proceso listo
- }
- return IDLE_TASK;
- }
- void SchedMFQ::mostrar_estados(){
- cout << "RDY: " << READY << ", RNING: " << RUNNING << ", BLK: " << BLOCKED << endl;
- for( uint q = 0; q < n_colas; q++ ){
- cout << "Cola " << q << ", size: " << v_cola[q].size() << endl;
- for (it_type it = v_cola[q].begin(); it != v_cola[q].end(); it++) {
- cout << "PID: " << it->first << ", STATE: " << it->second.state << endl;
- }
- }
- }
- int SchedMFQ::tick(int core, const enum Motivo m) {
- uint switch_process = 0;
- int cur_pid = current_pid(core);
- cout << "PID at core " << core << " = " << cur_pid << endl;
- if (cur_pid == IDLE_TASK)
- return next_pid();
- uint p_q = pid_queue(cur_pid);
- if (p_q == 65535) //FIXME
- return IDLE_TASK;
- process* cur_process=&(v_cola[p_q][cur_pid]);
- switch (m) {
- case TICK:
- cur_process->quantum_count++;
- if (cur_process->quantum_count >= q_cola[p_q]) {
- switch_process = 1;
- cur_process->state = READY;
- cur_process->quantum_count = 0;
- cout << "Marcando a cur_pid("<< cur_pid<<") como READY (>quantum)" << endl;
- if(p_q < n_colas - 1) { //Hay una cola "peor" para ir; lo llevo
- v_cola[p_q+1][cur_pid]=*cur_process;
- v_cola[p_q].erase(cur_pid);
- }
- }
- break;
- case BLOCK:
- switch_process = 1;
- cur_process->state = BLOCKED;
- break;
- case EXIT:
- switch_process = 1;
- //cout << v_cola[p_q].size() << endl;
- cout << "borro pid: " << cur_pid << endl;
- v_cola[p_q].erase(cur_pid);
- //cout << v_cola[p_q].size() << endl;
- break;
- }
- mostrar_estados();
- if (switch_process) {
- cur_pid = next_pid();
- if (cur_pid == IDLE_TASK){
- cout << "SWITCHING -1 ??? " << endl;
- return IDLE_TASK;
- }
- p_q = pid_queue(cur_pid);
- process* next_process=&(v_cola[p_q][cur_pid]);
- next_process->state = RUNNING;
- return cur_pid;
- }
- return cur_pid;
- }
|