使用动态优先权的进程调度算法的模拟.docx
《使用动态优先权的进程调度算法的模拟.docx》由会员分享,可在线阅读,更多相关《使用动态优先权的进程调度算法的模拟.docx(18页珍藏版)》请在冰豆网上搜索。
使用动态优先权的进程调度算法的模拟
实验四使用动态优先权的进程调度算法的模拟
1、实验目的
通过动态优先权算法的模拟加深对进程概念和进程调度过程的理解。
2、实验内容
(1)用C语言来实现对N个进程采用动态优先算法的进程调度;
(2)每个用来标识进程的进程控制块PCB用结构来描述,包括以下字段:
●进程标识符id
●进程优先数priority,并规定优先数越大的进程,其优先权越高;
●进程已占用的CPU时间cputime;
●进程还需占用的CPU时间alltime,当进程运行完毕时,alltime变为0;
●进程的阻塞时间startblock,表示当进程再运行startblock个时间片后,进程将进入阻塞状态;
●进程被阻塞的时间blocktime,表示已阻塞的进程再等待blocktime个时间片后,将转换成就绪态
●进程状态state;
●队列指针next,用来将PCB排成队列
(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)为了清楚地观察诸进程的调度过程,程序应将每个时间片内的进程的情况显示出来,参照的具体格式如下:
RUNNINGPROG:
i
READY_QUEUE:
->id1->id2
BLOCK_QUEUE:
->id3->id4
==================================
ID01234
PRIORITYP0P1P2P3P4
CPUTIMEC0C1C2C3C4
ALLTIMEA0A1A2A3A4
STARTBLOCKT0T1T2T3T4
BLOCKTIMEB0B1B2B3B4
STATES0S1S2S3S4
程序:
2、程序示例:
//A.4使用动态优先权的进程调度算法的模拟
//byYanQiwei
#include
#defineN5
voidinit();
voidprint();
intgetRunning();
voidsort();
intrun(inttime);
enumSTATE{Ready,Run,Block,RunOut};
structPROCESS{
intID;
intPriority;
intCputime;
intAlltime;
intStartblock;
intBlocktime;
enumSTATEState;
}Process[N];
intREADY[N];
intBLOCK[N];
intRUNOUT[N][2];
intmain(){
intTime=0;
init();
printf("Time:
%d\n",Time);
sort();
print();
while
(1){
Time++;
getchar();
printf("Time:
%d\n",Time);
if(run(Time))
break;
//sort();
}
//print(Time);
return0;
}
voidinit()
{
inti;
//printf("Inputpropertiesof%dprocess(PRIORITY,ALLTIME,STARTBLOCK,BLOCKTIME):
\n",
//N);
for(i=0;iREADY[i]=-1;
BLOCK[i]=-1;
RUNOUT[i][0]=-1;
RUNOUT[i][1]=-1;
Process[i].ID=i;
Process[i].Cputime=0;
Process[i].State=Ready;
/*printf("Number%d:
",i);
scanf("%d,%d,%d,%d",&Process[i].Priority,&Process[i].Alltime,
&Process[i].Startblock,&Process[i].Blocktime);
*/
Process[i].Startblock=-1;Process[i].Blocktime=0;
}
Process[0].Priority=9;Process[0].Alltime=3;Process[0].Startblock=2;Process[0].Blocktime=3;
Process[1].Priority=38;Process[1].Alltime=3;
Process[2].Priority=30;Process[2].Alltime=6;
Process[3].Priority=29;Process[3].Alltime=3;
Process[4].Priority=0;Process[4].Alltime=4;
}
voidprint()
{
inti;
if(getRunning()>=0)
printf("\tRUNNINGPROG:
%d\n",getRunning());
printf("\tREADY_QUEUE:
");
for(i=0;iif(READY[i]>=0)
printf("->%d",Process[READY[i]].ID);
else{
break;
}
}
printf("\n\tBLOCK_QUEUE:
");
for(i=0;iif(BLOCK[i]>=0)
printf("->%d",Process[BLOCK[i]].ID);
else{
break;
}
}
printf("\n=====================================================\n");
printf("ID\t");
for(i=0;iprintf("\t%d",Process[i].ID);
}
printf("\nPRIORITY");
for(i=0;iprintf("\t%d",Process[i].Priority);
}
printf("\nCPUTIME\t");
for(i=0;iprintf("\t%d",Process[i].Cputime);
}
printf("\nALLTIME\t");
for(i=0;iprintf("\t%d",Process[i].Alltime);
}
printf("\nSTARTBLOCK");
for(i=0;iprintf("\t%d",Process[i].Startblock);
}
printf("\nBLOCKTIME");
for(i=0;iprintf("\t%d",Process[i].Blocktime);
}
printf("\nSTATE\t");
for(i=0;iswitch(Process[i].State){
case0:
printf("\tReady");break;
case1:
printf("\tRun");
if(Process[i].Alltime==0)
{
Process[i].State=RunOut;
}
elseProcess[i].State=Ready;
break;
case2:
printf("\tBlock");break;
case3:
printf("\tRunOut");break;
}
}
printf("\n");
printf("\tRUNOUTLIST:
");
for(i=0;iif(RUNOUT[i][0]>=0)
printf("->%d(%d)",Process[RUNOUT[i][0]].ID,RUNOUT[i][1]);
else{
printf("\n");
break;
}
}
printf("\n");
}
intgetRunning()
{
inti;
for(i=0;iif(Process[i].State==Run)
returni;
}
for(i=0;iif(Process[i].Startblock==0)
returni;
}
return-1;
}
voidsort()
{
inti,j,k;
for(i=0;iREADY[i]=-1;
BLOCK[i]=-1;
}
for(i=0;iif(Process[i].State==Ready||Process[i].State==Run){
//Process[i].State=Ready;
if(Process[i].Alltime==0)
continue;
for(j=0;jif(READY[j]<0)
{
READY[j]=i;
break;
}
elseif(Process[i].Priority<=Process[READY[j]].Priority)
{continue;}
else{
for(k=N-1;k>j;--k){
READY[k]=READY[k-1];
}
READY[j]=i;
break;
}
}
}
elseif(Process[i].State==Block){
for(j=0;jif(BLOCK[j]<0)
{
BLOCK[j]=i;
break;
}
elseif(Process[i].Blocktime>=Process[BLOCK[j]].Blocktime)
{continue;}
else{
for(k=N-1;k>j;--k){
BLOCK[k]=BLOCK[k-1];
}
BLOCK[j]=i;
break;
}
}
}
}
}
intrun(inttime)
{
inti,runNum;
runNum=READY[0];
if(runNum<0&&BLOCK[0]<0){
printf("EveryprocessisOVER!
\n");
return1;
}
else{
if(runNum>=0){
Process[runNum].Priority-=3;
Process[runNum].Alltime-=1;
Process[runNum].Cputime+=1;
Process[runNum].State=Run;
for(i=0;iif(i!
=runNum){
if(Process[i].State==Ready){
Process[i].Priority+=1;
}
elseif(Process[i].State==Block){
Process[i].Blocktime-=1;
if(Process[i].Blocktime==0){
Process[i].State=Ready;
}
}
}
}
//print();
if(Process[runNum].Alltime==0)
{
//Process[runNum].State=RunOut;
for(i=0;iif(RUNOUT[i][0]<0){
RUNOUT[i][0]=runNum;
RUNOUT[i][1]=time;
break;
}
}
}
elseif(Process[runNum].Startblock>=0){
Process[runNum].Startblock-=1;
if(Process[runNum].Startblock==0){
Process[runNum].State=Block;
//Process[runNum].Startblock-=1;
}
}
}
elseif(BLOCK[0]>=0){
for(i=0;iif(Process[i].State==Block){
Process[i].Startblock=-1;
Process[i].Blocktime-=1;
if(Process[i].Blocktime==0){
Process[i].State=Ready;
}
}
}
//print();
}
}
sort();
print();
return0;
}
3、思考题
(1)在实际的调度中,除了按调度算法选择下一个执行的进程外,还应处理哪些工作?
运行结果;