Skip to content

Instantly share code, notes, and snippets.

@violetcode
Created October 9, 2012 19:57
Show Gist options
  • Save violetcode/3861054 to your computer and use it in GitHub Desktop.
Save violetcode/3861054 to your computer and use it in GitHub Desktop.
struct process{
int id;
int arrival;
int burst;
};
queue<process> processes;
void first_come(){
queue<process> ready_queue;
bool running = false;
bool idle = false;
process p;
int t = 0;
int idle_time = 0;
while(!processes.empty()){
//put processes on the ready queue.
while(processes.front().arrival == t){
ready_queue.push(processes.front());
processes.pop();
}
//Select a process to run.
if(!running){
//If the ready queue is empty, then we idle.
if(ready_queue.empty()){
if(!idle){
cout << "Time " << t << " Idle" << endl;
}
idle = true;
running = false;
idle_time++;
//If the ready queue is not empty, then we select the first
//process from it.
} else {
idle = false;
running = true;
p = ready_queue.front();
cout << "Time " << t << " Process " << p.id << endl;
ready_queue.pop();
p.burst -= 1;
}
t++;
//If we're already running a process, wait for it to finish.
} else {
if(p.burst == 0){
cout << "Process " << p.id << " finished at " << t << endl;
running = false;
} else {
p.burst -= 1;
t++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment