您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 销售管理 > 先来先服务调度和最短作业优先调度算法实验报告
实验概述:【实验目的及要求】理解并掌握处理机调度算法【实验原理】基于先来先服务调度和最短作业优先调度算法思想用C语言编程实现【实验环境】(使用的软件)VisualC++6.0实验内容:本实验模拟在单处理机情况下处理机调度,用C语言编程实现先来先服务和最短作业优先调度算法。【实验方案设计】FCFS流程图:SJF流程图:试验总设计流程图:进程等待时间=进程开始运行时间-进程提交时间(即进程处于就绪态时间)进程周转时间=进程结束时间-进程提交时间【实验过程】(实验步骤、记录、数据、分析)测试用例1:屏幕显示:Pleaseinputthetotalnumberofjobs输入:4回车屏幕显示:Pleaseinputjobnumber,submittimeandruntime输入:19.00.2回车28.50.5回车38.01.0回车49.10.1回车屏幕显示:Whatkindofalgorithmdoyouwant?Pleaseinput1toselectFCFS,or2toselectSJF.输入:3回车屏幕显示:Youhaveinputawrongnumber,pleaseinputagain.输入:1回车屏幕输出结果:submitrunstartingfinalwaitturnaround38.01.08.09.00.01.028.50.59.09.50.51.0其他数输入选择数以选择调度算法SJF算法2FCFS算法1按照提交时间排序先按提交时间排序计算各作业参数:开始时间,结束时间等待时间,周转时间并输出并输出报错继续输数计算各作业参数:开始时间,结束时间等待时间,周转时间并输出再按最短运行时间排序输入作业个数、作业号、提交时间、运行时间19.00.29.59.70.50.749.10.19.79.80.60.7屏幕显示:Theaverageturnaroundtimeis0.85Whatkindofalgorithmdoyouwant?Pleaseinput1toselectFCFS,or2toselectSJF,or0toexit.测试数据二:submitrun160.5250.936.30.1FCFS和SJF算法结果一样:submitrunstartingfinalwaitturnaround250.955.900.9160.566.500.536.30.16.56.60.20.3Theaverageturnaroundtimeis0.567测试数据三:submitrun150.224.20.335.10.345.20.1FCFS:submitrunstartingfinalwaitturnaround24.20.34.24.500.3150.255.200.235.10.35.25.50.10.445.20.15.55.60.30.4Theaverageturnaroundtimeis0.325SJF:submitrunstartingfinalwaitturnaround24.20.34.24.500.3150.255.200.245.20.15.25.300.135.10.35.35.60.20.5Theaverageturnaroundtimeis0.275源程序:#includestdio.h#includestdlib.h#includestring.h#defineM50structsjf{intjobnumber;floatsubmittime;floatruntime;floatstarttime;floatfinishtime;floatwaittime;floatturnaroundtime;}temp;staticstructsjfst[M];voidinput(structsjf*p,intN){inti;printf(Pleaseinputthejobnumber,submittimeandruntime:\nForexmple:18.52.0\n);for(i=0;iN;i++){scanf(%d%f%f,&p[i].jobnumber,&p[i].submittime,&p[i].runtime);}}voidprint(structsjf*p,intN){intk;floath,g;printf(runorder:);printf(%d,p[0].jobnumber);for(k=1;kN;k++)printf(--%d,p[k].jobnumber);printf(\nTheprocess'sinformation:\n);printf(\njobnum\tsubmit\trun\tstart\tfinal\twait\tturnaround\n);for(k=0;kN;k++){h+=p[k].turnaroundtime;printf(%d\t%-.1f\t%-.1f\t%-.1f\t%-.1f\t%-.1f\t%-.1f\t\n,p[k].jobnumber,p[k].submittime,p[k].runtime,p[k].starttime,p[k].finishtime,p[k].waittime,p[k].turnaroundtime);}g=h/N;printf(\nTheaverageturnaroundtimeis%-.2f\n,g);}/*按提交时间从小到大排序*/voidsort1(structsjf*p,intN){inti,j;for(i=0;iN;i++)for(j=0;j=i;j++)if(p[i].submittimep[j].submittime){temp=p[i];p[i]=p[j];p[j]=temp;}}/*运行*/voiddeal(structsjf*p,intN){intk;for(k=0;kN;k++){if(k==0){p[k].starttime=p[k].submittime;p[k].finishtime=p[k].submittime+p[k].runtime;}else{if(p[k].submittimep[k-1].finishtime){p[k].starttime=p[k].submittime;p[k].finishtime=p[k].submittime+p[k].runtime;}else{p[k].starttime=p[k-1].finishtime;p[k].finishtime=p[k-1].finishtime+p[k].runtime;}}}for(k=0;kN;k++){p[k].turnaroundtime=p[k].finishtime-p[k].submittime;p[k].waittime=p[k].starttime-p[k].submittime;}}voidsort2(structsjf*p,intN){intnext,m,n,k,i;floatmin;sort1(p,N);for(m=0;mN;m++){i=0;if(m==0)p[m].finishtime=p[m].submittime+p[m].runtime;else{if(p[m].submittimep[m-1].finishtime){p[m].finishtime=p[m].submittime+p[m].runtime;}elsep[m].finishtime=p[m-1].finishtime+p[m].runtime;}for(n=m+1;nN;n++){if(p[n].submittime=p[m].finishtime)/*判断内存中每次完成之后又多少到达的进程*/i++;}min=p[m+1].runtime;next=m+1;for(k=m+1;km+i;k++)/*找出到达后的进程中运行时间最小的进程*/{if(p[k+1].runtimemin){min=p[k+1].runtime;next=k+1;}}temp=p[m+1];p[m+1]=p[next];p[next]=temp;}deal(p,N);print(p,N);}voidmain(){intN,i;printf(Pleaseinputthetotalnumberofjobs:);scanf(%d,&N);input(st,N);loop2:printf(Whatkindofalgorithmdoyouwant?Pleaseinput1toselectFCFS,or2toselectSJFor0toexit:);loop:scanf(%d,&i);if(i==0)exit(1);elseif(i==1){sort1(st,N);deal(st,N);print(st,N);gotoloop2;}elseif(i==2){sort2(st,N);gotoloop2;}else{printf(Youhaveinputawrongnumber,pleaseinputagain:);gotoloop;}}【结论】(结果)测试1:测试二:测试三:【小结】实验中产生的错误及原因分析:测试用例1的结果:错误1:错误解决方式:主要是子函数sort2()中出的错:i的作用域,程序修改:将原来:intnext,m,n,k,i=0;floatmin;sort1(p,N);for(m=0;mN;m++){改为:intnext,m,n,k,i;floatmin;sort1(p,N);for(m=0;mN;m++){i=0;测试用例2的结果:错误1:错误原因:未考虑到“提交时间(submittime)大于上个进程的结束时间”的情况:解决方法:将原来的:else{p[m].finishtime=p[m-1].finishtime+p[m].runtime;}修改为:else{if(p[m].submittimep[m-1].finishtime){p[m].finishtime=p[m].submittime+p[m].runtime;}elsep[m].finishtime=p[m-1].finishtime+p[m].runtime;}测试用例3的结果:错误1:错误分析:同2一样,未在子函数sort()中未考虑到“提交时间(submittime)大于上个进程的结束时间”的情况:解决方法:将原来的:elsep[m].finishtime=p[m-1].finishtime+p[m].runtime;修改为:else{if(p[m].submittimep[m-1].finishtime){p[m].finishtime=p[m].submittime+p[m].runtime;}elsep[m].finishtime=p[m-1].finishtime+p[m].runtime;}实验的体会及收获:通过这次试验,我对处理机的调度算法特别是FCFS和SJF有了更深的理解,而且锻炼了我的思维能力,使我能更全面地思考问题,以后还需要多做些这方面的练习。试验不足之处:试验未考虑同一时间提交多个进程的情况,如:测试数据:submitrun170.227.20.5370.1结果应该是:FCFS:submitrunstartfinalwaitturnaround170.27.07.50.00.5370.17.57.60.50.627.20.57.67.80.40.6Theaverageturnaroundtimeis0.57.SJF:Jobnumsubmitrunstartingfinalwaitturnaround370.17.07.10.00.1170.57.17.60.10.627.20.27.67.80.60.6Theaverageturnaroundtimeis0.43.而程序运行结果是:程序结果表明该程序还是存在不足之处的。这是需要继续改进的地方。指导教师评语及成绩:评语:成绩
本文标题:先来先服务调度和最短作业优先调度算法实验报告
链接地址:https://www.777doc.com/doc-1840098 .html