实验五模拟进度调度文档格式.docx
- 文档编号:3481331
- 上传时间:2023-05-01
- 格式:DOCX
- 页数:29
- 大小:1.15MB
实验五模拟进度调度文档格式.docx
《实验五模拟进度调度文档格式.docx》由会员分享,可在线阅读,更多相关《实验五模拟进度调度文档格式.docx(29页珍藏版)》请在冰点文库上搜索。
/*进程进入系统后等待cpu的时间*/
Charstate;
/*进程的状态*/
Structnode*next;
/*链接指针*/
}PCB
设计三个队列:
就绪、完成、运行,其中运行队列中应该只有一个进程。
创建进程,就是用户输入进程的标识符和运行所需时间,系统为其生成一个PCB,存放进程的信息,将新生成的PCB插入就绪队列。
进程任务结束,系统将其PCB插入完成队列,该进程就消亡。
任务1:
动态优先权调度算法模拟:
优先权=1+等待时间/需要服务时间----最高响应比优先
(1)输入一批作业的进程标识和估计运行时间,将其PCB插入就绪队列
(2)当就绪队列非空、CPU空闲时,计算出每个就绪进程的优先权
(3)将优先权最高的进程从就绪队列移入运行队列,状态改为运行(模拟进程调度)
(4)在屏幕上输出当前所有进程的状态和优先权信息(已完成进程不显示)
(5)让运行进程执行足够的服务时间
(6)将运行进程移入完成队列
(7)提示可以输入新的作业,如果有新作业输入,则转向
(1),否则转向
(2)
(8)如果就绪队列为空,则结束
程序框图如下:
程序源代码如下:
#include<
stdio.h>
string.h>
typedefstructnode
charname[10];
//进程标识符
floatprin;
//进程的优先级
intround;
//进程轮转的时间片
intneedtime;
//进程还需要当CPU时间
intwaittime;
//进程进入系统后等待当CPU的时间
charstate;
//进程当状态
structnode*next;
//链接指针
}PCB;
PCB*readyQueue=NULL,*runningQueue=NULL,*finishedQueue=NULL,*finishedQueueTail=NULL,*readyQueueTail=NULL,pcbPool[10];
intpoolSize=0;
voidcreatePro();
//根据进程标识和估计运行时间,将其PCB插入就绪队列
voidreadyQueueNotNull();
//当就绪队列非空、CPU空闲时,计算每个就绪进程当优先权
voidinsertRunningQueue();
//将优先权最高当进程从就绪队列移入运行队列,状态改为运行
voiddisplayQueue();
//将所有非结束进程的状态和优先权信息显示在屏幕
voidrunPro();
//让运行进程执行足够的服务时间
voidaddNewPro();
//提示可以输入新的进程
intmain(intargc,charconst*argv[])
createPro();
while(readyQueue!
=NULL)
{
readyQueueNotNull();
insertRunningQueue();
displayQueue();
runPro();
puts("
"
);
addNewPro();
}
puts("
Bye"
return0;
}
voidcreatePro()
charstop='
y'
;
intneedtime,i=0;
while(stop=='
)
//获取用户输入进程名
Pleaseinputtheprocessname:
"
scanf("
%s"
name);
strcpy(pcbPool[i].name,name);
//获取用户输入进程需要时间
Pleaseinputtheneedoftime:
%d"
&
needtime);
pcbPool[i].needtime=needtime;
pcbPool[i].state='
0'
pcbPool[i].next=NULL;
pcbPool[i].prin=0.0f;
pcbPool[i].waittime=0;
if(i==0)
{
readyQueue=&
pcbPool[0];
readyQueueTail=&
}else{
readyQueueTail->
next=&
pcbPool[i];
}
if(i++>
9)
break;
Doyouwanttoinputmoreprocess(yorn):
stop);
poolSize=i+1;
voidreadyQueueNotNull()
PCB*pcb=readyQueue;
while
(1)
pcb->
prin=1+pcb->
waittime/pcb->
needtime;
if(pcb->
next==NULL)
else
pcb=pcb->
next;
voidinsertRunningQueue()
PCB*currentPCB=readyQueue,*lastPCB=readyQueue,*lastOfChosePCB=readyQueue,*chosePCB=readyQueue;
//选择优先级最高的进程
if(currentPCB->
prin<
currentPCB->
prin)
lastOfChosePCB=lastPCB;
chosePCB=currentPCB;
//判断就绪队列是否判断结束
next!
lastPCB=currentPCB;
currentPCB=currentPCB->
chosePCB->
state='
1'
//将优先级最高的进程状态标识为运行时
runningQueue=chosePCB;
//将优先级最高的进程从就绪队列移入运行队列
//将优先级最高的进程从就绪队列中移出
if(readyQueue==chosePCB)
readyQueue=readyQueue->
else
lastOfChosePCB->
next=chosePCB->
voiddisplayQueue()
if(readyQueue!
PCBpcb=*readyQueue;
while
(1)
printf("
Process:
%sState:
%dprin:
%f\n"
pcb.name,pcb.state,pcb.prin);
if(pcb.next==NULL)
break;
else
pcb=*(pcb.next);
if(runningQueue!
printf("
runningQueue->
name,runningQueue->
state,runningQueue->
prin);
voidrunPro()
sleep(runningQueue->
PCB*pcb=readyQueue;
pcb->
waittime=pcb->
waittime+runningQueue->
if(pcb->
pcb=pcb->
//将运行了需要时间的进程移入完成队列
if(finishedQueue==NULL)
finishedQueue=runningQueue;
finishedQueueTail=runningQueue;
finishedQueueTail->
next=NULL;
}else{
next=runningQueue;
//清理运行时队列
runningQueue->
runningQueue=NULL;
voidaddNewPro()
intneedtime,i=poolSize-1;
scanf("
if(readyQueue==NULL)
puts("
scanf("
程序运行截图如下:
任务2:
时间片轮转调度算法模拟
输入一批作业的进程标识和估计运行时间,由其PCB组成就绪队列。
时间片轮转调度,系统给就绪队列的第一个进程分配一个时间片,大小等于n(个时钟周期),再将它移入运行队列。
注意,时间片的大小要比大多数进程的估计运行时间短。
当时间片结束的时候,要将没有运行完的进程从运行队列移到就绪队列的末尾,再次进行调度。
在每次调度时将所有进程的当前情况显示出来。
//根据进程标识和估计运行时间,将其PCB插入就绪队列
pcbPool[i].round=1;
runningQueue=readyQueue;
readyQueue=readyQueue->
%dNeedtime:
%d\n"
pcb.name,pcb.state,pcb.needtime);
round);
//模拟执行进程
//更新运行队列
needtime=runningQueue->
needtime-runningQueue->
round;
//将运行结束当进程移入完成队列
if(runningQueue->
needtime<
=0)
if(finishedQueue==NULL)
finishedQueue=runningQueue;
finishedQueueTail=runningQueue;
finishedQueueTail->
readyQueue=runningQueue;
readyQueueTail=runningQueue;
readyQueueTail->
FCFS调度算法模拟
按一定的顺序输入一批作业的进程标识和估计运行时间,由其PCB按照进程标识输入的顺序组成就绪队列。
当就绪队列非空、CPU空闲时,将就绪队列的第一个进程从就绪队列移入运行队列,状态改为运行。
以后个步骤类似于2。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 模拟 进度 调度