使用动态优先权的进程调度算法的模拟.docx
- 文档编号:24288469
- 上传时间:2023-05-26
- 格式:DOCX
- 页数:35
- 大小:53.33KB
使用动态优先权的进程调度算法的模拟.docx
《使用动态优先权的进程调度算法的模拟.docx》由会员分享,可在线阅读,更多相关《使用动态优先权的进程调度算法的模拟.docx(35页珍藏版)》请在冰豆网上搜索。
使用动态优先权的进程调度算法的模拟
使用动态优先权的进程调度算法的模拟
(1)用C语言实现对5个进程采用动态优先权优先算法的进程调度。
(2)每个用来标识进程的进程控制块PCB用结构来描述,包括以下字段:
·进程标识树ID
·进程优先数PRIORITY,并规定优先数越大的进程,其优先全越高。
·进程已占用的CPU时间CPUTIME。
·进程还需占用的CPU时间ALLTIME。
当进程运行完毕时,ALLTIME变为0
·进程的阻塞时间STARTBLOCK,表示当进程再运行STARTBLOCK个时间片后,进程将进入阻塞状态
·进程被阻塞的时间BLOCKTIME,表示已阻塞的进程再等待BLOCKTIME个时间片后,将转换成就绪状态
·进程状态STATE
(3)优先数改变的原则:
·进程在就绪队列中待一个时间片,优先数增加1
·进程每运行一个时间片,优先数减3
(4)假设在调度前,系统中有5个进程,它们的初始状态如下:
ID01234
PRIORITY93830290
CPUTIME00000
ALLTIME33634
STARTBLOCK2-1-1-1-1
BLOCKTIME30000
STATEREADYREADYREADYREADYREADY
(5)为了清楚的观察诸进度的调度过程,程序应将每个时间片内的进程的情况显示出来,具体格式如下:
RUNNINGPROG:
i
========================================================================
ID01234
PRIORITYP0P1P2P3P4
CPUTIMEC0C1C2C3C4
ALLTIMEA0A1A2A3A4
STARTBLOCKT0T1T2T3T4
BLOCKTIMEB0B1B2B3B4
STATEREADYREADYREADYREADYREADY
代码如下:
#include
#include
voidmain()
{
intID[5]={0,1,2,3,4};
intPRIORITY[5]={9,38,30,29,0};
intCPUTIME[5]={0,0,0,0,0};
intALLTIME[5]={3,3,6,3,4};
intSTARTBLOCK[5]={2,-1,-1,-1,-1};
intBLOCKTIME[5]={3,0,0,0,0};
intstr[5];//1=READY,0=BLOCK,-1=END
intmax5(intPRIORITY50,intPRIORITY51,intPRIORITY52,intPRIORITY53,intPRIORITY54);
intmax4(intPRIORITY40,intPRIORITY41,intPRIORITY42,intPRIORITY43);
intmax3(intPRIORITY30,intPRIORITY31,intPRIORITY32);
intmax2(intPRIORITY20,intPRIORITY21);
inti=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]);
elseif(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]);
elseif(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]);
elseif(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]);
elseif(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]);
elseif(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]);
//===================================================================
elseif(ALLTIME[0]==0||ALLTIME[1]==0||ALLTIME[2]>0||ALLTIME[3]>0||ALLTIME[4]>0)
a=max3(PRIORITY[2],PRIORITY[3],PRIORITY[4]);
elseif(ALLTIME[0]==0||ALLTIME[1]>0||ALLTIME[2]==0||ALLTIME[3]>0||ALLTIME[4]>0)
a=max3(PRIORITY[1],PRIORITY[3],PRIORITY[4]);
elseif(ALLTIME[0]==0||ALLTIME[1]>0||ALLTIME[2]>0||ALLTIME[3]==0||ALLTIME[4]>0)
a=max3(PRIORITY[1],PRIORITY[2],PRIORITY[4]);
elseif(ALLTIME[0]==0||ALLTIME[1]>0||ALLTIME[2]>0||ALLTIME[3]>0||ALLTIME[4]==0)
a=max3(PRIORITY[1],PRIORITY[2],PRIORITY[3]);
elseif(ALLTIME[0]>0||ALLTIME[1]==0||ALLTIME[2]==0||ALLTIME[3]>0||ALLTIME[4]>0)
a=max3(PRIORITY[0],PRIORITY[3],PRIORITY[4]);
elseif(ALLTIME[0]>0||ALLTIME[1]==0||ALLTIME[2]>0||ALLTIME[3]==0||ALLTIME[4]>0)
a=max3(PRIORITY[0],PRIORITY[2],PRIORITY[4]);
elseif(ALLTIME[0]>0||ALLTIME[1]==0||ALLTIME[2]>0||ALLTIME[3]>0||ALLTIME[4]==0)
a=max3(PRIORITY[0],PRIORITY[2],PRIORITY[3]);
elseif(ALLTIME[0]>0||ALLTIME[1]>0||ALLTIME[2]==0||ALLTIME[3]==0||ALLTIME[4]>0)
a=max3(PRIORITY[0],PRIORITY[1],PRIORITY[4]);
elseif(ALLTIME[0]>0||ALLTIME[1]>0||ALLTIME[2]==0||ALLTIME[3]>0||ALLTIME[4]==0)
a=max3(PRIORITY[0],PRIORITY[1],PRIORITY[3]);
elseif(ALLTIME[0]>0||ALLTIME[1]>0||ALLTIME[2]>0||ALLTIME[3]==0||ALLTIME[4]==0)
a=max3(PRIORITY[0],PRIORITY[1],PRIORITY[2]);
//====================================================================
elseif(ALLTIME[0]==0||ALLTIME[1]==0||ALLTIME[2]==0||ALLTIME[3]>0||ALLTIME[4]>0)
a=max2(PRIORITY[3],PRIORITY[4]);
elseif(ALLTIME[0]==0||ALLTIME[1]==0||ALLTIME[2]>0||ALLTIME[3]==0||ALLTIME[4]>0)
a=max2(PRIORITY[2],PRIORITY[4]);
elseif(ALLTIME[0]==0||ALLTIME[1]==0||ALLTIME[2]>0||ALLTIME[3]>0||ALLTIME[4]==0)
a=max2(PRIORITY[2],PRIORITY[3]);
elseif(ALLTIME[0]==0||ALLTIME[1]>0||ALLTIME[2]==0||ALLTIME[3]==0||ALLTIME[4]>0)
a=max2(PRIORITY[1],PRIORITY[4]);
elseif(ALLTIME[0]==0||ALLTIME[1]>0||ALLTIME[2]==0||ALLTIME[3]>0||ALLTIME[4]==0)
a=max2(PRIORITY[1],PRIORITY[3]);
elseif(ALLTIME[0]>0||ALLTIME[1]==0||ALLTIME[2]==0||ALLTIME[3]==0||ALLTIME[4]>0)
a=max2(PRIORITY[0],PRIORITY[4]);
elseif(ALLTIME[0]>0||ALLTIME[1]==0||ALLTIME[2]==0||ALLTIME[3]>0||ALLTIME[4]==0)
a=max2(PRIORITY[0],PRIORITY[3]);
elseif(ALLTIME[0]==0||ALLTIME[1]>0||ALLTIME[2]>0||ALLTIME[3]==0||ALLTIME[4]==0)
a=max2(PRIORITY[1],PRIORITY[2]);
elseif(ALLTIME[0]>0||ALLTIME[1]==0||ALLTIME[2]>0||ALLTIME[3]==0||ALLTIME[4]==0)
a=max2(PRIORITY[0],PRIORITY[2]);
elseif(ALLTIME[0]>0||ALLTIME[1]>0||ALLTIME[2]==0||ALLTIME[3]==0||ALLTIME[4]==0)
a=max2(PRIORITY[0],PRIORITY[1]);
//====================================================================
elseif(ALLTIME[0]>0||ALLTIME[1]==0||ALLTIME[2]==0||ALLTIME[3]==0||ALLTIME[4]==0)
a=PRIORITY[0];
elseif(ALLTIME[0]==0||ALLTIME[1]>0||ALLTIME[2]==0||ALLTIME[3]==0||ALLTIME[4]==0)
a=PRIORITY[1];
elseif(ALLTIME[0]==0||ALLTIME[1]==0||ALLTIME[2]>0||ALLTIME[3]==0||ALLTIME[4]==0)
a=PRIORITY[2];
elseif(ALLTIME[0]==0||ALLTIME[1]==0||ALLTIME[2]==0||ALLTIME[3]>0||ALLTIME[4]==0)
a=PRIORITY[3];
elseif(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;
}
elseif(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;
}
elseif(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;
}
elseif(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;
}
elseif(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("RUNNINGPROG:
%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("\nAllofthePROGShasbeenfinished.\n");
}
//==========================
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 使用 动态 优先权 进程 调度 算法 模拟
![提示](https://static.bdocx.com/images/bang_tan.gif)