📄 fifo.cpp
字号:
#include "fifo.h"
#include <FSTREAM>
using namespace std;
fifo::fifo(int seconds_per_page):simulator(seconds_per_page)
{
}
void fifo::simulate_arbitrary(string file)
{
string outfile ="arbitray.out";
ofstream osf(outfile.c_str());
loadworkload(file);
osf<<"Simulating!"<<endl;
int finish_time = 0;
float agg_latency = 0;
float mean_latency;
int totaljob =0;
event evt;
for(int time =1;!waiting.empty()||!workload.empty();time++)
{
//requesting print event is coming !
while(!workload.empty() && time == workload.front().arrival_time())
{
evt= workload.front();
osf<<"\tArriving: "<<evt.getjob()<<" at "<<time<<" seconds"<<endl;
waiting.push(workload.front());
workload.pop();
}
//do the print job !
if(!waiting.empty() && time >= finish_time)
{
totaljob ++;
agg_latency += time - evt.arrival_time();
evt = waiting.front();
osf<<"\tServicing: "<<evt.getjob()<<" at "<<time<<" seconds"<<endl;
waiting.pop();
finish_time = time + evt.getjob().getnumpages() * seconds_per_page;
}
}
osf<<"\n\ttotal job "<<totaljob<<endl;
osf<<"\taggregate latency: "<<agg_latency<<" seconds"<<endl;
mean_latency = agg_latency / totaljob;
osf<<"\tmean latency : "<<mean_latency<<" seconds "<<endl;
osf.close();
}
void fifo::simulate_bigfirst(string file)
{
string outfile = "bigfirst.out";
ofstream osf(outfile.c_str());
loadworkload(file);
osf<<"Simulating!"<<endl;
int finish_time = 0;
float agg_latency = 0;
float mean_latency;
int totaljob =0;
event evt;
for(int time =1;!priority_waiting.empty()||!workload.empty();time++)
{
//requesting print event is coming !
while(!workload.empty() && time == workload.front().arrival_time())
{
evt= workload.front();
osf<<"\tArriving: "<<evt.getjob()<<" at "<<time<<" seconds"<<endl;
priority_waiting.push(workload.front());
workload.pop();
}
//do the print job !
if(!priority_waiting.empty() && time >= finish_time)
{
totaljob ++;
agg_latency += time - evt.arrival_time();
evt = priority_waiting.top();
osf<<"\tServicing: "<<evt.getjob()<<" at "<<time<<" seconds"<<endl;
priority_waiting.pop();
finish_time = time + evt.getjob().getnumpages() * seconds_per_page;
}
}
osf<<"\n\ttotal job "<<totaljob<<endl;
osf<<"\taggregate latency: "<<agg_latency<<" seconds"<<endl;
mean_latency = agg_latency / totaljob;
osf<<"\tmean latency : "<<mean_latency<<" seconds "<<endl;
osf.close();
}
void fifo::simulate(string file)
{
if(file.find("arbitrary")!= string::npos)
{
simulate_arbitrary(file);
cout<<"The log file name is arbitrary.out"<<endl;
return;
}
if(file.find("bigfirst") != string::npos)
{
simulate_bigfirst(file);
cout<<"The log file name is arbitrary.out"<<endl;
return;
}
cerr<<"The program don't know what algorithm to use"<<endl;
cerr<<"You should specify the file name with arbitrary or bigfirst"<<endl;
}
bool operator < (event evtleft,event evtright)
{
return evtleft.getjob().getnumpages() < evtright.getjob().getnumpages();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -