| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- #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
- nucleos = argn[1];
- quantum = argn[2];
- cur_pid = IDLE_TASK;
- }
- SchedRR::~SchedRR() {
- p_map.clear();
- }
- void SchedRR::load(int pid) {
- p_map[pid].state=READY;
- }
- void SchedRR::unblock(int pid) {
- p_map[pid].state=READY;
- }
- int SchedRR::next_pid(){
- // FIXME Usar colas?
- //Hasta el fin de la 'lista', hay alguno listo?
- for( it_type it = p_map.find(cur_pid); it != p_map.end(); it++) {
- if (it->first==cur_pid || it->first==IDLE_TASK) //Como arranco del sig?
- continue;
- if (it->second.state == READY){
- return it->first;
- }
- }
- //Desde el inicio hasta donde estaba, hay alguno listo?
- for( it_type it = p_map.begin(); it != p_map.find(cur_pid); it++) {
- if (it->first==IDLE_TASK)
- continue;
- if (it->second.state == READY)
- return it->first;
- }
- if (p_map[cur_pid].state==READY)
- return cur_pid;
- return IDLE_TASK;
- }
- int SchedRR::tick(int cpu, const enum Motivo m) {
- uint switch_process=0;
- 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 (p_map[cur_pid].quantum_count>quantum) {
- 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;
- }
-
- return cur_pid;
- }
|