编写并调试一个多道程序系统的作业调度模拟

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

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

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

资源描述

编写并调试一个多道程序系统的作业调度模拟程序五编写并调试一个多道程序系统的作业调度模拟程序。作业调度算法:采用基于优先级的作业调度。可以参考课本中的例子自行设计。#include<stdio.h>#include<stdlib.h>#definegetjch(type)(type*)malloc(sizeof(type))#defineN10structjcb{/*定义作业控制块PCB*/charname[10];floatneedtime;/*运行时间*/floatarrivetime;/*提交时刻*/floatstorage[N];/*系统资源*/structjcb*link;}*ready=NULL,*pb=NULL,*p;typedefstructjcbJCB;floatTc,Ti,Wi,T=0;/*完成时刻,周转时间,带权周转时间,时间量*/floatTiSum=0,WiSum=0;/*平均周转时间,带权a平均周转时间*/floatsource[N];intn;voidinput();/*输入作业信息*/intspace();/*返回就绪队列中作业的数目*/voidfcfs();/*先来先服务算法*/voiddisp(JCB*pr);/*显示相应的作业*/voidrunning();/*运行作业组*/voiddestroy();/*撤销作业*/voidinput()/*建立作业控制块函数*/{inti,k,num;printf("请输入所拥有的资源种类:");scanf("%d",&n);printf("输入系统所拥有资源数:\n");for(i=0;i<n;i++){printf("资源[%d]:",i);scanf("%f",&source[i]);}printf("\n输入作业数量:");scanf("%d",&num);for(i=0;i<num;i++){printf("\n作业号[%d]:\n",i);p=getjch(JCB);printf("输入作业名:");scanf("%s",p->name);printf("输入提交时间:");scanf("%f",&p->arrivetime);printf("输入运行时间:");scanf("%f",&p->needtime);printf("输入所需资源数:\n");for(k=0;k<n;k++){printf("资源[%d]:",i);scanf("%f",&p->storage[k]);}printf("\n");p->link=NULL;fcfs();}}intspace(){intl=0;JCB*pr=ready;while(pr!=NULL){l++;pr=pr->link;}return(l);}voiddisp(JCB*pr)/*建立作业显示函数,用于显示当前作业*/{inti;printf("\n%6s\t%6s\t%6s\t","作业名","运行时间","提交时刻");for(i=0;i<n;i++)printf("资源[%d]\t",i);printf("\n%6s\t%6.2f\t\t%6.2f\t",pr->name,pr->needtime,pr->arrivetime);for(i=0;i<n;i++)printf("\t%6.2f",pr->storage[i]);printf("\n");}voiddestroy()/*建立作业撤消函数(作业运行结束,撤消作业)*/{free(p);}voidcheck(){JCB*first,*fir,*p;intflag=0,i,test=0;first=pb;while(first&&(T>=first->arrivetime)&&(flag==0)){for(i=0;i<n;i++){if(source[i]>=first->storage[i])source[i]=source[i]-first->storage[i];elsetest=1;}if(test==0){p=first;first=first->link;p->link=NULL;if(ready==NULL)ready=p;else{fir=ready;while(fir->link!=NULL){fir=fir->link;}fir->link=p;}}elseflag=1;}pb=first;}voidfcfs(){JCB*first,*second;intins=0;if((pb==NULL)||(p->arrivetime<pb->arrivetime)){p->link=pb;pb=p;}else{first=pb;second=first->link;while(second!=NULL){if(p->arrivetime<second->arrivetime){p->link=second;second=NULL;first->link=p;ins=1;}else{first=first->link;second=second->link;}}if(ins==0)first->link=p;}}voidrunning(){JCB*pr;inti;printf("正在运行的作业是:%s\n",p->name);disp(p);if(ready!=NULL){printf("就绪队列如下:\n");pr=ready;while(pr!=NULL){disp(pr);pr=pr->link;}}elseprintf("就绪队列为空队列!\n");if(pb!=NULL){printf("后备队列如下:\n");pr=pb;while(pr!=NULL){disp(pr);pr=pr->link;}}elseprintf("后备队列为空队列!\n");printf("作业%s的开始运行时刻T:%4.2f\n",p->name,T);Tc=T+p->needtime;T=Tc;Ti=Tc-p->arrivetime;Wi=Ti/(p->needtime);for(i=0;i<n;i++)source[i]=source[i]+p->storage[i];printf("完成时刻Tc:%4.2f\n",Tc);printf("周转时间Ti:%4.2f\n",Ti);printf("带权周转时间Wi:%4.2f\n",Wi);TiSum+=Ti;WiSum+=Wi;destroy();}main()//主函数{intlen;charch;input();T=pb->arrivetime;check();len=space();while((len!=0)&&(ready!=NULL)){system("pause");p=ready;ready=p->link;p->link=NULL;running();if(pb!=NULL){if(ready==NULL){if(T<pb->arrivetime)T=pb->arrivetime;}check();len=space();}}printf("\n该作业组的平均周转时间:%4.2f\n",TiSum/len);printf("该作业组的带权平均周转时间:%4.2f\n",WiSum/len);ch=getchar();}

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

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

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

×
保存成功