//"fifo.h"
#ifndef _FIFO_H
#define _FIFO_H
#include<queue>
#include<string>
using namespace std;
#include "simulator.h"
#include "event.h"
class fifo:public simulator {
public:
fifo(int seconds_per_page);
//the default construct
void simulate(string file);
//the function simulate
};
#endif
//"fifo.cpp"
#include<iostream>
using namespace std;
#include "fifo.h"
fifo::fifo(int seconds_per_page):simulator(seconds_per_page){}
//the default construct to initialize
void fifo::simulate(string file){
loadworkload(file);
queue<event> waiting;
queue<event> servicing;
int time_counter = 0;
int servicedtime = workload.front().arrival_time();
int sz = workload.size();
float aggregate_latency = 0;
int complete_jobs = 0;
cout<<"FIFO Simulation"<<endl<<endl;
//if workload is empty,go to the end,give the information
while(!workload.empty()){
for(;complete_jobs < sz; time_counter++){
event e;
while(1){
if(!workload.empty()){
e = workload.front();
//when the pinter is busy,the other job should wait, do it one by one.
if(time_counter == e.arrival_time()){
waiting.push(e);
cout <<"/tArriving: " << e.getjob()
<< " at " << time_counter <<" seconds "<< endl;
if(servicing.empty()){
servicing.push(e);
cout <<"/tServicing: " << servicing.front().getjob()
<< " at " << time_counter <<" seconds "<< endl;
}
workload.pop();
}else{
break;
}
}else{
break;
}
}
//if queue<event> waiting is empty,turn to the next circular
if(waiting.empty()){continue;}
if(servicing.empty()){
waiting.pop();
e = waiting.front();
servicing.push(e);
cout <<"/tServicing: " << servicing.front().getjob()
<< " at " << time_counter <<" seconds "<< endl;
aggregate_latency += servicedtime - waiting.front().arrival_time();
}
//if the event is finish serviced, pop it out from
//servicing queue and prepare for the next event
if(time_counter == servicing.front().getjob().getnumpages() * seconds_per_page +
servicedtime - 1){
servicedtime = time_counter + 1;
servicing.pop();
complete_jobs++;
}
}
}
//the output information
cout<<endl<<"/tTotal jobs: " << complete_jobs <<endl;
cout<<"/tAggregate latency: " <<aggregate_latency <<" seconds " <<endl;
cout<<"/tMean latency: "<< aggregate_latency/complete_jobs <<" seconds" << endl;
}