《2022年2022年惠州学院操作系统实验五》由会员分享,可在线阅读,更多相关《2022年2022年惠州学院操作系统实验五(24页珍藏版)》请在金锄头文库上搜索。
1、实验五模拟进度调度1、实验目的1.理解 PCB 2.理解进程的并发执行3.理解进程的FCFS、动态优先权和时间片轮转三种调度算法,并模拟实现这三种算法2、实验器材微型计算机、 fedora 版本的 Linux 系统3、实验内容PCB 在本设计中的基本结构Typedef struct node Char name10;/* 进程标识符 */ Float prin;/* 进程的优先级 */ Int round;/* 进程轮转的时间片*/ Int needtime;/* 进程还需要的cpu 时间 */ Int waittime;/* 进程进入系统后等待cpu 的时间 */ Char state; /
2、* 进程的状态 */ Struct node *next;/* 链接指针 */ PCB 设计三个队列:就绪、完成、运行,其中运行队列中应该只有一个进程。创建进程,就是用户输入进程的标识符和运行所需时间,系统为其生成一个PCB,存放进程的信息,将新生成的PCB 插入就绪队列。进程任务结束,系统将其PCB 插入完成队列,该进程就消亡。任务 1 :动态优先权调度算法模拟:优先权 = 1 + 等待时间 / 需要服务时间 - 最高响应比优先(1) 输入一批作业的进程标识和估计运行时间,将其PCB 插入就绪队列(2) 当就绪队列非空、CPU 空闲时,计算出每个就绪进程的优先权(3) 将优先权最高的进程从就
3、绪队列移入运行队列,状态改为运行(模拟进程调度) (4) 在屏幕上输出当前所有进程的状态和优先权信息(已完成进程不显示) (5) 让运行进程执行足够的服务时间(6) 将运行进程移入完成队列(7) 提示可以输入新的作业,如果有新作业输入,则转向(1) ,否则转向 (2) (8) 如果就绪队列为空,则结束程序框图如下:名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 24 页 - - - - - - - - - 程序源代码如下:#include #include typedef
4、 struct node char name10; / 进程标识符float prin; / 进程的优先级int round; / 进程轮转的时间片名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 24 页 - - - - - - - - - int needtime; / 进程还需要当CPU 时间int waittime; / 进程进入系统后等待当CPU 的时间char state; / 进程当状态struct node *next; / 链接指针 PCB; PCB *r
5、eadyQueue=NULL, *runningQueue=NULL, *finishedQueue=NULL, *finishedQueueTail=NULL, *readyQueueTail=NULL, pcbPool10; int poolSize = 0; void createPro(); / 根据进程标识和估计运行时间,将其PCB 插入就绪队列void readyQueueNotNull(); / 当就绪队列非空、CPU 空闲时,计算每个就绪进程当优先权void insertRunningQueue(); / 将优先权最高当进程从就绪队列移入运行队列,状态改为运行void disp
6、layQueue(); / 将所有非结束进程的状态和优先权信息显示在屏幕void runPro(); / 让运行进程执行足够的服务时间void addNewPro(); / 提示可以输入新的进程int main(int argc, char const *argv) createPro(); while(readyQueue!=NULL) readyQueueNotNull(); insertRunningQueue(); displayQueue(); runPro(); puts(); addNewPro(); puts(Bye); return 0; void createPro() c
7、har name10; char stop = y; int needtime, i=0; while(stop=y) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 24 页 - - - - - - - - - / 获取用户输入进程名puts(Please input the process name: ); scanf(%s, name); strcpy(pcbPooli.name, name); / 获取用户输入进程需要时间puts(Please input the
8、 need of time: ); scanf(%d, &needtime); pcbPooli.needtime = needtime; pcbPooli.state = 0; pcbPooli.next = NULL; pcbPooli.prin = 0.0f; pcbPooli.waittime = 0; if(i=0) readyQueue = &pcbPool0; readyQueueTail = &pcbPool0; else readyQueueTail-next = &pcbPooli; readyQueueTail = &pcbPooli; if(i+ 9) break; p
9、uts(Do you want to input more process(y or n): ); scanf(%s, &stop); poolSize = i + 1; void readyQueueNotNull() PCB *pcb = readyQueue; while(1) pcb-prin = 1 + pcb-waittime / pcb-needtime; if(pcb-next=NULL) break; else pcb = pcb-next; void insertRunningQueue() 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - -
10、- - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 24 页 - - - - - - - - - PCB *currentPCB = readyQueue, *lastPCB = readyQueue, *lastOfChosePCB = readyQueue, *chosePCB = readyQueue; while(1) / 选择优先级最高的进程if(currentPCB-prin prin) lastOfChosePCB = lastPCB; chosePCB = currentPCB; / 判断就绪队列是否判断结束if(currentPCB-ne
11、xt!=NULL) lastPCB = currentPCB; currentPCB = currentPCB-next; else break; chosePCB-state = 1; / 将优先级最高的进程状态标识为运行时runningQueue = chosePCB; / 将优先级最高的进程从就绪队列移入运行队列/ 将优先级最高的进程从就绪队列中移出if(readyQueue=chosePCB) readyQueue = readyQueue-next; else lastOfChosePCB-next = chosePCB-next; void displayQueue() if(re
12、adyQueue!=NULL) PCB pcb = *readyQueue; while(1) printf(Process: %s State: %d prin: %fn, pcb.name, pcb.state, pcb.prin); if(pcb.next=NULL) break; else pcb = *(pcb.next); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 24 页 - - - - - - - - - if(runningQueue!=NULL)
13、 printf(Process: %s State: %d prin: %fn, runningQueue-name, runningQueue-state, runningQueue-prin); void runPro() sleep(runningQueue-needtime); if(readyQueue!=NULL) PCB *pcb = readyQueue; while(1) pcb-waittime = pcb-waittime + runningQueue-needtime; if(pcb-next=NULL) break; else pcb = pcb-next; / 将运
14、行了需要时间的进程移入完成队列if(finishedQueue=NULL) finishedQueue = runningQueue; finishedQueueTail = runningQueue; finishedQueueTail-next = NULL; else finishedQueueTail-next = runningQueue; finishedQueueTail = runningQueue; / 清理运行时队列runningQueue-state = 0; runningQueue = NULL; void addNewPro() char name10; char
15、stop = y; int needtime, i=poolSize-1; puts(Do you want to input more process(y or n): ); scanf(%s, &stop); while(stop=y) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 24 页 - - - - - - - - - / 获取用户输入进程名puts(Please input the process name: ); scanf(%s, name); str
16、cpy(pcbPooli.name, name); / 获取用户输入进程需要时间puts(Please input the need of time: ); scanf(%d, &needtime); pcbPooli.needtime = needtime; pcbPooli.state = 0; pcbPooli.next = NULL; pcbPooli.prin = 0.0f; pcbPooli.waittime = 0; if(readyQueue=NULL) readyQueue = &pcbPooli; readyQueueTail = &pcbPooli; else ready
17、QueueTail-next = &pcbPooli; readyQueueTail = &pcbPooli; if(i+ 9) break; else puts(Do you want to input more process(y or n): ); scanf(%s, &stop); poolSize = i + 1; 程序运行截图如下:名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 24 页 - - - - - - - - - 名师资料总结 - - -精品资料欢迎
18、下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 24 页 - - - - - - - - - 任务 2 :时间片轮转调度算法模拟输入一批作业的进程标识和估计运行时间,由其PCB 组成就绪队列。时间片轮转调度,系统给就绪队列的第一个进程分配一个时间片,大小等于n ( 个时钟周期 ) ,再将它移入运行队列。注意,时间片的大小要比大多数进程的估计运行时间短。当时间片结束的时候,要将没有运行完的进程从运行队列移到就绪队列的末尾,再次进行调度。在每次调度时将所有进程的当前情况显示出来。名师资料总结 - - -精品资料
19、欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 24 页 - - - - - - - - - 程序框图如下:程序源代码如下:#include #include typedef struct node char name10; / 进程标识符float prin; / 进程的优先级int round; / 进程轮转的时间片名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 24 页 -
20、 - - - - - - - - int needtime; / 进程还需要当CPU 时间int waittime; / 进程进入系统后等待当CPU 的时间char state; / 进程当状态struct node *next; / 链接指针 PCB; PCB *readyQueue=NULL, *runningQueue=NULL, *finishedQueue=NULL, *finishedQueueTail=NULL, *readyQueueTail=NULL, pcbPool10; int poolSize = 0; void createPro(); / 根据进程标识和估计运行时间
21、,将其PCB插入就绪队列void insertRunningQueue(); / 将优先权最高当进程从就绪队列移入运行队列,状态改为运行void displayQueue(); / 将所有非结束进程的状态和优先权信息显示在屏幕void runPro(); / 让运行进程执行足够的服务时间int main(int argc, char const *argv) createPro(); while(readyQueue!=NULL) insertRunningQueue(); displayQueue(); runPro(); puts(); puts(Bye); return 0; void
22、createPro() char name10; char stop = y; int needtime, i=0; while(stop=y) / 获取用户输入进程名puts(Please input the process name: ); scanf(%s, name); strcpy(pcbPooli.name, name); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 24 页 - - - - - - - - - / 获取用户输入进程需要时间puts(Pl
23、ease input the need of time: ); scanf(%d, &needtime); pcbPooli.needtime = needtime; pcbPooli.state = 0; pcbPooli.next = NULL; pcbPooli.prin = 0.0f; pcbPooli.round = 1; if(i=0) readyQueue = &pcbPool0; readyQueueTail = &pcbPool0; else readyQueueTail-next = &pcbPooli; readyQueueTail = &pcbPooli; if(i+
24、9) break; puts(Do you want to input more process(y or n): ); scanf(%s, &stop); poolSize = i + 1; void insertRunningQueue() runningQueue = readyQueue; readyQueue = readyQueue-next; runningQueue-state = 1; void displayQueue() if(readyQueue!=NULL) PCB pcb = *readyQueue; while(1) printf(Process: %s Stat
25、e: %d Needtime: %dn, pcb.name, pcb.state, pcb.needtime); if(pcb.next=NULL) break; else pcb = *(pcb.next); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 24 页 - - - - - - - - - if(runningQueue!=NULL) printf(Process: %s State: %d Needtime: %dn, runningQueue-name
26、, runningQueue-state, runningQueue-needtime); void runPro() sleep(runningQueue-round); / 模拟执行进程/ 更新运行队列runningQueue-needtime = runningQueue-needtime - runningQueue-round; runningQueue-state = 0; / 将运行结束当进程移入完成队列if(runningQueue-needtimenext = NULL; else finishedQueueTail-next = runningQueue; finished
27、QueueTail = runningQueue; else if(readyQueue=NULL) readyQueue = runningQueue; readyQueueTail = runningQueue; else readyQueueTail-next = runningQueue; readyQueueTail = runningQueue; readyQueueTail-next = NULL; / 清理运行时队列runningQueue = NULL; 程序运行截图如下:名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - -
28、 - - 名师精心整理 - - - - - - - 第 13 页,共 24 页 - - - - - - - - - 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 24 页 - - - - - - - - - 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 24 页 - - - - - - - - - 名师资料总结 - - -精品资料欢迎下载 -
29、 - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 16 页,共 24 页 - - - - - - - - - 任务 2 :FCFS 调度算法模拟按一定的顺序输入一批作业的进程标识和估计运行时间,由其PCB 按照进程标识输入的顺序组成就绪队列。当就绪队列非空、CPU 空闲时,将就绪队列的第一个进程从就绪队列移入运行队列,状态改为运行。以后个步骤类似于2。程序框图如下:名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 17
30、页,共 24 页 - - - - - - - - - 程序源代码如下:#include #include typedef struct node char name10; / 进程标识符float prin; / 进程的优先级int round; / 进程轮转的时间片int needtime; / 进程还需要当CPU 时间名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 18 页,共 24 页 - - - - - - - - - int waittime; / 进程进入系统后等待当CP
31、U 的时间char state; / 进程当状态struct node *next; / 链接指针 PCB; PCB *readyQueue=NULL, *runningQueue=NULL, *finishedQueue=NULL, *finishedQueueTail=NULL, *readyQueueTail=NULL, pcbPool10; int poolSize = 0; void createPro(); / 根据进程标识和估计运行时间,将其PCB插入就绪队列void insertRunningQueue(); / 将优先权最高当进程从就绪队列移入运行队列,状态改为运行void
32、displayQueue(); / 将所有非结束进程的状态和优先权信息显示在屏幕void runPro(); / 让运行进程执行足够的服务时间void addNewPro(); / 提示可以输入新的进程int main(int argc, char const *argv) createPro(); while(readyQueue!=NULL) insertRunningQueue(); displayQueue(); runPro(); puts(); addNewPro(); puts(Bye); return 0; void createPro() char name10; char
33、stop = y; int needtime, i=0; while(stop=y) / 获取用户输入进程名puts(Please input the process name: ); scanf(%s, name); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 19 页,共 24 页 - - - - - - - - - strcpy(pcbPooli.name, name); / 获取用户输入进程需要时间puts(Please input the need of time: )
34、; scanf(%d, &needtime); pcbPooli.needtime = needtime; pcbPooli.state = 0; pcbPooli.next = NULL; pcbPooli.prin = 0.0f; pcbPooli.waittime = 0; if(i=0) readyQueue = &pcbPool0; readyQueueTail = &pcbPool0; else readyQueueTail-next = &pcbPooli; readyQueueTail = &pcbPooli; if(i+ 9) break; puts(Do you want
35、to input more process(y or n): ); scanf(%s, &stop); poolSize = i + 1; void insertRunningQueue() runningQueue = readyQueue; readyQueue = readyQueue-next; runningQueue-state = 1; void displayQueue() if(readyQueue!=NULL) PCB pcb = *readyQueue; while(1) printf(Process: %s State: %d Waittime: %dn, pcb.na
36、me, pcb.state, pcb.waittime); if(pcb.next=NULL) break; else 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 20 页,共 24 页 - - - - - - - - - pcb = *(pcb.next); if(runningQueue!=NULL) printf(Process: %s State: %d Waittime: %dn, runningQueue-name, runningQueue-state, runni
37、ngQueue-waittime); void runPro() sleep(runningQueue-needtime); / 模拟执行进程/ 更新运行队列if(readyQueue!=NULL) PCB *pcb = readyQueue; while(1) pcb-waittime = pcb-waittime + runningQueue-needtime; if(pcb-next=NULL) break; else pcb = pcb-next; / 将运行结束当进程移入完成队列if(finishedQueue=NULL) finishedQueue = runningQueue;
38、finishedQueueTail = runningQueue; finishedQueueTail-next = NULL; else finishedQueueTail-next = runningQueue; finishedQueueTail = runningQueue; / 清理运行时队列runningQueue-state = 0; runningQueue = NULL; void addNewPro() char name10; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - -
39、 - - 第 21 页,共 24 页 - - - - - - - - - char stop = y; int needtime, i=poolSize-1; puts(Do you want to input more process(y or n): ); scanf(%s, &stop); while(stop=y) / 获取用户输入进程名puts(Please input the process name: ); scanf(%s, name); strcpy(pcbPooli.name, name); / 获取用户输入进程需要时间puts(Please input the need
40、of time: ); scanf(%d, &needtime); pcbPooli.needtime = needtime; pcbPooli.state = 0; pcbPooli.next = NULL; pcbPooli.prin = 0.0f; pcbPooli.waittime = 0; if(readyQueue=NULL) readyQueue = &pcbPooli; readyQueueTail = &pcbPooli; else readyQueueTail-next = &pcbPooli; readyQueueTail = &pcbPooli; if(i+ 9) br
41、eak; puts(Do you want to input more process(y or n): ); scanf(%s, &stop); poolSize = i + 1; 程序运行截图如下:名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 22 页,共 24 页 - - - - - - - - - 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 23 页,共 24 页 - - - - - - - - - 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 24 页,共 24 页 - - - - - - - - -