#pragma once
#ifndef FIFO_H#define FIFO_H#include"simulator.h"class fifo : public simulator
{ public: fifo(int second_per_page); virtual void simulate(string file); //virtual void addevent(event e);private:
//queue<event> wait;}; #endif
#include "event.h"
#include "fifo.h"
fifo::fifo(int second_per_page) : simulator(second_per_page) {}
void fifo::simulate(string file)
{ loadworkload(file);//注意此处如果在fifo类中有私有的queue<event>将会初始化次对象而不是simulotor中的对象int latency = 0;//每个客户的服务时间
int aggregate = 2;//服务完成时间 int time;//进队时间 int page;//wait类中的头元素页数 string name;//同上,此处三个变量是为了判断服务时wait中有无服务完成队首元素 queue<event> wait;//服务队列 event ev = event(job(0,""),0); wait.push(ev); int count = 0; while (count < 100)//这个。。。想不到一个合适的变量来判定 { if (!workload.empty()) { while (count == workload.front().arrival_time())//进入服务队列 { wait.push(workload.front());cout << "arriving: " << workload.front().getjob().getnumpages() << " pages from " << workload.front().getjob().getuser() << " at " << count << "second" << endl;
workload.pop();
if (workload.empty()) break;}
}if (!wait.empty())
{ page = wait.front().getjob().getnumpages(); name = wait.front().getjob().getuser(); time = wait.front().arrival_time(); if (count == aggregate) { wait.pop();//服务时,轮到该元素时正值上一元素刚好完成,将其出队 if (!wait.empty()) { cout << "servicing: " << wait.front().getjob().getnumpages() << " pages from " << wait.front().getjob().getuser() << " at " << count << " second" << endl; latency = seconds_per_page*wait.front().getjob().getnumpages(); } } } if (!wait.empty()) { if (page != wait.front().getjob().getnumpages() || name != wait.front().getjob().getuser() || time != wait.front().arrival_time()) aggregate += latency; }count++;
}
}