资源简介

Experiment 1 Printer Simulation: FIFO(2 hours), fifo.cpp,fifo.h

资源截图

代码片段和文件信息


#include “fifo.h“
#include 

using namespace std;

fifo::fifo(int seconds_per_page):simulator(seconds_per_page){}

void fifo::simulate(string file){
    loadworkload(file);

    queue waiting;
    queue 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“<
     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:  “ <                           <<“ 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<     cout<<“Aggregate latency: “ <     cout<<“Mean latency: “<< aggregate_latency/eventNum <<“ seconds“ << endl;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       2879  2017-06-19 16:05  实验源代码1\fifo.cpp

     文件        324  2011-05-11 23:10  实验源代码1\fifo.h

     目录          0  2017-06-23 21:50  实验源代码1

----------- ---------  ---------- -----  ----

                 3203                    3


评论

共有 条评论