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

📄 nfd.cpp

📁 操作系统 课程设计 非强占短进程优先算法和先来先服务算法
💻 CPP
字号:
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<conio.h>
#include<vector>
#include<string>
using namespace std;
//------------------------------------------------------------------------------------------------
//进程输入
struct process{
	double ctime;
	double utime;
	double time;
	string name;
};
bool flag(vector<process>& ps,string name0){
	if(ps.size ()==0) return 1;
	for(vector<process>::size_type i=0;i<ps.size ();i++)
       if(ps[i].name == name0) return 0;
	return 1;
}

void creat(vector<process>& ps0,process &p0){
	//cout<<endl;
p1:	cout<<"进程名: ";
	string name0;
    cin>> name0 ;
    if(!flag(ps0,name0)){ cout<<"进程名已经存在!重新输入:"<<endl;goto p1;}
	p0.name =name0;
	cout<<"到达时间(十进制数): ";
	cin>>p0.ctime ;
	cout<<"估计运行时间(十进制数): ";
	cin>>p0.utime ;
	p0.time =0;
	ps0.push_back (p0);
}

void print(vector<process>& ps){
	if(ps.size ()==0){cout<<"无进程"<<endl;return;}
	cout<<setw(12)<<"进程名"<<setw(10)<<"到达时间"<<setw(10)<<"运行时间"<<endl;
	for(vector<process>::size_type i=0;i<ps.size ();i++)
		cout<<setw(12)<<ps[i].name <<setw(10)<<ps[i].ctime<<setw(10)<<ps[i].utime<<endl;
	cout<<endl;
}
//-----------------------------------------------------------------------------------------------
//先来先服务算法

bool compare(const process& x,const process& y){
	return x.ctime <y.ctime ;
}
double time1(vector<process>& ps){
	double x=0;
	for(vector<process>::size_type i=0;i<ps.size ();i++)
	{
		if(i==0) {ps[0].time =ps[0].utime ;	x=x+ps[0].utime+ ps[0].ctime ;}                                                         
		else
		{
			if(x<ps[i].ctime){ps[i].time=ps[i].utime;x=ps[i].ctime+ps[i].utime;}
			else{ps[i].time =x + ps[i].utime -ps[i].ctime ;x=x+ps[i].utime;}
		}
	}
	x=0;
    for(vector<process>::size_type j=0;j<ps.size ();j++)
		x=x+ps[j].time ;
	return (x/ps.size ());	
}

double time2(vector<process>& ps){
	double x=0;
    for(vector<process>::size_type j=0;j<ps.size ();j++)
	     x=x+ps[j].time/ps[j].utime ; 
	return (x/ps.size ());	
}

void printf(vector<process>& ps){
	if(ps.size ()==0){cout<<"无进程"<<endl;return;}
	cout<<setw(12)<<"进程名"<<setw(10)<<"到达时间"<<setw(10)<<"运行时间"<<setw(10)<<"结束时间"<<setw(10)<<"周转时间"<<endl;
	for(vector<process>::size_type i=0;i<ps.size ();i++)
		cout<<setw(12)<<ps[i].name <<setw(10)<<ps[i].ctime<<setw(10)<<ps[i].utime<<setw(10)<<ps[i].time + ps[i].ctime<<setw(10)<<ps[i].time<<endl;
	cout<<endl;
}

void fc_serve(vector<process>& ps){
	sort(ps.begin (),ps.end (),compare);
	cout<<endl;
    double x,y;
	x=time1(ps);
	y=time2(ps);
	cout<<"      先来先服务算法进程执行顺序"<<endl;
	printf(ps);
	cout<<"      先来先服务算法评价:"<<endl;
	cout<<"      平均周转时间为:"<<x<<endl;
	cout<<"      平均带权周转时间为:"<<y<<endl;
}
//-----------------------------------------------------------------------------------------------
//非抢占短进程优先算法

bool compare0(const process& x,const process& y){
	return x.utime <y.utime ;
}

vector<process> select(vector<process>& ps,vector<process>& p){
	vector<process>::iterator iter0=ps.begin ();
	vector<process>::iterator iter1=iter0+1;
	if(ps.size ()==0 || iter1==ps.end ()) return ps;
	while(iter1->ctime ==iter0->ctime ) iter1=iter1+1;
	if(iter1!=iter0+1) sort(iter0,iter1,compare0);
	for( ;iter0!=iter1;++iter0) p.push_back (*iter0);
	double x=0;
	for(vector<process>::size_type i=0;i<p.size ();i++)
	    x=x+p[0].ctime+p[i].utime;
	while(iter1!=ps.end ()){
	    for( ; iter1!=ps.end () && iter1->ctime <=x;++iter1);
	    if(iter0==iter1) {p.push_back (*iter0);x=x+iter0->utime ; ++iter0; ++iter1;}
        else {
		   sort(iter0,iter1,compare0);
		   if(iter1==ps.end ()) {
			    for( ;iter0!=iter1; ++iter0)
			    	p.push_back (*iter0);
			    return p;
		   }
		p.push_back (*iter0);
		x=x+iter0->utime;
		++iter0;
		}
	}	
	return p;
}

void sp_serve(vector<process>& ps){
	sort(ps.begin (),ps.end (),compare);
	vector<process> p;
	p=select(ps,p);
	cout<<endl;
	double x,y;
	x=time1(p);
	y=time2(p);
	cout<<"      非抢占短进程优先算法进程执行顺序"<<endl;
	printf(p);
	cout<<"      非抢占短进程优先算法评价:"<<endl;
	cout<<"      平均周转时间为:"<<x<<endl;
	cout<<"      平均带权周转时间为:"<<y<<endl;
}

//-----------------------------------------------------------------------------------------------
int main (){
	process p;
	vector<process> ps;
	char ch;
	cout<<"*******************************************"<<endl;
    cout<<"*                                         *"<<endl;
    cout<<"* 模拟进程调度的时间是以十进制方式实现的。*"<<endl;
	cout<<"*                                         *"<<endl;
	cout<<"*******************************************"<<endl;
	cout<<endl;
	do{
        cout<<"请输入进程的基本信息:"<<endl;
        creat(ps,p);
		cout<<"是否继续输入(是'Y'):";
	    ch =_getch(); 
		cout<<endl;cout<<endl;
	}while(ch=='y' || ch=='Y');
	cout<<endl;cout<<endl;
	cout<<"      输入的进程信息为:"<<endl;
	print(ps);
	do{
	     cout<<endl;
		 cout<< "         ----= 算法菜单=------"<<endl; cout<<endl;
		 cout<< "       s - 非抢占短进程优先算法 "<<endl;
		 cout<< "       f - 先来先服务算法 "<<endl;
		 cout<< "       e - 退出 "<<endl;
		 ch =_getch(); 
		 switch(ch)
		 {
			 case 's' :
				 sp_serve(ps);
				 break;
			 case 'f' :
                 fc_serve(ps);
				 break;
			 case 'e':break;
			 default:
				 cout<<"操作错误!"<<endl;
		 }
		 cout<<endl;
	 }while(ch!='e');
	return 0;
}

⌨️ 快捷键说明

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