SSD5_Exercise 2

//"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;  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值