#include "fifo.h"
#include <iostream>
using namespace std;
fifo::fifo(int seconds_per_page):simulator(seconds_per_page){}
void fifo::simulate(string file){
loadworkload(file);
queue<event> waiting;
queue<event> servicing;
int timer = 0;//现在的时间
int service_time = workload.front().arrival_time();//打印时的时间
int sz = workload.size();
float aggregate_latency = 0;
int eventNum = 0;
cout<<"FIFO Simulation"<<endl;
while(!workload.empty()){
for( ;eventNum < sz; timer++){
while(true){
if(!workload.empty()){
//当timer等于workload中顶端元素的wait_until时,往waiting里压入even,在屏幕上打印出这项任务到达的信息。
//判断serving是否为空,空的话压入workload顶端元素even,并打印出这项任务正在打印的信息
if(timer == workload.front().arrival_time()){
waiting.push(workload.front());
cout <<"Arriving: " <<workload.front().getjob()
<<" at " << timer <<" seconds "<< endl;
if(servicing.empty()){
servicing.push(workload.front());
cout <<"Servicing: " << servicing.front().getjob()
<< " at " << timer <<" seconds "<< endl;
}
workload.pop();
}else
break;
}else
break;
}
if(waiting.empty()){
continue;
}
//判断serving是否为空,空的话,waiting弹出顶端元素。Serving压入waiting的顶端元素,并打印这项任务正在打印的信息,
//并且计算aggregate_latency += service_time - waiting.front().arrival_time();
if(servicing.empty()){
waiting.pop();
servicing.push(waiting.front());
cout <<"Servicing: " << servicing.front().getjob()
<< " at " << timer <<" seconds "<< endl;
aggregate_latency += service_time - waiting.front().arrival_time();
}
//判断timer == servicing.front().getjob().getnumpages() *seconds_per_page +service_time - 1
//如果是的话,证明上一项even已经处于结束阶段,打印时间+1,并且弹出serving的顶端元素。
if(timer == servicing.front().getjob().getnumpages() * seconds_per_page +
service_time - 1){
service_time = timer + 1;
servicing.pop();
eventNum++;
}
}
}
cout<<endl<<"Total jobs: " << eventNum <<endl;
cout<<"Aggregate latency: " <<aggregate_latency <<" seconds " <<endl;
cout<<"Mean latency: "<< aggregate_latency/eventNum <<" seconds" << endl;
}