实验3 高(动态)优先权优先的进程调度算法模拟
时间:2025-04-09
时间:2025-04-09
操作系统实验,
实验3 高(动态)优先权优先的进程调度算法模拟
1.实验目的
通过动态优先权算法的模拟加深对进程概念和进程调度过程的理解。
2.实验环境
装有操作系统Windows XP和开发工具VC++6.0,内存在256M以上的微机;
或者:装有Linux(Fedora 7)操作系统和gcc编译器,内存在256M以上的微机。
3.实验内容
(1)用C语言来实现对N个进程采用动态优先权优先算法的进程调度。
(2)每个用来标识进程的进程控制块PCB用结构来描述,包括以下字段:
●进程标识数ID;
●进程优先数PRIORITY,并规定优先数越大的进程,其优先权越高;
●进程已占用的CPU时间CPUTIME;
●进程还需占用的CPU时间NEEDTIME。当进程运行完毕时,NEEDTIME变
为0;
●进程的阻塞时间STARTBLOCK,表示当进程再运行STARTBLOCK个时间片
后,进程将进入阻塞状态;
●进程被阻塞的时间BLOCKTIME,表示已阻塞的进程再等待BLOCKTIME个
时间片后,进程将转换成就绪状态;
●进程状态STA TE;(READY, RUNNING, BLOCK, FINISH)
●队列指针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
NEEDTIME 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 PROCESS: $id0
READY QUEUE: $id1->$id2
BLOCK QUEUE: $id3->$id4
FINISH QUEUE: $id0->$id1->$id2->$id3->$id4
================================================================ ==
ID PRIORITY CPUTIME NEEDTIME STA TE STARTBLOCK
操作系统实验,
BLOCKTIME
0 XX XX XX XX XX XX
1 XX XX XX XX XX XX
2 XX XX XX XX XX XX
3 XX XX XX XX XX XX
4 XX XX XX XX XX XX
=============================================================== =====
4.实验要求
(1)将源程序(priority.c)和程序运行结果写入实验报告。
(2)将该算法执行过程与高响应比优先调度算法的执行过程进行比较。
5.实验结果
程序代码:
#include<stdio.h>
#define N 5
void init();
void print();
int getRunning();
void sort();
int run(int time);
enum STATE{Ready,Running,Block,RunOut};
struct Process{
int id;
int priority;
int cputime;
int alltime;
int startblock;
int blocktime;
enum STATE State;
}process[N];
int READY[N];
int BLOCK[N];
int RUNOUT[N][2];
int main(){
int time=0;
init();
printf("time=%d\n",time);
sort();
print();
while(1){
time++;
getchar();
printf("time=%d\n",time);
if(run(time))
break;
//sort();
}
//printf("time");
return 0;
}
void init(){///数据不同初始化数据
int i;
//printf("inpt") ;
for(i=0;i<N;++i){
READY[i]=-1;
BLOCK[i]=-1;
RUNOUT[i][0]=-1;
RUNOUT[i][0]=-1;
process[i].id=i;
process[i].cputime=0;
process[i].State=Ready;
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;
}
void print(){
int i;
if(getRunning()>=0)
printf("\tRunning prog:%d\n",getRunning());
printf("\tREADY Queue:");
for(i=0;i<N;++i){
if(READY[i]>=0)
printf("->%d",process[READY[i]].id);
else break;
}
printf("\n\tBLOCK Queue:");
for(i=0;i<N;++i){
if(BLOCK[i]>=0)
printf("->%d",process[BLOCK[i]].id);
else break;
}
printf("\n========================================================================\n");
printf("id\t"); //制表
for(i=0;i<N;++i)
printf("\t%d",process[i].id);
printf("\npriority");
for(i=0;i<N;++i)
printf("\t%d",process[i].priority);
printf("\ncputime\t");
for(i=0;i<N;++i)
printf("\t%d",process[i].cputime);
printf("\nalltime\t");
for(i=0;i<N;++i)
printf("\t%d",process[i].alltime);
printf("\nstartblock");
for(i=0;i<N;++i)
printf("\t%d",process[i].startblock);
printf("\nblocktime");
for(i=0;i<N;i++)
printf("\t%d",process[i].blocktime);
printf("\nState\t");
for(i=0;i<N;++i){
switch(process[i].State){
case 0:printf("\tREADY");break;
case 1:printf("\tRUN");
if(process[i].alltime==0){
process[i].State=RunOut;
}
else process[i].State=Ready;break;
case 2:printf("\tBLOCK");break;
case 3:printf("\tRunOut");break;
}
}
printf("\n");
printf("\tRunOut List:");
for(i=0;i<N;++i){
上一篇:PEP小学英语词性转换练习
下一篇:电大金融学学习手册单选题答案