进程调度算法5种全

操作系统进程调度算法

5种方式都包含
我这里的短作业优先采用的是按照时间和短作业比较方法,判断执行的。

# include <stdio.h>
# include <stdlib.h> 
# include <string.h>
# include<iostream>
# include<queue>
# include<list>
#include <Windows.h>
using namespace std;
struct input{
	int num;
	char name;
    double daoda;
    double service;  
    
}input[ ]={
    { 0, 'A', 0, 3 },
    { 1, 'B', 2, 6 },
    { 2, 'C', 4, 4 },
    { 3, 'D', 6, 5 },
    { 4, 'E', 8, 2 }
}; 
struct FCFS{
	char name;
	double wancheng;
    double changetime;//平均周转时间
	double powertime;//带权周转时间 
}FCFS[6];
struct SJF{
	char name;
	double wancheng;
    double changetime;//平均周转时间
	double powertime;//带权周转时间 
}SJF[6];
struct GXY{
	char name;
	double wancheng;
    double changetime;//平均周转时间
	double powertime;//带权周转时间 
	double zanshi;//存放暂时的优先权 
}GXY[6];
struct RR{
	char name;
	double wancheng;
    double changetime;//平均周转时间
	double powertime;//带权周转时间 
	double ser;//存放任务经历的时间 
}RR[6];
struct MFQ{
	char name;
	double wancheng;
    double changetime;//平均周转时间
	double powertime;//带权周转时间 
	double ser;
}MFQ[7];
//list<node>ListQ;   //链表队列
char jincheng[6]={'A','B','C','D','E'};//进程名 
int  daoda[6]={0,2,4,6,8};//到达时间 
int service[6]={3,6,4,5,2};//服务时间 
int time[7];//到达时间排序之后的表 
int duan[7];//按照作业长短排序 
double FCFSchangetime=0;
double FCFSpowertime=0; 
double SJFchangetime=0;
double SJFpowertime=0;
double GXYchangetime=0;
double GXYpowertime=0;
double RRchangetime=0;
double RRpowertime=0;
double MFQchangetime=0;
double MFQpowertime=0;


void  shurujincheng();//1输入作业模拟情况; 
void perform();//显示作业情况
void xianlaixianfuwu();//先来先服务 
void performFCFS();//先来先服务算法输出时间 
void paixutime();//按照到达时间排序 
void duanzuoye();//短作业优先
void paixuwork();//按照作业服务时间排序 
void performSJF();//输出短作业优先 
void gaoxingying();//高响应比 
void performGXY();//输出高响应比 
void timeswitch();//时间片轮转
void performRR() ;//输出时间片轮转 
void duojiMFQ();//多级反馈队列 
void performMFQ();//输出多级反馈队列 
int main()
{
	int n = 1;
	while( n!=0 )
	{
		printf("**************************\n") ;
		printf("进程调度模拟程序:\n");
		printf("1: 输入作业模拟情况\n");
		printf("2: 显示作业情况\n");
		printf("3: 先来先服务\n");
		printf("4: 短作业优先算法\n");
		printf("5: 高响应比优先算法\n");
		printf("6: 时间片轮转算法\n");
		printf("7: 多级反馈队列\n");
		printf("8: 算法结果比较\n");
		printf("0: 退出\n");
		printf("请输入选择:\n");
		printf("*****************************\n");
		//printf("%d\n",n);
		scanf("%d",&n);	
		switch(n)
		{
		    case 1:
		    	   printf("\n*****************************\n");
		    	    shurujincheng();
		    	    perform();
		    	    printf("\n*****************************\n");
		    	break;
		    case 2:
		    	    printf("\n*****************************\n");
		    	    perform();
		    	    printf("\n*****************************\n");
		    	break;
		    case 3:
		    	   printf("\n*****************************\n");
		    	   xianlaixianfuwu();
		    	   perform();
		    	   performFCFS();
		    	   printf("\n*****************************\n");
		    	break;
		    case 4:
		    	   printf("\n*****************************\n");
		    	   //duanzuoye();
		    	   duanzuoye(); 
		    	   perform();
		    	   performSJF();
		    	   printf("\n*****************************\n");
		    	break;
		    case 5:
		    	   printf("\n*****************************\n");
		    	   gaoxingying(); 
		    	   perform();
		    	   performGXY();
		    	   printf("\n*****************************\n");
		    	break;
		    case 6:
		    	   printf("\n*****************************\n");
		    	   timeswitch(); 
		    	   perform();
		    	   performRR();
		    	   printf("\n*****************************\n");
		    	break;
		    case 7:
		    	   printf("\n*****************************\n");
		    	   duojiMFQ();
		    	   perform();
		    	   performMFQ();
		    	   printf("\n*****************************\n");
		    	break;
		    case 8:
		    	   printf("\n*****************************\n");
		    	   perform();
		    	   printf("\n*****************************\n");
		    	   performFCFS();
		    	   printf("\n*****************************\n");
				   performSJF();
				   printf("\n*****************************\n");
		    	   performGXY();
		    	   printf("\n*****************************\n");
		    	   performRR();
		    	   printf("\n*****************************\n");
		    	   performMFQ();
		    	   printf("\n*****************************\n");
		    	break;
		    case 0: 
		           printf("\n*****************************\n");
		           n=0;
		           
		           printf("\n*****************************\n");
		    	break;
		    default :
			    printf("\n输入错误。请重新输入:\n");
			    continue;
						
		}	
	 } 
}


void  shurujincheng()
{
	int n;
	printf("输入5个作业:\n");
	printf("\n输入进程信息(进程名,到达时间,服务时间):\n");
	for(int i=0;i<5;i++)
	{
		getchar();
		printf("进程 %d :",i);
		input[i].name=getchar();
		printf("%c\n",input[i].name);
		printf("到达时间:");
		scanf("%lf",&input[i].daoda);
		printf("服务时间:");
		scanf("%lf",&input[i].service);	
		input[i].num=i;
	}	
	return ;
} 

void perform()
{
   printf("进程名      :");
   for(int i=0;i<5;i++)
   {
   	    printf("%8c",input[i].name);
   }
   printf("\n");
   printf("到达时间    :");
   for(int i=0;i<5;i++)
   {
   	    printf("%8.2lf",input[i].daoda);
   }
   printf("\n");
   printf("服务时间    :");
   for(int i=0;i<5;i++)
   {
   	    printf("%8.2lf",input[i].service);
   }
   printf("\n");
	
	
	
	return ;
}

void xianlaixianfuwu()//先来先服务 
{
	
	int daoda=0;//到达时间 
	int ser=0;//服务时间 
	daoda=input[time[0]].daoda+daoda; 
	paixutime();
	for(int i=0 ; i<5 ; i++)
	{
		
		if(daoda >= input[time[i]].daoda)//到达时间总是小于服务时间 
		{
			FCFS[time[i]].name=input[time[i]].name;
			FCFS[time[i]].wancheng=input[time[i]].service+daoda;
			FCFS[time[i]].changetime=FCFS[time[i]].wancheng-input[time[i]].daoda;
			FCFS[time[i]].powertime=FCFS[time[i]].changetime/input[time[i]].service;	
			daoda=daoda+input[time[i]].service;
		}
		else
		{
			daoda=input[time[i]].daoda;
			FCFS[time[i]].name=input[time[i]].name;
			FCFS[time[i]].wancheng=input[time[i]].service+daoda;
			FCFS[time[i]].changetime=FCFS[time[i]].wancheng-input[time[i]].daoda;
			FCFS[time[i]].powertime=FCFS[time[i]].changetime/input[time[i]].service;	
			daoda=daoda+input[time[i]].service;
		}
	    
	} 
	return ;
}

void performFCFS()
{ 
   double sumchange=0;
   double sumpower=0;
   //printf("\n");
   printf("完成时间    :");
   for(int i=0;i<5;i++)
   {
   	    printf("%8.2lf",FCFS[i].wancheng);
   }
   printf("\n");
   printf("周转时间    :");
   for(int i=0;i<5;i++)
   {
   	    printf("%8.2lf",FCFS[i].changetime);
   	    sumchange+=FCFS[i].changetime; 
   }
    printf("\n");
   printf("带全周转时间:");
   for(int i=0;i<5;i++)
   {
   	    printf("%8.2lf",FCFS[i].powertime);
   	    sumpower+=FCFS[i].powertime;
   }
   printf("\n");
   FCFSchangetime=sumchange/5;
   FCFSpowertime=sumpower/5;
   printf("平均周转时间\t\t带权周转时间\t\t\n");
   printf("%8.2lf\t\t%8.2lf\t\t\n",FCFSchangetime,FCFSpowertime);
   return ;
}

void duanzuoye()//短作业优先 
{
	paixutime();
	paixuwork();
    int daoda=0;//到达时间 
    //int ser=0;
    int i=0,j=0; 
    int flag[7];
    memset(flag,0,sizeof(flag)); 
    if(input[time[i]].name!=input[duan[j]].name)
	{
		daoda+=input[time[i]].daoda;
		SJF[time[i]].wancheng=daoda+input[time[i]].service;
		daoda+=input[time[i]].service;
		flag[time[i]]=1;
		SJF[time[i]].changetime=SJF[time[i]].wancheng-input[time[i]].daoda;
		SJF[time[i]].powertime=SJF[time[i]].changetime/input[time[i]].service;
	}
	printf("%d\n ",daoda);
    for(int k=1;k<5;k++)
	{
	    j=0;
	    i=0;
	    while(flag[time[i]]==1)
		{
			i++;
		}//保证每次比较的都是没有执行过的 
		while(flag[duan[j]]==1)
		{
			j++;
		}//保证每次比较的都是没有执行过的 
		//printf("%c %c\n",input[time[i]].name,input[duan[j]].name);
		if(daoda<input[time[i]].daoda && daoda<input[duan[j]].daoda)//保证2个都还没到达 
		{    
		    //printf("1%c  %c\n",input[time[i]].name,input[duan[j]].name);
			if(flag[time[i]]==0)
			{
				if(input[time[i]].daoda < input[duan[i]].daoda)
				{
					daoda=daoda+input[time[i]].daoda;
		            SJF[time[i]].wancheng=daoda+input[time[i]].service;
			        daoda+=input[time[i]].service;
			        flag[time[i]]=1;
					SJF[time[i]].changetime=SJF[time[i]].wancheng-input[time[i]].daoda;
					SJF[time[i]].powertime=SJF[time[i]].changetime/input[time[i]].service;
				}
				else
				{
					daoda+=input[duan[j]].daoda;
		    		SJF[duan[j]].wancheng=daoda+input[duan[j]].service;
					daoda+=input[duan[j]].service;
					flag[duan[j]]=1;
					SJF[duan[j]].changetime=SJF[duan[j]].wancheng-input[duan[j]].daoda;
					SJF[duan[j]].powertime=SJF[duan[j]].changetime/input[duan[j]].service;
				}
			}
			
			printf("%d \n ",daoda);
		}
		else if(input[time[i]].name==input[duan[j]].name && daoda>input[time[i]].name)//两个都到达了 
		{
		//	printf("2%c  %c\n",input[time[i]].name,input[duan[j]].name);
			if(input[time[i]].service < input[duan[j]].service)
			{
				//daoda=input[time[i]].daoda;
				//printf("%d",daoda);
		        SJF[time[i]].wancheng=daoda+input[time[i]].service;
			    daoda+=input[time[i]].service;
			    flag[time[i]]=1;
				SJF[time[i]].changetime=SJF[time[i]].wancheng-input[time[i]].daoda;
				SJF[time[i]].powertime=SJF[time[i]].changetime/input[time[i]].service;
				
			}
			else
			{
				   //daoda=input[duan[j]].daoda;
				   //printf("%d\n ",daoda);
		    		SJF[duan[j]].wancheng=daoda+input[duan[j]].service;
					daoda+=input[duan[j]].service;
					flag[duan[j]]=1;
					SJF[duan[j]].changetime=SJF[duan[j]].wancheng-input[duan[j]].daoda;
					SJF[duan[j]].powertime=SJF[duan[j]].changetime/input[duan[j]].service;
			}
		}
		else if(input[time[i]].daoda <daoda && input[duan[j]].daoda>daoda)//两个都到达了,此时比较的是 
		{
			//printf("3%c  %c\n",input[time[i]].name,input[duan[j]].name);
			//if(input[time[i]].service < input[duan[j]].service )
		    {
		    	//daoda=input[time[i]].daoda;
		    	//printf("%d\n",daoda);
		    	//printf("%c\n",input[time[i]].name);
		        SJF[time[i]].wancheng=daoda+input[time[i]].service;
			    daoda+=input[time[i]].service;
			    flag[time[i]]=1;
				SJF[time[i]].changetime=SJF[time[i]].wancheng-input[time[i]].daoda;
				SJF[time[i]].powertime=SJF[time[i]].changetime/input[time[i]].service;
			}
		 
		}
		else if(input[time[i]].daoda >daoda && input[duan[j]].daoda< daoda)
		{
					//printf("%d\n",daoda);
				    //printf("%c\n",input[duan[j]].name);
		    		SJF[duan[j]].wancheng=daoda+input[duan[j]].service;
					daoda+=input[duan[j]].service;
					flag[duan[j]]=1;
					SJF[duan[j]].changetime=SJF[duan[j]].wancheng-input[duan[j]].daoda;
					SJF[duan[j]].powertime=SJF[duan[j]].changetime/input[duan[j]].service;
		}
		else
		{
			//printf("3%c  %c\n",input[time[i]].name,input[duan[j]].name);
			if(input[time[i]].service < input[duan[j]].service )
		    {
		    	//daoda=input[time[i]].daoda;
		    	//printf("%d\n",daoda);
		    	//printf("%c\n",input[time[i]].name);
		        SJF[time[i]].wancheng=daoda+input[time[i]].service;
			    daoda+=input[time[i]].service;
			    flag[time[i]]=1;
				SJF[time[i]].changetime=SJF[time[i]].wancheng-input[time[i]].daoda;
				SJF[time[i]].powertime=SJF[time[i]].changetime/input[time[i]].service;
			}
			else
			{
				    //daoda=input[duan[j]].daoda;
				    //printf("%d\n",daoda);
				    //printf("%c\n",input[duan[j]].name);
		    		SJF[duan[j]].wancheng=daoda+input[duan[j]].service;
					daoda+=input[duan[j]].service;
					flag[duan[j]]=1;
					SJF[duan[j]].changetime=SJF[duan[j]].wancheng-input[duan[j]].daoda;
					SJF[duan[j]].powertime=SJF[duan[j]].changetime/input[duan[j]].service;
			}
		}	
	 } 
	
	
	return ;
}

void performSJF()
{ 
   double sumchange=0;
   double sumpower=0;
   //printf("\n");
   printf("完成时间    :");
   for(int i=0;i<5;i++)
   {
   	    printf("%8.2lf",SJF[i].wancheng);
   }
   printf("\n");
   printf("周转时间    :");
   for(int i=0;i<5;i++)
   {
   	    printf("%8.2lf",SJF[i].changetime);
   	    sumchange+=SJF[i].changetime; 
   }
    printf("\n");
   printf("带全周转时间:");
   for(int i=0;i<5;i++)
   {
   	    printf("%8.2lf",SJF[i].powertime);
   	    sumpower+=SJF[i].powertime;
   }
   printf("\n");
   SJFchangetime=sumchange/5;
   SJFpowertime=sumpower/5;
   printf("平均周转时间\t\t带权周转时间\t\t\n");
   printf("%8.2lf\t\t%8.2lf\t\t\n",SJFchangetime,SJFpowertime);
   return ;
}
void gaoxingying()
{
	paixutime();
	int flag[7];
	int num[7];
	int daoda=0;
    memset(flag,0,sizeof(flag)); 
	daoda+=input[time[0]].daoda;
	GXY[time[0]].wancheng=daoda+input[time[0]].service;
	daoda+=input[time[0]].service;
	flag[time[0]]=1;
	GXY[time[0]].changetime=GXY[time[0]].wancheng-input[time[0]].daoda;
	GXY[time[0]].powertime=GXY[time[0]].changetime/input[time[0]].service;
	int zanshi[7];
	memset(zanshi,0,sizeof(zanshi)); 
	int maxnum=0;
	int max=0;
	for(int i=1;i<5;i++)
	{
		if(daoda < input[time[i]].daoda)//还没到 
		{
			printf("%c ",input[time[i]].name);
			daoda=input[time[i]].daoda; 
			GXY[time[i]].wancheng=daoda+input[time[i]].service;
	        daoda+=input[time[i]].service;
	        flag[time[i]]=1;
	        GXY[time[i]].changetime=GXY[time[i]].wancheng-input[time[i]].daoda;
	        GXY[time[i]].powertime=GXY[time[i]].changetime/input[time[i]].service;
		}
		else//已经到了 
		{
			for(int j=1;j<5;j++)//找到已经到了的 ,算出优先权存放起来 
			{
				if(time[j]<daoda)
				{
					if(flag[time[j]]==0)
					{
						printf("%c ",input[time[j]].name);
			             //printf("%d \n",daoda);
						//GXY[time[j]].wancheng=;
	                    //daoda+=input[time[j]].service;
	                    zanshi[time[j]]=1;
	                    GXY[time[j]].zanshi=(daoda+input[time[j]].service-input[time[j]].daoda)/input[time[j]].service+1;
	                    //GXY[time[j]].changetime=GXY[time[j]].wancheng-input[time[j]].daoda;
	                    //GXY[time[j]].powertime=GXY[time[j]].changetime/input[time[j]].service;
					}
				}
			}
			for(int j=1;j<5;j++)
			{
				if(zanshi[time[j]]==1)
				{
					if(max<GXY[time[j]].zanshi)
					{
						max=GXY[time[j]].zanshi;
						maxnum=time[j];
					}
				}
			}
			memset(zanshi,0,sizeof(zanshi)); 
			//daoda=input[time[i]].daoda; 
			printf("%c ",input[maxnum].name);
			printf("%d \n",daoda);
			GXY[maxnum].wancheng=daoda+input[maxnum].service;
	        daoda+=input[maxnum].service;
	        flag[maxnum]=1;
	        GXY[maxnum].changetime=GXY[maxnum].wancheng-input[maxnum].daoda;
	        GXY[maxnum].powertime=GXY[maxnum].changetime/input[maxnum].service; 
			maxnum=0;
			max=0;
		}	
	}
	return ;
}
void performGXY()
{ 
   double sumchange=0;
   double sumpower=0;
   //printf("\n");
   printf("完成时间    :");
   for(int i=0;i<5;i++)
   {
   	    printf("%8.2lf",GXY[i].wancheng);
   }
   printf("\n");
   printf("周转时间    :");
   for(int i=0;i<5;i++)
   {
   	    printf("%8.2lf",GXY[i].changetime);
   	    sumchange+=GXY[i].changetime; 
   }
    printf("\n");
   printf("带全周转时间:");
   for(int i=0;i<5;i++)
   {
   	    printf("%8.2lf",GXY[i].powertime);
   	    sumpower+=GXY[i].powertime;
   }
   printf("\n");
   GXYchangetime=sumchange/5;
   GXYpowertime=sumpower/5;
   printf("平均周转时间\t\t带权周转时间\t\t\n");
   printf("%8.2lf\t\t%8.2lf\t\t\n",GXYchangetime,GXYpowertime);
   return ;
}
void timeswitch()//时间片轮转 
{
   queue<int>q;
   int daoda=0; 
   int num;
   int zanshi=0;//存放没用完的时间片; 
   int z=0;
   int flag[7];
    memset(flag,0,sizeof(flag)); 
   printf("输入时间片长度:\n");
   scanf("%d",&num); 
   //printf("%d",num);
   paixutime();
   q.push(time[0]);
   flag[0]=1;
   for(int i=0;i<5;i++)
	 {
	 	RR[i].changetime=0;
	 	RR[i].wancheng=0;
	 	RR[i].ser=0;
	 	RR[i].powertime=0;
	 	
	 }
    while(!q.empty())
   {
	   //printf("q.size:%d \n",q.size());
   	   //printf("daoda:%d  zanshi:%d\n",daoda,zanshi); 
	   daoda+=num;
	   for(int i=1;i<5;i++)
	    {
	  		if(daoda>=input[time[i]].daoda &&flag[time[i]]==0)
	  		{
	  			q.push(input[time[i]].num);
	  			flag[time[i]]=1;
	  			//printf("进入队列:%c \n",input[time[i]].name);	
			}						 				 	
	   	}
			if((RR[q.front()].ser+num) >= input[q.front()].service)//任务可以执行完 
			{
				zanshi=num-(input[q.front()].service-RR[q.front()].ser);
				//printf("   任务完成:%c 到达时间:%d  剩余时间片:%d\n",input[q.front()].name,daoda,zanshi); 
				daoda=daoda-zanshi;
				RR[q.front()].ser=input[q.front()].service;	
				RR[q.front()].wancheng=daoda;
				
				RR[q.front()].changetime=RR[q.front()].wancheng-input[q.front()].daoda;
				RR[q.front()].powertime=RR[q.front()].changetime/input[q.front()].service;
				q.pop();
				
			}
			else //任务执行不完 
			{
			        RR[q.front()].ser+=num;
				    //printf("运行名字:%c 服务:%.2lf ",input[q.front()].name,RR[q.front()].ser);
				    
				    //printf("   任务未完成:%c 到达:%d  \n",input[q.front()].name,daoda);
					
				    z=q.front();
				    q.push(z);
				    //printf("进入队列 %c \n",input[q.back()].name);
				    q.pop();
				    zanshi=0;
			}	    
    }
  
} 

void performRR()
{
	double sumchange=0;
   double sumpower=0;
   //printf("\n");
   printf("完成时间    :");
   for(int i=0;i<5;i++)
   {
   	    printf("%8.2lf",RR[i].wancheng);
   }
   printf("\n");
   printf("周转时间    :");
   for(int i=0;i<5;i++)
   {
   	    printf("%8.2lf",RR[i].changetime);
   	    sumchange+=RR[i].changetime; 
   }
    printf("\n");
   printf("带全周转时间:");
   for(int i=0;i<5;i++)
   {
   	    printf("%8.2lf",RR[i].powertime);
   	    sumpower+=RR[i].powertime;
   }
   printf("\n");
   RRchangetime=sumchange/5;
   RRpowertime=sumpower/5;
   printf("平均周转时间\t\t带权周转时间\t\t\n");
   printf("%8.2lf\t\t%8.2lf\t\t\n",RRchangetime,RRpowertime);
   return ;
	
}


void duojiMFQ()//多级反馈队列 
{
    int daoda=0; 
     int num=0;
     int zanshi=0;//存放没用完的时间片; 
     int z=0;
     int flag[7],wan[7];//wan标记完成与否,flag标记进去没 
     memset(flag,0,sizeof(flag));
      memset(flag,0,sizeof(wan));
      paixutime();
     //q.push(time[0]);
     flag[0]=1;
     printf("申请队列数:\n");
     int n;
     scanf("%d",&n);
     queue<int>q[n];
	 q[0].push(time[0]);
	 int index=0;
	 for(int i=0;i<5;i++)
	 {
	 	MFQ[i].changetime=0;
	 	MFQ[i].wancheng=0;
	 	MFQ[i].ser=0;
	 	MFQ[i].powertime=0;
	 	
	 }
	 while(1)
	 {
	 	   for(int i=0;i<5;i++)//退出条件:所有的都执行了 
		    {
				if(wan[i]==1)
				{
					if(i==4)
					{
						printf("%d\n",i);
						return ;
					}
				}
				else
				{
					break;
				}
			}
			num=index+1;
			daoda+=num;//到达时间加运行时间
	 	    printf("daoda时间:%d 运行时间: %d\n",daoda,index+1);
			 for(int i=1;i<5;i++)//加入 
            {
            	if(daoda >= input[time[i]].daoda &&flag[time[i]]==0)
            	{
            		q[0].push(time[i]);
					flag[time[i]]=1;
					printf("进入队列的是 %c\n",input[time[i]].name); 
				}	
			} 
			if((MFQ[q[index].front()].ser+num) >= input[q[index].front()].service)//任务可以执行完 
			{
				zanshi=num-(input[q[index].front()].service-MFQ[q[index].front()].ser);
				printf("   任务完成:%c 到达时间:%d  \n",input[q[index].front()].name,daoda); 
				daoda=daoda-zanshi;
				MFQ[q[index].front()].ser=input[q[index].front()].service;	
				MFQ[q[index].front()].wancheng=daoda;
				
				MFQ[q[index].front()].changetime=MFQ[q[index].front()].wancheng-input[q[index].front()].daoda;
				MFQ[q[index].front()].powertime=MFQ[q[index].front()].changetime/input[q[index].front()].service;
				printf("完成时间:%d \n",MFQ[q[index].front()].wancheng); 
				wan[time[q[index].front()]]=1;
				q[index].pop();
			
			}
			else //任务执行不完 
			{
			        MFQ[q[index].front()].ser+=num;
				    printf("运行名字:%c 服务:%.2lf ",input[q[index].front()].name,MFQ[q[index].front()].ser);
				    
				    printf("   任务未完成:%c 到达:%d  \n",input[q[index].front()].name,daoda);
					
				    z=q[index].front();
				    if(index<(n-1))
				    {
				    	q[index+1].push(z);
				    	printf("%d %d \n",n-1,index+1);
					}
				      
				    else
				    {
				    	q[index].push(z);
					}
				    //printf("进入队列 %c 此时需转换队列 %d \n",input[q[index+1].back()].name,index+1);
				    q[index].pop();
				    zanshi=0;
			}
				for(int j=0; j<n;j++)
					{
						if(q[j].size()!=0)
						{
							index=j;
							break;
						}
						/*if(q[index].size()!=0)
						{
							break;
						}*/
					}
			
				    
	 }
     
	 return ;
}

void performMFQ()//输出多级反馈队列 
{
   double sumchange=0;
   double sumpower=0;
   //printf("\n");
   printf("完成时间    :");
   for(int i=0;i<5;i++)
   {
   	    printf("%8.2lf",MFQ[i].wancheng);
   }
   printf("\n");
   printf("周转时间    :");
   for(int i=0;i<5;i++)
   {
   	    printf("%8.2lf",MFQ[i].changetime);
   	    sumchange+=MFQ[i].changetime; 
   }
    printf("\n");
   printf("带全周转时间:");
   for(int i=0;i<5;i++)
   {
   	    printf("%8.2lf",MFQ[i].powertime);
   	    sumpower+=MFQ[i].powertime;
   }
   printf("\n");
   MFQchangetime=sumchange/5;
   MFQpowertime=sumpower/5;
   printf("平均周转时间\t\t带权周转时间\t\t\n");
   printf("%8.2lf\t\t%8.2lf\t\t\n",MFQchangetime,MFQpowertime);
   return ;

	
}

void paixutime()
{
	int zanshi1[6];
	int zanshi2[6];
	int num;
	for(int i=0;i<5;i++)
	{
		zanshi1[i]=input[i].daoda;
		zanshi2[i]=input[i].num;
	}
	for(int i=0;i<5;i++)
	{	
		for(int j=i+1;j<5;j++)
		{		
			if(zanshi1[i]>zanshi1[j])
			{
				num=zanshi2[i];
				zanshi2[i]=zanshi2[j];
				zanshi2[j]=num;
				
				num=zanshi1[i];
				zanshi1[i]=zanshi1[j];
				zanshi1[j]=num;				
			}
		 } 
	} 
	for(int i=0;i<5;i++)
	{
		time[i]=zanshi2[i];
		printf("%d ",time[i]);
	}//排序,按照到达时间长短
	printf("\n");
}

void paixuwork()
{
	int zanshi1[6];
	int zanshi2[6];
	int num;
	for(int i=0;i<5;i++)
	{
		zanshi1[i]=input[i].service;
		zanshi2[i]=input[i].num;
	}
	for(int i=0;i<5;i++)
	{	
		for(int j=i+1;j<5;j++)
		{		
			if(zanshi1[i]>zanshi1[j])
			{
				num=zanshi2[i];
				zanshi2[i]=zanshi2[j];
				zanshi2[j]=num;
				
				num=zanshi1[i];
				zanshi1[i]=zanshi1[j];
				zanshi1[j]=num;				
			}
		 } 
	} 
	for(int i=0;i<5;i++)
	{
		duan[i]=zanshi2[i];
		printf("%d ",duan[i]);
	}//排序,按照到达时间长短
	printf("\n");
	
	return ;
 } 

运行结果
实验输入需要先执行了3,4,5,6,7步,才可以执行2,显示所有作业情况

输入数据情况
输入数据情况
输入显示,接上图输入数据情况
执行先来先服务执行先来先服务执行短作业优先执行短作业优先

执行高响应比优先执行高响应比优先
蓝色字部分是过程中的测试可以后续自己删掉

时间片轮转时间片轮转
在这里插入图片描述多级反馈队列,过程可以自己注销

在这里插入图片描述
多级反馈队列所有过程最后结果输出在这里插入图片描述所有过程最后结果输出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值