1、实验五模拟进度调度实验五 模拟进度调度班级:09网络工程 姓名:林泽全 学号:0914021161、 实验目的1. 理解PCB2. 理解进程的并发执行3. 理解进程的FCFS、动态优先权和时间片轮转三种调度算法,并模拟实现这三种算法2、 实验器材 微型计算机、fedora版本的Linux系统3、 实验内容PCB在本设计中的基本结构Typedef struct nodeChar name10;/*进程标识符*/Float prin;/*进程的优先级*/Int round;/*进程轮转的时间片*/Int needtime;/*进程还需要的cpu时间*/Int waittime;/*进程进入系统后等
2、待cpu的时间*/Char state;/*进程的状态*/Struct node *next;/*链接指针*/PCB设计三个队列:就绪、完成、运行,其中运行队列中应该只有一个进程。创建进程,就是用户输入进程的标识符和运行所需时间,系统为其生成一个PCB,存放进程的信息,将新生成的PCB插入就绪队列。进程任务结束,系统将其PCB插入完成队列,该进程就消亡。任务1:动态优先权调度算法模拟:优先权= 1 + 等待时间/需要服务时间-最高响应比优先(1) 输入一批作业的进程标识和估计运行时间,将其PCB插入就绪队列(2) 当就绪队列非空、CPU空闲时,计算出每个就绪进程的优先权(3) 将优先权最高的进
3、程从就绪队列移入运行队列,状态改为运行(模拟进程调度)(4) 在屏幕上输出当前所有进程的状态和优先权信息(已完成进程不显示)(5) 让运行进程执行足够的服务时间(6) 将运行进程移入完成队列(7) 提示可以输入新的作业,如果有新作业输入,则转向(1) ,否则转向(2)(8) 如果就绪队列为空,则结束程序框图如下:程序源代码如下:#include #include typedef struct node char name10; /进程标识符 float prin; /进程的优先级 int round; /进程轮转的时间片 int needtime; /进程还需要当CPU时间 int waitt
4、ime; /进程进入系统后等待当CPU的时间 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 readyQueueNotNull(); /当就绪队列非空、CPU空闲时,计算每个就绪
5、进程当优先权void insertRunningQueue(); /将优先权最高当进程从就绪队列移入运行队列,状态改为运行void displayQueue(); /将所有非结束进程的状态和优先权信息显示在屏幕void runPro(); /让运行进程执行足够的服务时间void addNewPro(); /提示可以输入新的进程int main(int argc, char const *argv) createPro(); while(readyQueue!=NULL) readyQueueNotNull(); insertRunningQueue(); displayQueue(); run
6、Pro(); puts(); addNewPro(); puts(Bye); return 0;void 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); /获取用户输入进程需要时间 puts(Please input the need of time: ); scanf(%d, &needtime); pcb
7、Pooli.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 to input more process(y or
8、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()PCB *currentPCB = readyQueue, *lastPCB = readyQueue, *lastOfChosePCB = readyQueue, *ch
9、osePCB = readyQueue; while(1) /选择优先级最高的进程 if(currentPCB-prin prin) lastOfChosePCB = lastPCB; chosePCB = currentPCB; /判断就绪队列是否判断结束 if(currentPCB-next!=NULL) lastPCB = currentPCB; currentPCB = currentPCB-next; else break; chosePCB-state = 1; /将优先级最高的进程状态标识为运行时 runningQueue = chosePCB; /将优先级最高的进程从就绪队列移
10、入运行队列 /将优先级最高的进程从就绪队列中移出 if(readyQueue=chosePCB) readyQueue = readyQueue-next; else lastOfChosePCB-next = chosePCB-next;void displayQueue() if(readyQueue!=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 =
11、 *(pcb.next); if(runningQueue!=NULL) 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=NUL
12、L) break; else pcb = pcb-next; /将运行了需要时间的进程移入完成队列 if(finishedQueue=NULL) finishedQueue = runningQueue; finishedQueueTail = runningQueue; finishedQueueTail-next = NULL; else finishedQueueTail-next = runningQueue; finishedQueueTail = runningQueue; /清理运行时队列 runningQueue-state = 0; runningQueue = NULL;v
13、oid addNewPro() char name10; 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 of time:
14、 ); 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) break; els
15、e puts(Do you want to input more process(y or n): ); scanf(%s, &stop); poolSize = i + 1; 程序运行截图如下:任务2:时间片轮转调度算法模拟输入一批作业的进程标识和估计运行时间,由其PCB组成就绪队列。时间片轮转调度,系统给就绪队列的第一个进程分配一个时间片,大小等于n (个时钟周期),再将它移入运行队列。注意,时间片的大小要比大多数进程的估计运行时间短。当时间片结束的时候,要将没有运行完的进程从运行队列移到就绪队列的末尾,再次进行调度。在每次调度时将所有进程的当前情况显示出来。程序框图如下:程序源代码如下:
16、#include #include typedef struct node char name10; /进程标识符 float prin; /进程的优先级 int round; /进程轮转的时间片 int needtime; /进程还需要当CPU时间 int waittime; /进程进入系统后等待当CPU的时间 char state; /进程当状态 struct node *next; /链接指针 PCB;PCB *readyQueue=NULL, *runningQueue=NULL, *finishedQueue=NULL, *finishedQueueTail=NULL, *ready
17、QueueTail=NULL, pcbPool10;int poolSize = 0;void createPro(); /根据进程标识和估计运行时间,将其PCB插入就绪队列void insertRunningQueue(); /将优先权最高当进程从就绪队列移入运行队列,状态改为运行void displayQueue(); /将所有非结束进程的状态和优先权信息显示在屏幕void runPro(); /让运行进程执行足够的服务时间int main(int argc, char const *argv) createPro(); while(readyQueue!=NULL) insertRunn
18、ingQueue(); displayQueue(); runPro(); puts(); puts(Bye); return 0;void 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); /获取用户输入进程需要时间 puts(Please input the need of time: ); scanf(%
19、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+ 9) break; puts(Do you want to input mor
20、e 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 Needtime: %dn, pcb.name, pcb.state,
21、 pcb.needtime); if(pcb.next=NULL) break; else pcb = *(pcb.next); if(runningQueue!=NULL) printf(Process: %s State: %d Needtime: %dn, runningQueue-name, runningQueue-state, runningQueue-needtime);void runPro() sleep(runningQueue-round); /模拟执行进程 /更新运行队列 runningQueue-needtime = runningQueue-needtime - r
22、unningQueue-round; runningQueue-state = 0; /将运行结束当进程移入完成队列 if(runningQueue-needtimenext = NULL; else finishedQueueTail-next = runningQueue; finishedQueueTail = runningQueue; else if(readyQueue=NULL) readyQueue = runningQueue; readyQueueTail = runningQueue; else readyQueueTail-next = runningQueue; re
23、adyQueueTail = runningQueue; readyQueueTail-next = NULL; /清理运行时队列 runningQueue = NULL;程序运行截图如下:任务2:FCFS调度算法模拟按一定的顺序输入一批作业的进程标识和估计运行时间,由其PCB按照进程标识输入的顺序组成就绪队列。当就绪队列非空、CPU空闲时,将就绪队列的第一个进程从就绪队列移入运行队列,状态改为运行。以后个步骤类似于2。程序框图如下:程序源代码如下:#include #include typedef struct node char name10; /进程标识符 float prin; /进程
24、的优先级 int round; /进程轮转的时间片 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(); /根据进程标识和估计运行
25、时间,将其PCB插入就绪队列void insertRunningQueue(); /将优先权最高当进程从就绪队列移入运行队列,状态改为运行void displayQueue(); /将所有非结束进程的状态和优先权信息显示在屏幕void runPro(); /让运行进程执行足够的服务时间void addNewPro(); /提示可以输入新的进程int main(int argc, char const *argv) createPro(); while(readyQueue!=NULL) insertRunningQueue(); displayQueue(); runPro(); puts()
26、; addNewPro(); puts(Bye); return 0;void 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); /获取用户输入进程需要时间 puts(Please input the need of time: ); scanf(%d, &needtime); pcbPooli.needtim
27、e = 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 to input more process(y or n): ); scanf(%s, &stop);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1