实验3高动态优先权优先的进程调度算法模拟.docx
《实验3高动态优先权优先的进程调度算法模拟.docx》由会员分享,可在线阅读,更多相关《实验3高动态优先权优先的进程调度算法模拟.docx(13页珍藏版)》请在冰豆网上搜索。
![实验3高动态优先权优先的进程调度算法模拟.docx](https://file1.bdocx.com/fileroot1/2023-1/4/3566735f-47a1-45e8-bbbe-462638b383f0/3566735f-47a1-45e8-bbbe-462638b383f01.gif)
实验3高动态优先权优先的进程调度算法模拟
实验3高(动态)优先权优先的进程调度算法模拟
实验目的
通过动态优先权算法的模拟加深对进程概念和进程调度过程的理解。
实验环境
装有操作系统WindowsXP和开发工具VC++6.0,内存在256M以上的微机;
或者:
装有Linux(Fedora7)操作系统和gcc编译器,内存在256M以上的微机。
实验内容
用C语言来实现对N个进程采用动态优先权优先算法的进程调度。
每个用来标识进程的进程控制块PCB用结构来描述,包括以下字段:
进程标识数ID;
进程优先数PRIORITY,并规定优先数越大的进程,其优先权越高;
进程已占用的CPU时间CPUTIME;
进程还需占用的CPU时间NEEDTIME。
当进程运行完毕时,NEEDTIME变为0;
进程的阻塞时间STARTBLOCK,表示当进程再运行STARTBLOCK个时间片后,进程将进入阻塞状态;
进程被阻塞的时间BLOCKTIME,表示已阻塞的进程再等待BLOCKTIME个时间片后,进程将转换成就绪状态;
进程状态STATE;(READY,RUNNING,BLOCK,FINISH)
队列指针NEXT,用来将PCB排成队列。
优先数改变的原则:
进程在就绪队列中呆一个时间片,优先数增加1;
进程每运行一个时间片,优先数减3。
假设在调度前,系统中有5个进程,它们的初始状态如下:
ID01234
PRIORITY93830290
CPUTIME00000
NEEDTIME33634
STARTBLOCK2-1-1-1-1
BLOCKTIME30000
STATEREADYREADYREADYREADYREADY
为了清楚地观察进程的调度过程,程序应将每个时间片内的进程的情况显示出来,参照的具体格式如下:
RUNNINGPROCESS:
$id0
READYQUEUE:
$id1->$id2
BLOCKQUEUE:
$id3->$id4
FINISHQUEUE:
$id0->$id1->$id2->$id3->$id4
==================================================================
IDPRIORITYCPUTIMENEEDTIMESTATESTARTBLOCKBLOCKTIME
0XXXXXXXXXXXX
1XXXXXXXXXXXX
2XXXXXXXXXXXX
3XXXXXXXXXXXX
4XXXXXXXXXXXX
====================================================================
实验要求
将源程序(priority.c)和程序运行结果写入实验报告。
将该算法执行过程与高响应比优先调度算法的执行过程进行比较。
#include
#include
usingnamespacestd;
inti;//循环值
intj;//还在阻塞或就绪队列中的进程数
ints;
intm;//最大priority的id
structpcb
{
intid;
intp;//priority
intcputime;
intalltime;
intstartblock;
intblocktime;
intstate;//0表示ready1表示end-1表示block
};
structpcbpro[5]={
{0,9,0,3,2,3,0},
{1,38,0,3,-1,0,0},
{2,30,0,6,-1,0,0},
{3,29,0,3,-1,0,0},
{4,0,0,4,-1,0,0}
};
intchangestate0()
{
if(pro[0].startblock==0)
{
pro[0].state=-1;
pro[0].startblock--;
return1;
}
if(pro[0].blocktime==0)
{
pro[0].state=0;
return1;
}
if(pro[0].state==0&&pro[0].startblock!
=-1)
{
pro[0].startblock--;return1;}
if(pro[0].state==-1&&pro[0].blocktime!
=0)
{
pro[0].blocktime--;return1;
}
}
intstate0()
{
changestate0();
s=pro[0].p;
if(pro[0].state==-1)
s=-100;
returns;
}
intmaxp()//求出最大priority
{
state0();
intmax=s;
m=pro[0].id;
for(i=0;i{
if(pro[i+1].p>pro[i].p)
{
max=pro[i+1].p;
m=pro[i+1].id;
}
}
returnm;
}
voidchange()
{
maxp();
intx;//得到m现在的数组编号
for(i=0;i{
pro[i].p++;
}
for(i=0;i{
if(pro[i].id==m)
x=i;
}
pro[x].cputime++;
pro[x].p=pro[x].p-4;
pro[x].alltime--;
if(pro[x].alltime==0)
{
pro[x].state=1;
}
}
voiddisplay()
{
change();
cout<<"RUNNINGPROG:
"<cout<<"===============================================================\n";
cout<<"ID";
for(i=0;i{
cout.width(10);
cout<}
cout<for(i=0;i{
cout.width(10);
cout<}
cout<for(i=0;i{
cout.width(10);
cout<}
cout<for(i=0;i{
cout.width(10);
cout<}
cout<for(i=0;i{
cout.width(10);
cout<}
cout<for(i=0;i{
cout.width(10);
cout<}
cout<for(i=0;i{
cout.width(10);
cout<}
cout<}
intmain()
{
j=5;//刚开始有5个进程
while(j!
=0)
{
for(i=0;i{
if(pro[i].state==1){
for(;i{
pro[i]=pro[i+1];}
j=j-1;
}
}
display();
getchar();
}
}
运行结果:
RUNNINGPROG:
1
===============================================================
ID 0 1 2 3 4
PRIORITY 10 35 31 30 1
CPUTIME 0 1 0 0 0
ALLTIME 3 2 6 3 4
STARTBLOCK 1 -1 -1 -1 -1
BLOCKTIME 3 0 0 0 0
STATE 0 0 0 0 0
RUNNINGPROG:
1
===============================================================
ID 0 1 2 3 4
PRIORITY 11 32 32 31 2
CPUTIME 0 2 0 0 0
ALLTIME 3 1 6 3 4
STARTBLOCK 0 -1 -1 -1 -1
BLOCKTIME 3 0 0 0 0
STATE 0 0 0 0 0
RUNNINGPROG:
1
===============================================================
ID 0 1 2 3 4
PRIORITY 12 29 33 32 3
CPUTIME 0 3 0 0 0
ALLTIME 3 0 6 3 4
STARTBLOCK -1 -1 -1 -1 -1
BLOCKTIME 3 0 0 0 0
STATE -1 1 0 0 0
RUNNINGPROG:
2
===============================================================
ID 0 2 3 4
PRIORITY 13 30 33 4
CPUTIME 0 1 0 0
ALLTIME 3 5 3 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 2 0 0 0
STATE -1 0 0 0
RUNNINGPROG:
3
===============================================================
ID 0 2 3 4
PRIORITY 14 31 30 5
CPUTIME 0 1 1 0
ALLTIME 3 5 2 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 1 0 0 0
STATE -1 0 0 0
RUNNINGPROG:
2
===============================================================
ID 0 2 3 4
PRIORITY 15 28 31 6
CPUTIME 0 2 1 0
ALLTIME 3 4 2 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 0 0 0 0
STATE -1 0 0 0
RUNNINGPROG:
3
===============================================================
ID 0 2 3 4
PRIORITY 16 29 28 7
CPUTIME 0 2 2 0
ALLTIME 3 4 1 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 0 0 0 0
STATE 0 0 0 0
RUNNINGPROG:
2
===============================================================
ID 0 2 3 4
PRIORITY 17 26 29 8
CPUTIME 0 3 2 0
ALLTIME 3 3 1 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 0 0 0 0
STATE 0 0 0 0
RUNNINGPROG:
3
===============================================================
ID 0 2 3 4
PRIORITY 18 27 26 9
CPUTIME 0 3 3 0
ALLTIME 3 3 0 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 0 0 0 0
STATE 0 0 1 0
RUNNINGPROG:
2
===============================================================
ID 0 2 4
PRIORITY 19 24 10
CPUTIME 0 4 0
ALLTIME 3 2 4
STARTBLOCK -1 -1 -1
BLOCKTIME 0 0 0
STATE 0 0 0
RUNNINGPROG:
2
===============================================================
ID 0 2 4
PRIORITY 20 21 11
CPUTIME 0 5 0
ALLTIME 3 1 4
STARTBLOCK -1 -1 -1
BLOCKTIME 0 0 0
STATE 0 0 0
RUNNINGPROG:
2
===============================================================
ID 0 2 4
PRIORITY 21 18 12
CPUTIME 0 6 0
ALLTIME 3 0 4
STARTBLOCK -1 -1 -1
BLOCKTIME 0 0 0
STATE 0 1 0
RUNNINGPROG:
0
===============================================================
ID 0 4
PRIORITY 18 13
CPUTIME 1 0
ALLTIME 2 4
STARTBLOCK -1 -1
BLOCKTIME 0 0
STATE 0 0
RUNNINGPROG:
0
===============================================================
ID 0 4
PRIORITY 15 14
CPUTIME 2 0
ALLTIME 1 4
STARTBLOCK -1 -1
BLOCKTIME 0 0
STATE 0 0
RUNNINGPROG:
0
===============================================================
ID 0 4
PRIORITY 12 15
CPUTIME 3 0
ALLTIME 0 4
STARTBLOCK -1 -1
BLOCKTIME 0 0
STATE 1 0
RUNNINGPROG:
4
===============================================================
ID 4
PRIORITY 12
CPUTIME 1
ALLTIME 3
STARTBLOCK -1
BLOCKTIME