⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fifo.cpp

📁 ssd5的答案 op2 希望大家用处!
💻 CPP
字号:

#include "fifo.h"


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

	
	queue<event> waiting;    //other queue<event> for waiting
	queue<event> servicing;   //the event is being serviced,it includes only one event

	int time_counter = 0;     //counting time
	int servicedtime = workload.front().arrival_time(); //arriving time of the first event
												//it is the time every event begins servicing
	int sz = workload.size();    //the size of workload, just the number of the events
	float aggregate_latency = 0;    //Aggregate latency
	int complete_jobs = 0;          //total completed jobs
	
	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;
		
			//arriving 
			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;}

			//servicing
			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; 

}







⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -