静态优先权优先算法地进程调度程序Word格式.docx
- 文档编号:17071526
- 上传时间:2022-11-28
- 格式:DOCX
- 页数:20
- 大小:326.84KB
静态优先权优先算法地进程调度程序Word格式.docx
《静态优先权优先算法地进程调度程序Word格式.docx》由会员分享,可在线阅读,更多相关《静态优先权优先算法地进程调度程序Word格式.docx(20页珍藏版)》请在冰豆网上搜索。
打印所有进程到屏幕上。
4)voidMenu():
菜单以及执行。
5)voidnewPCB():
创建进程。
在1)中被调用。
6)voidreadFromFile():
从文件中读取数据。
7)voidreadyList(structPCB*pcb):
就绪列表。
8)voidrelease(structPCB*pcb),voidreleaseR(structPCB*pcb):
释放进程。
9)voidrun():
执行静态优先级进程调度算法。
10)structPCB*runTheProcess():
标识要执行的进程。
11)voidsetNewPCB(char*name,intpri,intentime,intsertime):
在6)中被调用。
12)inttimeOfData(FILE*f):
计算文件中进程数量。
2.3相关数据结构设计
结构体:
structPCB{}*head,*readyHead;
3.详细设计
3.1采用C语言定义的相关数据类型
structPCB
{
intArrivalTime;
intServiceTime;
charnumber[10];
};
structPCB*head,*thisP,*newP;
//动态接收进程及详细
structPCB*readyHead;
//动态存储就绪队列
采用结构体数组,创建一个进程,包含进程相关信息:
进程名称、进程优先级、进程到达时间、进程服务时间。
3.2调度算法的主要实现
structPCB*callMemory()//调入内存,返回调入内存的链表节点。
将达到进程进入内存时间的所有进程调入内存。
voidreadyList(structPCB*pcb)//单链表的形式创建就绪队列。
将进入内存的进程调入就绪列表。
structPCB*runTheProcess()//执行的进程,返回要执行进程的节点。
将就绪列表中满足运行条件的进程标识下来并返回节点。
voidrun()//静态优先权算法。
将执行的程序释放。
调入新的满足执行条件的进程。
就绪列表释放。
4.运行结果
4.1系统调试
在程序设计初期。
本想采用冒泡排序将进入内存的进程进行优先级排序,不过最后总是排序失败。
最后想到我进行的是进程调度模拟,应该就有进程调入内存以及调入CPU,因此之后设计其他函数进行调入内存、CPU的模拟。
而且最后选择新建就绪列表,进行选择插入就绪列表中。
以及随时使用屏幕打印语句printf来测试程序各个阶段执行状态。
4.2功能实现界面
(1)主菜单:
如图4.1所示。
图4.1主菜单
(2)进程调度模拟。
图4.2创建进程
2)
图4.3进程运行过程
3)进程在调度算法中,计算出的具体的完成时间,周转时间,带权时间。
如图4.4所示。
图4.4进程运行结果
(3)选择2:
从文件读取数据。
如图4.5。
在f:
\test.txt中,数据如下。
//从文件读取数据
/*数据格式如下
进程名优先级到达时间服务时间
proc12110
proc22111
proc3322
*/
运行结果。
如图4.6
图4.6文件读取数据及运行结果
5.使用说明
根据屏幕提示输入即可。
需要用户注意的是优先级(整数表示)越大优先权越大。
6.心得体会
本次课程设计,第一天上午去拿课程设计题目采用静态优先权优先算法的进程调度程序,下午开始查找资料并且构思。
在网上看到一些课程设计用到的有数据结构体struct,便再度拿出C语言书本以及数据结构课本,主要看了数据结构体和单链表方面的知识。
上面也曾说到,程序设计初期打算在单链表中直接使用冒泡排序法进程优先级的排序,总是失败。
后来意识到,进程调度不仅要考虑到进程优先级,同时还有进入内存的时间。
因为只有到达进入内存的时间,进程才会被调入内存中,进一步通过满足条件被调入CPU中执行,或者说分配CPU进而执行。
所以索性模拟出调入内存方法以及调入CPU方法,与此有极大关系的是单链表就绪列表的建立。
进行代码编写以及测试,从而模拟进程调度算法完成。
7.附录
7.1源代码
#include<
stdio.h>
stdlib.h>
string.h>
intSequenceNumber=1;
//进程编号
intProcessAmount;
//进程数量
intStartCount=0;
//调入内存进程计数
structPCB{//进程控制块
intNo;
//进程号
charname[16];
//进程名
intenterMemoryTime;
//进入内存时间
intserviceTime;
//服务时间
intpriority;
//优先级
structPCB*next;
//函数声明
intgetch();
inttimeOfData(FILE*);
voidListAllPCB();
voidMenu();
voidprintPCB(structPCB*,int,int,int,double);
voidprintPCBP(structPCB*);
voidprintField();
voidprintFieldP();
voidrelease(structPCB*);
voidreleaseR(structPCB*);
voidsetNewPCB(char*,int,int,int);
structPCB*runTheProcess();
voidnewPCB(){//建立PCB
newP=(structPCB*)malloc(sizeof(structPCB));
if(head==NULL){//判断头节点是否为空
head=newP;
//为空,头节点指向新开辟的内存
}else{
thisP=head;
while(thisP->
next!
=NULL){
thisP=thisP->
next;
}
thisP->
next=newP;
//遍历单链表,找到最后一个元素
}
thisP=newP;
thisP->
No=SequenceNumber;
SequenceNumber++;
printf("
进程号%d\n"
thisP->
No);
输入进程名:
"
);
scanf("
%s"
name);
输入优先级:
%d"
&
thisP->
priority);
输入进入内存时间:
enterMemoryTime);
输入服务时间:
serviceTime);
next=NULL;
}
voidbuildProcess(){//创建进程
inti=0;
输入进程数量:
ProcessAmount);
while(i<
ProcessAmount){
newPCB();
i++;
voidreadyList(structPCB*pcb){//单链表的形式创建就绪队列
if(readyHead==NULL){
readyHead=newP;
thisP=readyHead;
strcpy(thisP->
name,pcb->
No=pcb->
No;
priority=pcb->
priority;
enterMemoryTime=pcb->
enterMemoryTime;
serviceTime=pcb->
serviceTime;
structPCB*callMemory(){//调入内存,返回调入内存的链表节点
intat;
//到达时间
structPCB*markP;
if(head==NULL){
printf("
程序没有找到。
\n"
markP=thisP=head;
//标记指向头节点
at=thisP->
//到达时间为头节点到达时间
=NULL){//当下一节点不为空
if(at>
next->
enterMemoryTime){//判断当前时间是否大于下一节点时间
markP=thisP->
//是,标记此节点
at=markP->
//到达时间更改为标记时间
}
//向后遍历
returnmarkP;
voidrun(){//静态优先级算法执行。
structPCB*temp;
//临时节点用来存储调入内存节点
structPCB*runPro;
//用来接收执行的节点
inti;
//循环初始条件
intat,srt,runtime=0;
//到达时间,开始执行时间,运行时间。
intturnOverTime;
//周转时间
doubleptot;
//带权周转时间
没有发现进程。
for(i=0;
i<
ProcessAmount;
i++){//循环进程的数量次
while(head!
temp=callMemory();
if(i==0){//初始情况下球开始执行时间,完成时间,周转时间,带权周转时间
srt=at=temp->
runtime=at+temp->
turnOverTime=runtime-at;
ptot=turnOverTime*1.0/temp->
}
readyList(temp);
//初始纳入就绪列表以及后续
release(temp);
//释放进程
while(head!
=NULL&
&
(temp=callMemory())->
enterMemoryTime<
=runtime){
readyList(temp);
//循环判断是否纳入就绪列表
release(temp);
runPro=runTheProcess();
if(i>
0){//初始之后,计算各时间及周转
srt=runtime;
runtime+=runPro->
turnOverTime=runtime-runPro->
ptot=turnOverTime*1.0/runPro->
printf("
当前执行的进程:
printField();
printPCB(runPro,srt,runtime,turnOverTime,ptot);
releaseR(runPro);
\n就绪进程列表:
ListAllPCB(readyHead);
structPCB*runTheProcess(){//执行的进程,返回要执行进程的节点
inttime,pri;
structPCB*markThis;
//标记要返回的节点
printf("
没有可运行的进程。
markThis=thisP=readyHead;
time=thisP->
//当前到达时间
pri=thisP->
//当前进程的优先级
if(time==thisP->
enterMemoryTime){//判断之后节点的到达时间是否与当前一致
if(pri<
priority){//一致,则判断优先级决定
pri=thisP->
//更改优先级判断条件为下一优先级
markThis=thisP->
//标记当前节点的下一节点
}else{
break;
returnmarkThis;
voidrelease(structPCB*pcb){//删除原单链表的进程节点
structPCB*markP,*f;
markP=head;
if(pcb!
if(pcb==head){
f=head;
head=head->
free(f);
}else{
thisP=head->
while(thisP!
if(pcb==thisP){
f=thisP;
markP->
next=thisP->
free(f);
break;
}else{
markP=thisP;
thisP=thisP->
voidreleaseR(structPCB*pcb){//删除就绪列表中进程节点
markP=readyHead;
if(pcb==readyHead){
f=readyHead;
readyHead=readyHead->
thisP=readyHead->
voidListAllPCB(structPCB*h){//打印所有进程
printFieldP();
if(h==NULL){
没有进程。
thisP=h;
while(thisP!
printPCBP(thisP);
\n按任意键继续...\n"
getch();
\n\n"
voidprintPCB(structPCB*pcb,inta,intb,intc,doubled){//打印单个数据结果
%-8s%-8d%-8d%-8d%-8d"
pcb->
No,pcb->
priority,pcb->
enterMemoryTime,pcb->
%-8d%-8d%-8d%-8.2lf\n"
a,b,c,d);
}else
voidprintPCBP(structPCB*pcb){
if(pcb!
=NULL)
%-8s%-8d%-8d%-8d%-8d\n"
else
voidprintField(){//打印表头,以及周转时间
进程名\t进程号\t优先级\t到达时\t服务时\t开始执\t完成时\t周转时\t带权周\n"
\t\t\t间\t间\t行时间\t间\t间\t转时间\n"
voidprintFieldP(){//打印表头,字段
进程名\t进程号\t优先级\t到达时\t服务时\n"
\t\t\t间\t间\n"
voidreadFromFile(){
FILE*fp;
inttimes;
charfilePath[20],temp[8];
intpri,emt,st;
输入文件路径:
filePath);
fp=fopen(filePath,"
r"
times=timeOfData(fp);
ProcessAmount=times-2;
times-1){
if(i==0){//这是用来接收文本格式第一行的汉字。
fscanf(fp,"
temp);
}else{//以下用来接收进程数据
name);
pri);
emt);
st);
setNewPCB(name,pri,emt,st);
fclose(fp);
voidsetNewPCB(char*name,intpri,intentime,intsertime){//赋值建立PCB,用于从文本中获取数据
name,name);
priority=pri;
enterMemoryTime=entime;
serviceTime=sertime;
inttimeOfData(FILE*f){//求取从文本中获取数据的进程数量
chartemp[20];
if(f==NULL){
文件没有找到。
exit(0);
while(fgets(temp,20,f)!
i++;
re
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 静态 优先权 优先 算法 进程 调度 程序