操作系统实验报告-进程调度zzp

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

·1·沈阳工程学院学生实验报告实验室名称:计算机实验室实验课程名称:操作系统实验项目名称:进程调度实验日期:2016年月日班级:物联网151姓名:学号:指导教师:曲乐声刘琪批阅教师:成绩:一.实验目的本实验要求用高级语言编写模拟进程调度程序,以便加深理解有关进程控制快、进程队列等概念,并体会和了解基于时间片轮转调度算法的具体实施办法。二.实验设备PC机一台,WIN-TC软件。三.实验项目(1)设计进程控制块PCB的结构,通常应包括如下信息:进程名、或轮转时间片数、进程已占用的CPU时间、进程到完成还需要的时间、进程的状态、进程优先级、当前队列指针等(2)编写调度算法程序:时间片轮转、优先级调度算法程序(3)按要求输出结果。(附图)四.根据实验写结果1.时间片轮转时间片大小进程名ABCDE平均q=4到达时间01234服务时间23456完成时间2591522周转时间24712188.6带权周转时间11.31.752.431.892.优先级时间片大小进程名ABCDE平均q=4到达时间01234服务时间23456优先数12345完成时间211131820周转时间21011151610.8带权周转时间13.32.7532.62.5四.实验程序代码(附页)成绩评定算法正确性2.521.510.50程序正确性2.521.510.50结果及分析的正确性543210成绩·2·时间片轮转#includestdio.h#includestdlib.h#includestring.htypedefstructnode{charname[20];/*进程的名字*/intprio;/*进程的优先级*/intround;/*分配CPU的时间片*/intcputime;/*CPU执行时间*/intneedtime;/*进程执行所需要的时间*/charstate;/*进程的状态,W——就绪态,R——执行态,F——完成态*/intcount;/*记录执行的次数*/structnode*next;/*链表指针*/}PCB;PCB*ready=NULL,*run=NULL,*finish=NULL;/*定义三个队列,就绪队列,执行队列和完成队列*/intnum;voidGetFirst();/*从就绪队列取得第一个节点*/voidOutput();/*输出队列信息*/voidInsertPrio(PCB*in);/*创建优先级队列,规定优先数越小,优先级越高*/voidInsertTime(PCB*in);/*时间片队列*/voidInsertFinish(PCB*in);/*时间片队列*/voidPrioCreate();/*优先级输入函数*/voidTimeCreate();/*时间片输入函数*/voidPriority();/*按照优先级调度*/voidRoundRun();/*时间片轮转调度*/intmain(void){charchose;printf(输入进程名及其需要运行的时间\n);scanf(%d,&num);实验报告·3·getchar();printf(Schedulingmethodforinputprocess:(P/R)\n);scanf(%c,&chose);switch(chose){case'P':case'p':PrioCreate();Priority();break;case'R':case'r':TimeCreate();RoundRun();break;default:break;}Output();return0;}voidGetFirst()/*取得第一个就绪队列节点*/{run=ready;if(ready!=NULL){run-state='R';ready=ready-next;run-next=NULL;}}·4·voidOutput()/*输出队列信息*/{PCB*p;p=ready;printf(进程名\tpriority\tnumber\tcputime\ttimeyouneed\tprocessstate\tcounter\n);while(p!=NULL){printf(%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n,p-name,p-prio,p-round,p-cputime,p-needtime,p-state,p-count);p=p-next;}p=finish;while(p!=NULL){printf(%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n,p-name,p-prio,p-round,p-cputime,p-needtime,p-state,p-count);p=p-next;}p=run;while(p!=NULL){printf(%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n,p-name,p-prio,p-round,p-cputime,p-needtime,p-state,p-count);p=p-next;}}voidInsertPrio(PCB*in)/*创建优先级队列,规定优先数越小,优先级越低*/{PCB*fst,*nxt;fst=nxt=ready;实验报告·5·if(ready==NULL)/*如果队列为空,则为第一个元素*/{in-next=ready;ready=in;}else/*查到合适的位置进行插入*/{if(in-prio=fst-prio)/*比第一个还要大,则插入到队头*/{in-next=ready;ready=in;}else{while(fst-next!=NULL)/*移动指针查找第一个别它小的元素的位置进行插入*/{nxt=fst;fst=fst-next;}if(fst-next==NULL)/*已经搜索到队尾,则其优先级数最小,将其插入到队尾即可*/{in-next=fst-next;fst-next=in;}else/*插入到队列中*/{nxt=in;in-next=fst;}}}·6·}voidInsertTime(PCB*in)/*将进程插入到就绪队列尾部*/{PCB*fst;fst=ready;if(ready==NULL){in-next=ready;ready=in;}else{while(fst-next!=NULL){fst=fst-next;}in-next=fst-next;fst-next=in;}}voidInsertFinish(PCB*in)/*将进程插入到完成队列尾部*/{PCB*fst;fst=finish;if(finish==NULL){in-next=finish;finish=in;}else实验报告·7·{while(fst-next!=NULL){fst=fst-next;}in-next=fst-next;fst-next=in;}}voidPrioCreate()/*优先级调度输入函数*/{PCB*tmp;inti;printf(Entertheprocessnameandprocesstimerequired:\n);for(i=0;inum;i++){if((tmp=(PCB*)malloc(sizeof(PCB)))==NULL){perror(malloc);exit(1);}scanf(%s,tmp-name);getchar();/*吸收回车符号*/scanf(%d,&(tmp-needtime));tmp-cputime=0;tmp-state='W';tmp-prio=50-tmp-needtime;/*设置其优先级,需要的时间越多,优先级越低*/tmp-round=0;tmp-count=0;InsertPrio(tmp);/*按照优先级从高到低,插入到就绪队列*/}·8·}voidTimeCreate()/*时间片输入函数*/{PCB*tmp;inti;printf(Entertheprocessnameandthetimerequiredfortheprocesstimeslice:\n);for(i=0;inum;i++){if((tmp=(PCB*)malloc(sizeof(PCB)))==NULL){perror(malloc);exit(1);}scanf(%s,tmp-name);getchar();scanf(%d,&(tmp-needtime));tmp-cputime=0;tmp-state='W';tmp-prio=0;tmp-round=2;/*假设每个进程所分配的时间片是2*/tmp-count=0;InsertTime(tmp);}}voidPriority()/*按照优先级调度,每次执行一个时间片*/{intflag=1;GetFirst();while(run!=NULL)/*当就绪队列不为空时,则调度进程如执行队列执行*/实验报告·9·{Output();/*输出每次调度过程中各个节点的状态*/while(flag){run-prio-=3;/*优先级减去三*/run-cputime++;/*CPU时间片加一*/run-needtime--;/*进程执行完成的剩余时间减一*/if(run-needtime==0)/*如果进程执行完毕,将进程状态置为F,将其插入到完成队列*/{run-state='F';run-count++;/*进程执行的次数加一*/InsertFinish(run);flag=0;}else/*将进程状态置为W,入就绪队列*/{run-state='W';run-count++;/*进程执行的次数加一*/InsertTime(run);flag=0;}}flag=1;GetFirst();/*继续取就绪队列队头进程进入执行队列*/}}voidRoundRun()/*时间片轮转调度算法*/{intflag=1;GetFirst();·10·while(run!=NULL){Output();while(flag){run-count++;run-cputime++;run-needtime--;if(run-needtime==0)/*进程执行完毕*/{run-state='F';InsertFinish(run);flag=0;}elseif(run-count==run-round)/*时间片用完*/{run-state='W';run-count=0;/*计数器清零,为下次做准备*/InsertTime(run);flag=0;}}flag=1;GetFirst();}}实验报告·11·优先级#includestdio.h#includestdlib.h#includeconio.h#definegetpch(type)(type*)malloc(sizeof(type))#defineNULL0structpcb{charname[10];charstate;intsuper;intntime;intrtime;structpcb*link;}*ready=NULL,*p;typedefstructpcbPCB;·12·voidsort(){PCB*first,*second;intinsert=0;if((ready==NULL)||((p-super)(ready-super))){p-link=ready;ready=p;}else/*进程比较优先级,插入适当的位置中*/{first=ready;second=first-link;while(second!=NULL){if((p-super)(second-super))/*若插入进程比当前进程优先数大*/{/*插入到当前进程前面*/p-link=second;fir

1 / 16
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功