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

📄 fcfs.cpp

📁 计算机操作系统中先来先服务算法fcfs模拟 环境C++语言 使用STL希望读者在阅读代码的时候有一定的STL基础
💻 CPP
字号:
#include <iostream>
#include <list>
#include <string>
using namespace std;

#include <time.h>
#include <stdio.h>
/**********************************************************************************/


struct PCB_time
{
	int PCB_time_m;
	int PCB_time_s;
};

struct PCB
{
	int pid;
	PCB_time arrive_time;                
	int request_time;	              //minitues
	PCB_time start_time;

};

void Display(list<PCB> PCB_list);
void main( void )
{
	list<PCB> PCB_list;
	PCB thisPCB;
	time_t now;
	time(&now);	

	struct tm when;

	PCB_time showTime;
	showTime.PCB_time_m = 0;
	showTime.PCB_time_s = 0;	
	int process_num = 0;
	cout << "input the process num\n";
	cin>>process_num;
	cout <<"please input the infomation the request time is seconds\n";
	while(process_num--)
	{
		time(&now);
		when = *localtime( &now );
		
		cin>>thisPCB.pid;
		cin>>thisPCB.request_time;
		thisPCB.arrive_time.PCB_time_m = when.tm_min;
		thisPCB.arrive_time.PCB_time_s = when.tm_sec;
		
		PCB_list.push_back(thisPCB); 
	}
	PCB_list.front().start_time = PCB_list.front().arrive_time;

	while(1)
	{
		/***************************show the processes********************************************/
		time(&now);
		when = *localtime( &now );
		if(when.tm_min > showTime.PCB_time_m || 
			(when.tm_min == showTime.PCB_time_m && when.tm_sec >= (showTime.PCB_time_s + 1)))
		{	
			if(PCB_list.size() == 0)
			{
				break;
			}
			Display(PCB_list);
			showTime.PCB_time_m = when.tm_min;
			showTime.PCB_time_s = when.tm_sec;
		}
		/***************************show the processes********************************************/

		while(1)
		{	
			PCB_time endTime;
			time(&now);
			when = *localtime( &now );	
			
			if(PCB_list.size() == 0)
			{
				break;				
			}
			/***************************get the ended time**********************************/
			endTime.PCB_time_m = PCB_list.front().start_time.PCB_time_m;
			endTime.PCB_time_s = PCB_list.front().start_time.PCB_time_s;

			if(PCB_list.front().start_time.PCB_time_s + PCB_list.front().request_time >=60)
			{
				endTime.PCB_time_m++;
				endTime.PCB_time_s = PCB_list.front().start_time.PCB_time_s 
					+ PCB_list.front().request_time - 60;
			}
			else
			{
				endTime.PCB_time_s = endTime.PCB_time_s
					+ PCB_list.front().request_time;
			}
			/********************************end getting the ended time*********************/
			time(&now);
			when = *localtime( &now );

			if((when.tm_min > endTime.PCB_time_m) || ( when.tm_sec >= endTime.PCB_time_s))
			{
				PCB_list.pop_front();
				PCB_list.front().start_time.PCB_time_m = when.tm_min;
				PCB_list.front().start_time.PCB_time_s = when.tm_sec;
			}				
			break;				
		}
	}

}

void Display(list<PCB> PCB_list)
{
	cout<<"now the processes are:\n";
	for(list<PCB>::iterator it = PCB_list.begin(); it!= PCB_list.end(); it++)
	{
		cout<<"("<<(*it).pid << " " <<(*it).arrive_time.PCB_time_m << ":" 
			<<(*it).arrive_time.PCB_time_s <<" "
			<<(*it).request_time <<")\n";
	}
}

⌨️ 快捷键说明

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