作业调度算法

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

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

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

资源描述

实验4作业调度算法学院:信息工程学院班级:计算机10-2班学号:1005120201姓名:常大财实验目的:1)加深对作业概念的理解。2)理解操作系统中调度的概念和调度算法。3)深入理解操作系统中如何组织、管理和调度作业,如何协调和控制各作业对CPU的使用。实验准备:以自己的用户名进入LINUX操作系统。会用vi编辑文本文件。在PC机上安装C语言集成开发环境。熟悉C语言编程。理解作业调度算法的基本思想。实验内容:编写程序完成批处理系统中的作业调度,要求分别采用先来先服务算法(FCFS)、短作业优先算法(SJF)和高响应比优先算法(HRN)。实验具体包括:首先确定作业控制块的内容,作业控制块的组织方式;然后完成作业调度;最后编写主函数对所做工作进行测试,并对程序的运行结果进行分析。源代码:#includestdio.h#definegetjcb(type)(type*)malloc(sizeof(type))#defineNULL0#includeconio.h#includemalloc.hintn=0,time=0;floateti,ewi;structjcb{charname[10];charstate;intts;floatsuper;inttb;inttc;floatti;floatwi;intntime;charresource[10];structjcb*link;}*p,*q,*head=NULL;typedefstructjcbJCB;voidinital(){inti;printf(\nInputjcbnum\n);scanf(%d,&n);printf(Input\nname\tts\tntime\tresource\n);for(i=0;in;i++){p=getjcb(JCB);scanf(%s\t%d\t%d\t%s,&p-name,&p-ts,&p-ntime,&p-resource);p-state='W';p-link=NULL;if(head==NULL)head=q=p;else{q-link=p;q=p;}}}voidprint(JCB*pr,intm){JCB*p;printf(\ntime=%d,time);if(m==3){printf(\nname\tstate\tts\tntime\tsuper\tsource\ttb\ttc\tti\twi\n);printf(%s\t%c\t%d\t%d\t%4.2f\t%s\t%d\t%d\t%4.2f\t%4.2f\n,pr-name,pr-state,pr-ts,pr-ntime,pr-super,pr-resource,pr-tb,pr-tc,pr-ti,pr-wi);}else{printf(\nname\tstate\tts\tntime\tsource\ttb\ttc\tti\twi\n);printf(%s\t%c\t%d\t%d\t%s\t%d\t%d\t%4.2f\t%4.2f\n,pr-name,pr-state,pr-ts,pr-ntime,pr-resource,pr-tb,pr-tc,pr-ti,pr-wi);}p=head;do{if(p-state=='W')if(m==3){printf(%s\t%c\t%d\t%d\t%4.2f\t%s\n,p-name,p-state,p-ts,p-ntime,p-super,p-resource);}else{printf(%s\t%c\t%d\t%d\t%s\n,p-name,p-state,p-ts,p-ntime,p-resource);}p=p-link;}while(p!=NULL);p=head;do{if(p-state=='F')if(m==3){printf(%s\t%c\t%d\t%d\t%4.2f\t%s\t%d\t%d\t%4.2f\t%4.2f\n,p-name,p-state,p-ts,p-ntime,p-super,p-resource,p-tb,p-tc,p-ti,p-wi);}else{printf(%s\t%c\t%d\t%d\t%s\t%d\t%d\t%4.2f\t%4.2f\n,p-name,p-state,p-ts,p-ntime,p-resource,p-tb,p-tc,p-ti,p-wi);}p=p-link;}while(p!=NULL);}voidrunning(JCB*p,intm);voidfcfs(intm){inti,iden;printf(\n\nthejcbisruning...);for(i=0;in;i++){p=head;iden=1;do{if(p-state=='W'&&p-ts=time)iden=0;if(iden)p=p-link;}while(p!=NULL&&iden);if(iden){i--;printf(\ntime=%d:\tnoJCBsubmib...wait...,time);time++;if(time100){printf(\nruntimeistoolong...error);getch();}}else{running(p,m);}}}voidsjf(intm){JCB*min;inti,iden;for(i=0;in;i++){p=min=head;iden=1;do{if(p-state=='W'&&p-ts=time)if(iden){min=p;iden=0;}elseif(p-ntimemin-ntime)min=p;p=p-link;}while(p!=NULL);if(iden){i--;printf(\ntime=%d:\tnoJCBsubmib...wait...,time);time++;if(time100){printf(\nruntimeistoolong...error);getch();}}else{running(min,m);}}}voidsuper();voidhrn(intm){JCB*min;inti,iden;for(i=0;in;i++){p=min=head;iden=1;super();do{if(p-state=='W'&&p-ts=time)if(iden){min=p;iden=0;}elseif(p-supermin-super)min=p;p=p-link;}while(p!=NULL);if(iden){i--;time++;printf(\ntime=%d:\tnoJCBsubmib...wait...,time);if(time1000){printf(\nruntimeistoolong...error...);getch();}}else{running(min,m);}}}voidsuper(){JCB*padv;padv=head;do{if(padv-state=='W'&&padv-ts=time)padv-super=(float)(time-padv-ts+padv-ntime)/padv-ntime;padv=padv-link;}while(padv!=NULL);}voidlast(){eti/=n;ewi/=n;printf(\neti=%7.3f\tewi=%7.3f\n,eti,ewi);}voidrunning(JCB*p,intm){p-tb=time;p-state='R';p-tc=p-tb+p-ntime;p-ti=(float)(p-tc-p-ts);p-wi=(float)(p-ti/p-ntime);eti+=p-ti;ewi+=p-wi;print(p,m);time+=p-ntime;p-state='F';printf(\n%shasbeenfinished!\npressanykeytocontinue...\n,p-name);getch();}intstart(){intm;charstr[100]=\nselectalgorithm\n1.FCFS\n2.SJF\n3.HRN\n4.EXIT\n;printf(%s,str);m=getch()-48;switch(m){case1:printf(\n\nstartrunningjob...);fcfs(m);break;case2:printf(\n\nstartrunningjob...);sjf(m);break;case3:printf(\n\nstartrunningjob...);hrn(m);break;case4:return0;default:printf(\n\nselecterror!tryagain...\n);start();}return0;}intmain(){inital();start();last();printf(\nfinished!);getch();return0;}运行结果:Inputjcbnum1Inputnametsntimeresourceb1206selectalgorithm1.FCFS2.SJF3.HRN4.EXITstartrunningjob...thejcbisruning...time=0:noJCBsubmib...wait...time=1namestatetsntimesourcetbtctiwibR120612120.001.00bhasbeenfinished!pressanykeytocontinue...

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

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

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

×
保存成功