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

📄 一维数组版.cpp

📁 操作系统调度算法(先来先服务,短作业优先,时间片轮转,响应比优先)
💻 CPP
字号:
////////////////////////////////////////////////////////////进程调度算法 
#include <iostream>
#include <iomanip>
using namespace std;
const    max = 100;                 
float    arrive_time [max];                     //到达时间
float    service_time [max];                    //服务时间
float    operate_time [max];                    //开始执行时间 
float    achieve_time [max];                    //完成时间
double   circulate_time [max];                  //周转时间
double   w_circulate_time [max];                //带权周转时间
double   avg_circulate_time;                    //平均周转时间
double   avg_w_circulate_time;                  //平均带权周转时间
int      n,i;
double   total1=0;
double   total2=0;
///////////////////////////////////////////////////////////输入
void input()
{
	cout<<"please insert the number of process:";
	cin>>n;
	cout<<"please insert the arrive time of each process:";
	for ( i = 0 ; i < n ; i++ )
		cin>>arrive_time[i];
	cout<<"please insert the service time of each process:";
	for ( i = 0 ; i < n ; i++ )
		cin>>service_time[i];
}
//////////////////////////////////////////////////////////////////打印
void print()
{
	cout<<"AT"<<setw(5)<<"ST"<<setw(5)
		<<"OT"<<setw(5)<<"FT"<<setw(5)<<"CT"<<setw(5)
		<<"W_CT"<<endl;
	for ( i = 0 ; i < n ; i++ )
		cout<<arrive_time[i]<<setw(5)<<service_time[i]<<setw(5)
		<<operate_time[i]<<setw(5)<<achieve_time[i]<<setw(5)<<circulate_time[i]<<setw(5)<<setprecision(3)
		<<w_circulate_time[i]<<endl;
	cout<<"the average of circulate:"<<total1/n<<endl;
	cout<<"the w_average of circulate:"<<total2/n<<endl;
}
////////////////////////////////////////////////////////////////////冒泡排序
void recorder()
{
	for( i = 0 ; i < n ; i++ )
		for( int j = i+1 ; j < n ; j++)
		{
			if( arrive_time[i] > arrive_time[j] )
			{
				int temp1 = arrive_time[j];
				arrive_time[j] = arrive_time[i];
				arrive_time[i] = temp1;
				int temp3 = service_time[j];
				service_time[j] = service_time[i];
				service_time[i] = temp3;
			}
		}
}
//////////////////////////////////////////////////////////先来先服务算法
void fcfs()
{
	recorder();
	operate_time[0] = arrive_time[0];
	achieve_time[0] = service_time[0] + operate_time[0];
	for ( i = 1 ; i < n ; i++ )
	{
		operate_time[i] = achieve_time[i-1];
		if( arrive_time[i] < achieve_time[i-1] )
			achieve_time[i] = service_time[i] + achieve_time[i-1];
		else
			achieve_time[i] = arrive_time[i] + service_time[i];
	}
	for ( i = 0 ; i < n ; i++ )
	{
		circulate_time [i] = achieve_time[i] - arrive_time[i];
		w_circulate_time [i] = circulate_time [i] / service_time[i];
	}
	for ( i = 0 ; i < n ; i++ )
		total1 +=circulate_time [i];
	for ( i = 0 ; i < n ; i++ )
		total2 +=w_circulate_time [i];
}
/////////////////////////////////////////////////////短作业优先算法
void sjf()
{
	int     next_jcb;
	int     now_time=0;
	for  ( int i = 0 ;  i < n ;  i++ )
		achieve_time[i]=0;
	for  ( i = 0 ; i < n ; i++)
	{
		next_jcb=0;
		while (next_jcb<n && (achieve_time[next_jcb]!=0 || arrive_time[next_jcb]>=now_time))
		{
			next_jcb++;
		}
		if (next_jcb==n)
		{
			next_jcb=0;
			while (achieve_time[next_jcb]!=0)
				next_jcb++;
			for (int j=0; j<n; j++)
			{
				if (achieve_time[j]==0 && arrive_time[j]<arrive_time[next_jcb])
					next_jcb=j;
			}
			now_time=arrive_time[next_jcb];
		}
		else
		{
			for (int j=0; j<n; j++)
			{
				if (service_time[j]<=service_time[next_jcb] && achieve_time[j]==0 && arrive_time[j]<=now_time)
					next_jcb=j;
			}
		}
		operate_time[next_jcb]=now_time;
		achieve_time[next_jcb]=operate_time[next_jcb]+service_time[next_jcb];
		circulate_time[next_jcb]=achieve_time[next_jcb]-arrive_time[next_jcb];
		w_circulate_time[next_jcb]=circulate_time[next_jcb]/service_time[next_jcb];
		avg_circulate_time+=circulate_time[next_jcb];
		avg_w_circulate_time+=w_circulate_time[next_jcb];
		now_time=achieve_time[next_jcb];
		cout << next_jcb+1 << " ";
	}
	for ( i = 0 ; i < n ; i++ )
		total1 +=circulate_time [i];
	for ( i = 0 ; i < n ; i++ )
		total2 +=w_circulate_time [i];
	cout << endl;
}
///////////////////////////////////////////////////////时间片轮转算法
void rr()
{
	int a=0;
	int q;                             //////////////////时间片
	int count=0;
	int still[max];
	cout<<"input q:";
	cin>>q;
	for(i=0;i<n;i++)
		still[i]=service_time[i];
	for(i=0;i<100;i++)
	{

		if(still[i%n]==0)
			continue;
		else 
		{
			if(still[i%n]-q>0)
			{
				still[i%n]=still[i%n]-q;
				count+=q;
			}
			else
			{
				count+=still[i%n];
				still[i%n]=0;
				achieve_time[i%n]=count;
			}
			cout<<"第"<<count<<"秒末"<<setw(6);
			for(int k=0;k<n;k++)
				cout<<setw(6)<<still[k]<<" ";
			cout<<endl;
		}
		
		
	}
	for ( i = 0 ; i < n ; i++ )
	{
		circulate_time [i] = achieve_time[i] - arrive_time[i];
		w_circulate_time [i] = circulate_time [i] / service_time[i];
	}
	for ( i = 0 ; i < n ; i++ )
		total1 +=circulate_time [i];
	for ( i = 0 ; i < n ; i++ )
		total2 +=w_circulate_time [i];
	

}
///////////////////////////////////////////////////////响应比优先算法
void hrn()
{
	float  Rp[max];
	float  now_time=0;
	int    next_jcb;
	for  ( int i = 0 ;  i<n ;  i++)
		achieve_time[i]=0;
	for ( i = 0 ; i<n ; i++)
	{
		next_jcb=0;
		while (next_jcb<n && (achieve_time[next_jcb]!=0 || arrive_time[next_jcb]>=now_time))
		{
			next_jcb++;
		}
		if (next_jcb==n)
		{
			next_jcb=0;
			while (achieve_time[next_jcb]!=0)
				next_jcb++;
			for (int j=0; j<n; j++)
			{
				if (achieve_time[j]==0 && arrive_time[j]<arrive_time[next_jcb])
					next_jcb=j;
			}
			now_time=arrive_time[next_jcb];
		}
		else
		{
			for (int j=0; j<n; j++)
			{
				Rp[j]=(now_time-arrive_time[j])/service_time[j];
				if (Rp[j]>=Rp[next_jcb] && achieve_time[j]==0 && arrive_time[j]<=now_time)
					next_jcb=j;
			}
		}
		operate_time[next_jcb]=now_time;
		achieve_time[next_jcb]=operate_time[next_jcb]+service_time[next_jcb];
		operate_time[next_jcb]=achieve_time[next_jcb]-arrive_time[next_jcb];
		w_circulate_time[next_jcb]=operate_time[next_jcb]/service_time[next_jcb];
		circulate_time[next_jcb]=achieve_time[next_jcb]-arrive_time[next_jcb];
		avg_circulate_time+=operate_time[next_jcb];
		avg_w_circulate_time+=w_circulate_time[next_jcb];
		now_time=achieve_time[next_jcb];
	}
	for ( i = 0 ; i < n ; i++ )
		total1 +=circulate_time [i];
	for ( i = 0 ; i < n ; i++ )
		total2 +=w_circulate_time [i];
	cout << endl;
}
int main()
{	
	while(1)
	{
		for(i=0;i<n;i++)
		{
			arrive_time [i]=0;                     //到达时间
			service_time [i]=0;                    //服务时间
			operate_time [i]=0;                    //开始执行时间 
			achieve_time [i]=0;                    //完成时间
			circulate_time [i]=0;                  //周转时间
			w_circulate_time [i]=0;                //带权周转时间
			avg_circulate_time=0;                    //平均周转时间
			avg_w_circulate_time=0;
			total1=0;
			total2=0;
		}
		char c;
		cout<<"请选择算法(提示0退出):\n1.先来先服务\n2.短作业优先\n3.时间片轮转\n4.响应比优先\n";
		cin>>c;
		if(c=='0')
			break;
		switch(c)
		{
			case '1':
				input();
				fcfs();
				print();
				break;
			case '2':
				input();
				sjf();
				print();
				break;
			case '3':
				input();
				rr();
				print();
				break;
			case '4':
				input();
				hrn();
				cout<<"the average of circulate:"<<total1/n<<endl;
				cout<<"the w_average of circulate:"<<setprecision(4)<<total2/n<<endl;
				break;
		}
	}
	
	return 0;
}














⌨️ 快捷键说明

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