优先级和时间片轮转法调度

时间:2021-12-05 21:13:20
#include "stdio.h" 
#include <malloc.h>
#define max 100 
#define etime 4 //定义CPU轮转时间片为4
#define pfree 0 /*process end*/ 
#define running 1 /*process running status*/ 
#define aready 2 /*process aready status */ 
#define blocking 3 /*process aready blocking status*/ 
typedef struct node 
{ 
	char name; 
	int status;
	int pri; //进程优先级
	int ptime;//进程时间
	int ax,bx,cx,dx; 
	int pc; 
	int psw; 
	
}pcb;
pcb *processarray[max]; //进程数组
static int processnum;
pcb *createprocess(pcb *processarray[]) //接收进程,放入进程数组
{ 
	pcb *p,*q; 
	int a,b,c,d,m,n,r,t; 
	char ID; 
	q=NULL; 
	printf("input the first nine status pcb:");  
	scanf("\n%c%d%d%d%d%d%d%d%d",&ID,&a,&b,&c,&d,&m,&n,&r,&t); 
	for(int i=0;i<max;i++)
	{ 
		p=(pcb*)malloc(sizeof(pcb)); //创建进程PCB
		p->name=ID; 
		p->ax=a; 
		p->bx=b; 
		p->cx=c; 
		p->dx=d; 
		p->pc=m; 
		p->psw=n;
		p->pri=r; //进程优先级
		p->ptime=t;//进程时间
		p->status=aready;//进程设为就绪状态
		processnum++;//进程数加1
		//创建进程数组
	    processarray[i]=p;
		printf("input the next seven status pcb: "); 
		scanf("\n%c",&ID);
		if (ID == '*')  //若进程名输入为“*”则终止输入
			break;
		scanf("%d%d%d%d%d%d%d%d",&a,&b,&c,&d,&m,&n,&r,&t);
	} 

//显示用户输入的进程
	for(int j=0;j<processnum;j++) 
	{
		q=processarray[j];
	printf("\n process name. status.ax. bx. cx. dx. pc. psw.  pri.  ptime\n "); 
	printf("%10c%5d%5d%5d%5d%5d%5d%5d%5d%5d",q->name,q->status,q->ax,q->bx,q->cx,q->dx,q->pc,q->psw,q->pri,q->ptime); 
	
	} 
	return *processarray;
} 

void processRR(pcb *processarray[])//轮转法(RR)算法
{int i;
 int j;
 
 pcb *q;
 int totaltime=0;//总时间
 //总时间=总共所需时间片个数*时间片单位长度
	for(i=0;i<processnum;i++)
	{	if(processarray[i]->ptime%4==0)
	 totaltime+=processarray[i]->ptime;
		else
			totaltime+=(processarray[i]->ptime/4+1)*4;}
   
	printf("\nUsing RR method:");
   	printf("\n process name. status.ax. bx. cx. dx. pc. psw.  pri.  ptime\n ");
	 for(i=0;i<processnum&&totaltime;j=0)
	{ q=processarray[i];
     if(q->ptime==0)//若进程还需时间为0
	    j=0;//此句无任何作用
	     
	else if(q->ptime<=etime)//若进程还需时间小于或等于时间片长度
		{ 
		  q->status=pfree;//进程状态设为已完成
		  q->ptime=0;

		  printf("\n%10c%5d%8d%5d%5d%5d%5d%5d%5d%5d",q->name,q->status,q->ax,q->bx,q->cx,q->dx,q->pc,q->psw,q->pri,q->ptime); /*check process running status */ 
	      totaltime-=etime;
	}
		else if(q->ptime>etime)//若进程还需时间大于时间片长度
		{q->status=running;
		q->ptime=q->ptime-etime;
		printf("\n%10c%5d%8d%5d%5d%5d%5d%5d%5d%5d",q->name,q->status,q->ax,q->bx,q->cx,q->dx,q->pc,q->psw,q->pri,q->ptime); /*check process running status */ 
        q->status=aready;
	 totaltime-=etime;}
	 if(i<processnum-1)
			i++;
		else if(i==processnum-1)
			i=0;//走完一遍进程数组后,返回首个进程再走一遍,检查并完成未完成的进程
	 }}




void processStaPri(pcb *processarray[]) /*use 静态优先级算法 */ 
{ 
	pcb *q; 
    int i,j;
	printf("\n the process use StaPri method.\n"); 
	printf("running the frist process:\n"); 
	// 通过冒泡算法按进程优先级顺序重新排列进程数组
	for(i=0;i<processnum;i++) 
	for(j=0;j<processnum-1;j++)
	   {if(processarray[j]->pri>processarray[j+1]->pri)
	{q=processarray[j];
	processarray[j]=processarray[j+1];
	processarray[j+1]=q;}}
	
	for(i=0;i<processnum;i++)
	   {
        processarray[i]->status=running;//进程状态设为运行状态
		q=processarray[i];
			printf("\n process name. status.ax. bx. cx. dx. pc. psw.  pri.  ptime\n "); 
	printf("\n%10c%5d%8d%5d%5d%5d%5d%5d%5d%5d",q->name,q->status,q->ax,q->bx,q->cx,q->dx,q->pc,q->psw,q->pri,q->ptime); /*check process running status */ 
	q->status=pfree;//进程状态设为已完成
	}

 
	for(j=0;j<processnum;j++)
	{q=processarray[j];
		printf("\n检查进程%3c是否结束,进程当前状态:%3d",q->name,q->status);
	} 
    printf("\ngame is over!\n"); 
} 





void main() 
{ 
   
    *processarray=createprocess(processarray);
	processRR(processarray);
	processStaPri(processarray);
	
}