使用动态优先权的进程调度算法的模拟

(1)用C语言实现对5个进程采用动态优先权优先算法的进程调度。
(2)每个用来标识进程的进程控制块PCB用结构来描述,包括以下字段:
·进程标识树ID
·进程优先数PRIORITY,并规定优先数越大的进程,其优先全越高。
·进程已占用的CPU时间CPUTIME。
·进程还需占用的CPU时间ALLTIME。当进程运行完毕时,ALLTIME变为0
·进程的阻塞时间STARTBLOCK,表示当进程再运行STARTBLOCK个时间片后,进程将进入阻塞状态
·进程被阻塞的时间BLOCKTIME,表示已阻塞的进程再等待BLOCKTIME个时间片后,将转换成就绪状态
·进程状态STATE
(3)优先数改变的原则:
·进程在就绪队列中待一个时间片,优先数增加1
·进程每运行一个时间片,优先数减3
(4)假设在调度前,系统中有5个进程,它们的初始状态如下:
ID 0 1 2 3 4
PRIORITY 9 38 30 29 0
CPUTIME 0 0 0 0 0
ALLTIME 3 3 6 3 4
STARTBLOCK 2 -1 -1 -1 -1
BLOCKTIME 3 0 0 0 0
STATE READY READY READY READY READY
(5)为了清楚的观察诸进度的调度过程,程序应将每个时间片内的进程的情况显示出来,具体格式如下:
RUNNING PROG:i
========================================================================
ID 0 1 2 3 4
PRIORITY P0 P1 P2 P3 P4
CPUTIME C0 C1 C2 C3 C4
ALLTIME A0 A1 A2 A3 A4
STARTBLOCK T0 T1 T2 T3 T4
BLOCKTIME B0 B1 B2 B3 B4
STATE READY READY READY READY READY
代码如下:

#include<stdio.h>
#include <iostream.h>
void main()
{
int ID[5]={0,1,2,3,4};
int PRIORITY[5]={9,38,30,29,0};
int CPUTIME[5]={0,0,0,0,0};
int ALLTIME[5]={3,3,6,3,4};
int STARTBLOCK[5]={2,-1,-1,-1,-1};
int BLOCKTIME[5]={3,0,0,0,0};
int str[5];//1=READY,0=BLOCK,-1=END
int max5(int PRIORITY50,int PRIORITY51,int PRIORITY52,int PRIORITY53,int PRIORITY54);
int max4(int PRIORITY40,int PRIORITY41,int PRIORITY42,int PRIORITY43);
int max3(int PRIORITY30,int PRIORITY31,int PRIORITY32);
int max2(int PRIORITY20,int PRIORITY21);
int i=1,a,b0,b1,b2,b3,b4,c0,c1,c2,c3,c4;
//===================================================================
if(max5(ALLTIME[0],ALLTIME[1],ALLTIME[2],ALLTIME[3],ALLTIME[4])>0)
{
for(;max5(ALLTIME[0],ALLTIME[1],ALLTIME[2],ALLTIME[3],ALLTIME[4])>0;)
{
if(ALLTIME[0]>0||ALLTIME[1]>0||ALLTIME[2]>0||ALLTIME[3]>0||ALLTIME[4]>0)
a=max5(PRIORITY[0],PRIORITY[1],PRIORITY[2],PRIORITY[3],PRIORITY[4]);
else if(ALLTIME[0]==0||ALLTIME[1]>0||ALLTIME[2]>0||ALLTIME[3]>0||ALLTIME[4]>0)
a=max4(PRIORITY[1],PRIORITY[2],PRIORITY[3],PRIORITY[4]);
else if(ALLTIME[0]>0||ALLTIME[1]==0||ALLTIME[2]>0||ALLTIME[3]>0||ALLTIME[4]>0)
a=max4(PRIORITY[0],PRIORITY[2],PRIORITY[3],PRIORITY[4]);
else if(ALLTIME[0]>0||ALLTIME[1]>0||ALLTIME[2]==0||ALLTIME[3]>0||ALLTIME[4]>0)
a=max4(PRIORITY[0],PRIORITY[1],PRIORITY[3],PRIORITY[4]);
else if(ALLTIME[0]>0||ALLTIME[1]>0||ALLTIME[2]>0||ALLTIME[3]==0||ALLTIME[4]>0)
a=max4(PRIORITY[0],PRIORITY[1],PRIORITY[2],PRIORITY[4]);
else if(ALLTIME[0]>0||ALLTIME[1]>0||ALLTIME[2]>0||ALLTIME[3]>0||ALLTIME[4]==0)
a=max4(PRIORITY[0],PRIORITY[1],PRIORITY[2],PRIORITY[3]);
//===================================================================
else if(ALLTIME[0]==0||ALLTIME[1]==0||ALLTIME[2]>0||ALLTIME[3]>0||ALLTIME[4]>0)
a=max3(PRIORITY[2],PRIORITY[3],PRIORITY[4]);
else if(ALLTIME[0]==0||ALLTIME[1]>0||ALLTIME[2]==0||ALLTIME[3]>0||ALLTIME[4]>0)
a=max3(PRIORITY[1],PRIORITY[3],PRIORITY[4]);
else if(ALLTIME[0]==0||ALLTIME[1]>0||ALLTIME[2]>0||ALLTIME[3]==0||ALLTIME[4]>0)
a=max3(PRIORITY[1],PRIORITY[2],PRIORITY[4]);
else if(ALLTIME[0]==0||ALLTIME[1]>0||ALLTIME[2]>0||ALLTIME[3]>0||ALLTIME[4]==0)
a=max3(PRIORITY[1],PRIORITY[2],PRIORITY[3]);
else if(ALLTIME[0]>0||ALLTIME[1]==0||ALLTIME[2]==0||ALLTIME[3]>0||ALLTIME[4]>0)
a=max3(PRIORITY[0],PRIORITY[3],PRIORITY[4]);
else if(ALLTIME[0]>0||ALLTIME[1]==0||ALLTIME[2]>0||ALLTIME[3]==0||ALLTIME[4]>0)
a=max3(PRIORITY[0],PRIORITY[2],PRIORITY[4]);
else if(ALLTIME[0]>0||ALLTIME[1]==0||ALLTIME[2]>0||ALLTIME[3]>0||ALLTIME[4]==0)
a=max3(PRIORITY[0],PRIORITY[2],PRIORITY[3]);
else if(ALLTIME[0]>0||ALLTIME[1]>0||ALLTIME[2]==0||ALLTIME[3]==0||ALLTIME[4]>0)
a=max3(PRIORITY[0],PRIORITY[1],PRIORITY[4]);
else if(ALLTIME[0]>0||ALLTIME[1]>0||ALLTIME[2]==0||ALLTIME[3]>0||ALLTIME[4]==0)
a=max3(PRIORITY[0],PRIORITY[1],PRIORITY[3]);
else if(ALLTIME[0]>0||ALLTIME[1]>0||ALLTIME[2]>0||ALLTIME[3]==0||ALLTIME[4]==0)
a=max3(PRIORITY[0],PRIORITY[1],PRIORITY[2]);
//====================================================================
else if(ALLTIME[0]==0||ALLTIME[1]==0||ALLTIME[2]==0||ALLTIME[3]>0||ALLTIME[4]>0)
a=max2(PRIORITY[3],PRIORITY[4]);
else if(ALLTIME[0]==0||ALLTIME[1]==0||ALLTIME[2]>0||ALLTIME[3]==0||ALLTIME[4]>0)
a=max2(PRIORITY[2],PRIORITY[4]);
else if(ALLTIME[0]==0||ALLTIME[1]==0||ALLTIME[2]>0||ALLTIME[3]>0||ALLTIME[4]==0)
a=max2(PRIORITY[2],PRIORITY[3]);
else if(ALLTIME[0]==0||ALLTIME[1]>0||ALLTIME[2]==0||ALLTIME[3]==0||ALLTIME[4]>0)
a=max2(PRIORITY[1],PRIORITY[4]);
else if(ALLTIME[0]==0||ALLTIME[1]>0||ALLTIME[2]==0||ALLTIME[3]>0||ALLTIME[4]==0)
a=max2(PRIORITY[1],PRIORITY[3]);
else if(ALLTIME[0]>0||ALLTIME[1]==0||ALLTIME[2]==0||ALLTIME[3]==0||ALLTIME[4]>0)
a=max2(PRIORITY[0],PRIORITY[4]);
else if(ALLTIME[0]>0||ALLTIME[1]==0||ALLTIME[2]==0||ALLTIME[3]>0||ALLTIME[4]==0)
a=max2(PRIORITY[0],PRIORITY[3]);
else if(ALLTIME[0]==0||ALLTIME[1]>0||ALLTIME[2]>0||ALLTIME[3]==0||ALLTIME[4]==0)
a=max2(PRIORITY[1],PRIORITY[2]);
else if(ALLTIME[0]>0||ALLTIME[1]==0||ALLTIME[2]>0||ALLTIME[3]==0||ALLTIME[4]==0)
a=max2(PRIORITY[0],PRIORITY[2]);
else if(ALLTIME[0]>0||ALLTIME[1]>0||ALLTIME[2]==0||ALLTIME[3]==0||ALLTIME[4]==0)
a=max2(PRIORITY[0],PRIORITY[1]);
//====================================================================
else if(ALLTIME[0]>0||ALLTIME[1]==0||ALLTIME[2]==0||ALLTIME[3]==0||ALLTIME[4]==0)
a=PRIORITY[0];
else if(ALLTIME[0]==0||ALLTIME[1]>0||ALLTIME[2]==0||ALLTIME[3]==0||ALLTIME[4]==0)
a=PRIORITY[1];
else if(ALLTIME[0]==0||ALLTIME[1]==0||ALLTIME[2]>0||ALLTIME[3]==0||ALLTIME[4]==0)
a=PRIORITY[2];
else if(ALLTIME[0]==0||ALLTIME[1]==0||ALLTIME[2]==0||ALLTIME[3]>0||ALLTIME[4]==0)
a=PRIORITY[3];
else if(ALLTIME[0]==0||ALLTIME[1]==0||ALLTIME[2]==0||ALLTIME[3]==0||ALLTIME[4]>0)
a=PRIORITY[4];

//===================================================================
if(a==PRIORITY[0])
{
i=0;
PRIORITY[0]-=3;
PRIORITY[3]+=1;PRIORITY[1]+=1;PRIORITY[2]+=1;PRIORITY[4]+=1;
CPUTIME[0]+=1;
ALLTIME[0]-=1;
STARTBLOCK[0]-=1;
STARTBLOCK[3]-=1;STARTBLOCK[1]-=1;STARTBLOCK[2]-=1;STARTBLOCK[4]-=1;
BLOCKTIME[3]-=1;BLOCKTIME[1]-=1;BLOCKTIME[2]-=1;BLOCKTIME[4]-=1;
}
else if(a==PRIORITY[1])
{
i=1;
PRIORITY[1]-=3;
PRIORITY[0]+=1;PRIORITY[2]+=1;PRIORITY[3]+=1;PRIORITY[4]+=1;
CPUTIME[1]+=1;
ALLTIME[1]-=1;
STARTBLOCK[1]-=1;
STARTBLOCK[0]-=1;STARTBLOCK[3]-=1;STARTBLOCK[2]-=1;STARTBLOCK[4]-=1;
BLOCKTIME[0]-=1;BLOCKTIME[3]-=1;BLOCKTIME[2]-=1;BLOCKTIME[4]-=1;
}
else if(a==PRIORITY[2])
{
i=2;
PRIORITY[2]-=3;
PRIORITY[0]+=1;PRIORITY[1]+=1;PRIORITY[3]+=1;PRIORITY[4]+=1;
CPUTIME[2]+=1;
ALLTIME[2]-=1;
STARTBLOCK[2]-=1;
STARTBLOCK[0]-=1;STARTBLOCK[1]-=1;STARTBLOCK[3]-=1;STARTBLOCK[4]-=1;
BLOCKTIME[0]-=1;BLOCKTIME[1]-=1;BLOCKTIME[3]-=1;BLOCKTIME[4]-=1;
}
else if(a==PRIORITY[3])
{
i=3;
PRIORITY[3]-=3;
PRIORITY[0]+=1;PRIORITY[1]+=1;PRIORITY[2]+=1;PRIORITY[4]+=1;
CPUTIME[3]+=1;
ALLTIME[3]-=1;
STARTBLOCK[3]-=1;
STARTBLOCK[0]-=1;STARTBLOCK[1]-=1;STARTBLOCK[2]-=1;STARTBLOCK[4]-=1;
BLOCKTIME[0]-=1;BLOCKTIME[1]-=1;BLOCKTIME[2]-=1;BLOCKTIME[4]-=1;
}
else if(a==PRIORITY[4])
{
i=4;
PRIORITY[4]-=3;
PRIORITY[0]+=1;PRIORITY[1]+=1;PRIORITY[2]+=1;PRIORITY[3]+=1;
CPUTIME[4]+=1;
ALLTIME[4]-=1;
STARTBLOCK[4]-=1;
STARTBLOCK[0]-=1;STARTBLOCK[1]-=1;STARTBLOCK[2]-=1;STARTBLOCK[3]-=1;
BLOCKTIME[0]-=1;BLOCKTIME[1]-=1;BLOCKTIME[2]-=1;BLOCKTIME[3]-=1;
}
if(BLOCKTIME[0]==-1)
BLOCKTIME[0]=0;
if(BLOCKTIME[1]==-1)
BLOCKTIME[1]=0;
if(BLOCKTIME[2]==-1)
BLOCKTIME[2]=0;
if(BLOCKTIME[3]==-1)
BLOCKTIME[3]=0;
if(BLOCKTIME[4]==-1)
BLOCKTIME[4]=0;
if(BLOCKTIME[0]>0)
str[0]=0;
else
if(BLOCKTIME[0]<=0)
str[0]=1;
else
if(ALLTIME[0]<=0)
str[0]=-1;
if(BLOCKTIME[1]>0)
str[1]=0;
else
if(BLOCKTIME[1]<=0)
str[1]=1;
else
if(ALLTIME[1]<=0)
str[1]=-1;
if(BLOCKTIME[2]>0)
str[2]=0;
else
if(BLOCKTIME[2]<=0)
str[2]=1;
else
if(ALLTIME[2]<=0)
str[2]=-1;
if(BLOCKTIME[3]>0)
str[3]=0;
else
if(BLOCKTIME[3]<=0)
str[3]=1;
else
if(ALLTIME[3]<=0)
str[3]=-1;
if(BLOCKTIME[4]>0)
str[4]=0;
else
if(BLOCKTIME[4]<=0)
str[4]=1;
else
if(ALLTIME[4]<=0)
str[4]=-1;
//====================================================================
if(ALLTIME[0]<=0)
{
PRIORITY[0]=0;
ALLTIME[0]=0;
STARTBLOCK[0]=-1;
BLOCKTIME[0]=0;
}
if(ALLTIME[1]<=0)
{
PRIORITY[1]=0;
ALLTIME[1]=0;
STARTBLOCK[1]=-1;
BLOCKTIME[1]=0;
}
if(ALLTIME[2]<=0)
{
PRIORITY[2]=0;
ALLTIME[2]=0;
STARTBLOCK[2]=-1;
BLOCKTIME[2]=0;
}
if(ALLTIME[3]<=0)
{
PRIORITY[3]=0;
ALLTIME[3]=0;
STARTBLOCK[3]=-1;
BLOCKTIME[3]=0;
}
if(ALLTIME[4]<=0)
{
PRIORITY[4]=0;
ALLTIME[4]=0;
STARTBLOCK[4]=-1;
BLOCKTIME[4]=0;
}
//====================================================================
if(STARTBLOCK[0]==-2)
STARTBLOCK[0]=-1;
if(STARTBLOCK[1]==-2)
STARTBLOCK[1]=-1;
if(STARTBLOCK[2]==-2)
STARTBLOCK[2]=-1;
if(STARTBLOCK[3]==-2)
STARTBLOCK[3]=-1;
if(STARTBLOCK[4]==-2)
STARTBLOCK[4]=-1;
//===================================================================

//===================================================================
printf("RUNNING PROG: %d/n",i);
//printf("READY_QUENE:->id%d->id%d->id%d->id%d->id%d/n",b0,b1,b2,b3,b4);
//printf("BLOCK_QUENE:->id%d->id%d->id%d->id%d->id%d/n",c0,c1,c2,c3,c4);
printf("====================================================/n");
printf("ID/t/t%d/t%d/t%d/t%d/t%d/n",ID[0],ID[1],ID[2],ID[3],ID[4]);
printf("PRIORITY/t%d/t%d/t%d/t%d/t%d/n",PRIORITY[0],PRIORITY[1],PRIORITY[2],PRIORITY[3],PRIORITY[4]);
printf("CPUTIME/t/t%d/t%d/t%d/t%d/t%d/n",CPUTIME[0],CPUTIME[1],CPUTIME[2],CPUTIME[3],CPUTIME[4]);
printf("ALLTIME/t/t%d/t%d/t%d/t%d/t%d/n",ALLTIME[0],ALLTIME[1],ALLTIME[2],ALLTIME[3],ALLTIME[4]);
printf("STARTBLOCK/t%d/t%d/t%d/t%d/t%d/n",STARTBLOCK[0],STARTBLOCK[1],STARTBLOCK[2],STARTBLOCK[3],STARTBLOCK[4]);
printf("BLOCKTIME/t%d/t%d/t%d/t%d/t%d/n",BLOCKTIME[0],BLOCKTIME[1],BLOCKTIME[2],BLOCKTIME[3],BLOCKTIME[4]);
printf("STATE/t/t%d/t%d/t%d/t%d/t%d/n",str[0],str[1],str[2],str[3],str[4]);
printf("/t/t1=READY,0=BLOCK,-1=END/n/n/n/n");
}
}
else
printf("/nAll of the PROGS has been finished./n");
}
//===================================================================
int max5(int PRIORITY50,int PRIORITY51,int PRIORITY52,int PRIORITY53,int PRIORITY54)
{
int PRIORITY_max;
if(PRIORITY50>PRIORITY51)
PRIORITY_max=PRIORITY50;
else PRIORITY_max=PRIORITY51;
if(PRIORITY52>PRIORITY_max)
PRIORITY_max=PRIORITY52;
else
if(PRIORITY53>PRIORITY_max)
PRIORITY_max=PRIORITY53;
else
if(PRIORITY54>PRIORITY_max)
PRIORITY_max=PRIORITY54;
else
PRIORITY_max=PRIORITY_max;
return(PRIORITY_max);
}
int max4(int PRIORITY40,int PRIORITY41,int PRIORITY42,int PRIORITY43)
{
int PRIORITY_max;
if(PRIORITY40>PRIORITY41)
PRIORITY_max=PRIORITY40;
else PRIORITY_max=PRIORITY41;
if(PRIORITY42>PRIORITY_max)
PRIORITY_max=PRIORITY42;
else
if(PRIORITY43>PRIORITY_max)
PRIORITY_max=PRIORITY43;
else PRIORITY_max=PRIORITY_max;
return(PRIORITY_max);
}
int max3(int PRIORITY30,int PRIORITY31,int PRIORITY32)
{
int PRIORITY_max;
if(PRIORITY30>PRIORITY31)
PRIORITY_max=PRIORITY30;
else PRIORITY_max=PRIORITY31;
if(PRIORITY32>PRIORITY_max)
PRIORITY_max=PRIORITY32;
else PRIORITY_max=PRIORITY_max;
return(PRIORITY_max);
}
int max2(int PRIORITY20,int PRIORITY21)
{
int PRIORITY_max;
if(PRIORITY20>PRIORITY21)
PRIORITY_max=PRIORITY20;
else PRIORITY_max=PRIORITY21;
return(PRIORITY_max);
}//(使用VC6.0编译)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值