1、打印所有进程到屏幕上。4) void Menu():菜单以及执行。5) void newPCB():创建进程。在1)中被调用。6) void readFromFile():从文件中读取数据。7) void readyList(struct PCB *pcb):就绪列表。8) void release(struct PCB *pcb),void releaseR(struct PCB *pcb):释放进程。9) void run():执行静态优先级进程调度算法。10)struct PCB * runTheProcess():标识要执行的进程。11)void setNewPCB(char *nam
2、e,int pri,int entime,int sertime):在6)中被调用。12) int timeOfData(FILE *f):计算文件中进程数量。2.3相关数据结构设计结构体:struct PCB *head,*readyHead;3.详细设计3.1采用C语言定义的相关数据类型struct PCB int ArrivalTime; int ServiceTime; char number10;struct PCB *head,*thisP,*newP; /动态接收进程及详细struct PCB *readyHead; /动态存储就绪队列采用结构体数组,创建一个进程,包含进程相关信
3、息:进程名称、进程优先级、进程到达时间、进程服务时间。3.2调度算法的主要实现struct PCB * callMemory()/调入内存,返回调入内存的链表节点。将达到进程进入内存时间的所有进程调入内存。void readyList(struct PCB *pcb)/单链表的形式创建就绪队列。将进入内存的进程调入就绪列表。struct PCB * runTheProcess()/执行的进程,返回要执行进程的节点。将就绪列表中满足运行条件的进程标识下来并返回节点。void run()/静态优先权算法。将执行的程序释放。调入新的满足执行条件的进程。就绪列表释放。4.运行结果4.1系统调试 在程序
4、设计初期。本想采用冒泡排序将进入内存的进程进行优先级排序,不过最后总是排序失败。最后想到我进行的是进程调度模拟,应该就有进程调入内存以及调入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中
5、,数据如下。/从文件读取数据 /*数据格式如下 进程名 优先级 到达时间服务时间 proc1 2 1 10 proc2 2 1 11 proc3 3 2 2 */运行结果。如图4.6图4.6 文件读取数据及运行结果5.使用说明根据屏幕提示输入即可。需要用户注意的是优先级(整数表示)越大优先权越大。6.心得体会本次课程设计,第一天上午去拿课程设计题目采用静态优先权优先算法的进程调度程序,下午开始查找资料并且构思。在网上看到一些课程设计用到的有数据结构体struct,便再度拿出C语言书本以及数据结构课本,主要看了数据结构体和单链表方面的知识。上面也曾说到,程序设计初期打算在单链表中直接使用冒泡排序
6、法进程优先级的排序,总是失败。后来意识到,进程调度不仅要考虑到进程优先级,同时还有进入内存的时间。因为只有到达进入内存的时间,进程才会被调入内存中,进一步通过满足条件被调入CPU中执行,或者说分配CPU进而执行。所以索性模拟出调入内存方法以及调入CPU方法,与此有极大关系的是单链表就绪列表的建立。进行代码编写以及测试,从而模拟进程调度算法完成。7.附录7.1 源代码#includestdlib.hstring.hint SequenceNumber=1; /进程编号int ProcessAmount; /进程数量int StartCount=0; /调入内存进程计数struct PCB /进程
7、控制块 int No; /进程号 char name16; /进程名 int enterMemoryTime;/进入内存时间 int serviceTime; /服务时间 int priority; /优先级 struct PCB *next;/函数声明int getch();int timeOfData(FILE *);void ListAllPCB();void Menu();void printPCB(struct PCB *,int,int,int,double);void printPCBP(struct PCB *);void printField();void printFiel
8、dP();void release(struct PCB *);void releaseR(struct PCB *);void setNewPCB(char *,int,int,int);struct PCB * runTheProcess();void newPCB() /建立PCB newP=(struct PCB *)malloc(sizeof(struct PCB); if(head=NULL) /判断头节点是否为空 head=newP; /为空,头节点指向新开辟的内存 else thisP=head; while(thisP-next!=NULL) thisP=thisP-next
9、; thisP-next=newP; /遍历单链表,找到最后一个元素 thisP=newP; thisP-No=SequenceNumber; SequenceNumber+; printf(进程号 %dn,thisP-No);输入进程名:); scanf(%sname);输入优先级:%d,&thisP-priority);输入进入内存时间:enterMemoryTime);输入服务时间:serviceTime);next=NULL;void buildProcess() /创建进程 int i=0;输入进程数量:ProcessAmount); while(iname,pcb-No=pcb-N
10、o;priority=pcb-priority;enterMemoryTime=pcb-enterMemoryTime;serviceTime=pcb-serviceTime;struct PCB * callMemory() /调入内存,返回调入内存的链表节点 int at; /到达时间 struct PCB *markP; if(head=NULL) printf(程序没有找到。n markP=thisP=head; /标记指向头节点 at=thisP- /到达时间为头节点到达时间=NULL) /当下一节点不为空 if(atnext-enterMemoryTime) /判断当前时间是否大于
11、下一节点时间 markP=thisP- /是,标记此节点 at=markP- /到达时间更改为标记时间 /向后遍历 return markP;void run() /静态优先级算法执行。 struct PCB *temp; /临时节点用来存储调入内存节点 struct PCB *runPro; /用来接收执行的节点 int i; /循环初始条件 int at,srt,runtime=0; /到达时间,开始执行时间,运行时间。 int turnOverTime; /周转时间 double ptot; /带权周转时间没有发现进程。 for(i=0;i runtime=at+temp- turnOv
12、erTime=runtime-at; ptot=turnOverTime*1.0/temp- readyList(temp); /初始纳入就绪列表以及后续 release(temp); /释放进程 while(head!=NULL&(temp=callMemory()-enterMemoryTime0) /初始之后,计算各时间及周转 srt=runtime; runtime+=runPro- turnOverTime=runtime-runPro- ptot=turnOverTime*1.0/runPro- printf(当前执行的进程: printField(); printPCB(runP
13、ro,srt,runtime,turnOverTime,ptot); releaseR(runPro);n就绪进程列表: ListAllPCB(readyHead);struct PCB * runTheProcess() /执行的进程,返回要执行进程的节点 int time,pri; struct PCB *markThis; /标记要返回的节点 printf(没有可运行的进程。 markThis=thisP=readyHead; time=thisP-/当前到达时间 pri=thisP- /当前进程的优先级 if(time=thisP-enterMemoryTime)/判断之后节点的到达时
14、间是否与当前一致 if(pri/更改优先级判断条件为下一优先级 markThis=thisP- /标记当前节点的下一节点 else break; return markThis;void release(struct PCB *pcb) /删除原单链表的进程节点 struct PCB *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
15、; else markP=thisP; thisP=thisP-void releaseR(struct PCB *pcb) /删除就绪列表中进程节点 markP=readyHead; if(pcb=readyHead) f=readyHead; readyHead=readyHead- thisP=readyHead-void ListAllPCB(struct PCB *h) /打印所有进程 printFieldP(); if(h=NULL)没有进程。 thisP=h; while(thisP! printPCBP(thisP);n按任意键继续.n getch();nnvoid print
16、PCB(struct PCB *pcb,int a,int b,int c,double d) /打印单个数据结果%-8s%-8d%-8d%-8d%-8d,pcb-No,pcb-priority,pcb-enterMemoryTime,pcb-%-8d%-8d%-8d%-8.2lfn,a,b,c,d); elsevoid printPCBP(struct PCB *pcb) if(pcb!=NULL)%-8s%-8d%-8d%-8d%-8dn elsevoid printField() /打印表头,以及周转时间进程名t进程号t优先级t到达时t服务时t开始执t完成时t周转时t带权周nttt间t间
17、t行时间t间t间t转时间nvoid printFieldP() /打印表头,字段进程名t进程号t优先级t到达时t服务时nttt间t间nvoid readFromFile() FILE *fp; int times; char filePath20,temp8; int pri,emt,st;输入文件路径:,filePath); fp=fopen(filePath,r times=timeOfData(fp); ProcessAmount=times-2;times-1) if(i=0) /这是用来接收文本格式第一行的汉字。 fscanf(fp,temp); else /以下用来接收进程数据,n
18、ame);pri);emt);st); setNewPCB(name,pri,emt,st); fclose(fp);void setNewPCB(char *name,int pri,int entime,int sertime)/赋值建立PCB,用于从文本中获取数据name,name);priority=pri;enterMemoryTime=entime;serviceTime=sertime;int timeOfData(FILE *f) /求取从文本中获取数据的进程数量 char temp20; if(f=NULL)文件没有找到。 exit(0); while(fgets(temp,20,f)! i+; re
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1