操作系统实验(2)作业调度实验

上传人:仙*** 文档编号:33210317 上传时间:2021-10-16 格式:DOC 页数:34 大小:477KB
收藏 版权申诉 举报 下载
操作系统实验(2)作业调度实验_第1页
第1页 / 共34页
操作系统实验(2)作业调度实验_第2页
第2页 / 共34页
操作系统实验(2)作业调度实验_第3页
第3页 / 共34页
资源描述:

《操作系统实验(2)作业调度实验》由会员分享,可在线阅读,更多相关《操作系统实验(2)作业调度实验(34页珍藏版)》请在装配图网上搜索。

1、 操作系统实验(2) 实验题目:作 业 调 度 实 验课程名称:操 作 系 统 学 院: 专业班级: 学 号(1): 姓 名(1): 学 号(2): 姓 名(2): 任课教师: 2010年4月12日学院:管 理 学 院 班级: 组员: 组员: 评定: 实验题目:作业调度实验(1) 实验目的:用高级语言编写和调试一个或多个作业调度的模拟程序,以加深对作业调度算法的理解。(2)实验内容:编写并调试一个单道处理系统的作业等待模拟程序。作业等待算法:分别采用先来先服务(FCFS),最短作业优先(SJF)、响应比高者优先(HRN)的调度算法。对每种调度算法都要求打印每个作业开始运行时刻、完成时刻、周转时

2、间、带权周转时间,以及这组作业的平均周转时间及带权平均周转时间,以比较各种算法的优缺点。(3) 源代码(含注释):先来先服务(FCFS):#include stdio.h #include #include #define getpch(type) (type*)malloc(sizeof(type) #define NULL 0 struct jcb /* 定义作业控制块PCB */ char name10; char state; int ttime; int ntime; int rtime; int otime; int etime; float zztime; float dqtim

3、e; struct jcb* link; *ready=NULL,*p;int num,n;int srtime=0,ertime=1;float pztime=0.0,pqtime=0.0; typedef struct jcb JCB; void sort() /* 建立对进程进行提交时间排列函数*/ JCB *first, *second; int insert=0; if(ready=NULL)|(p-ttime)ttime) /*提交时间最大者,插入队首*/ p-link=ready; ready=p; else /* 进程比较提交时间,插入适当的位置中*/ first=ready;

4、 second=first-link; while(second!=NULL) if(p-ttime)ttime) /*若插入进程比当前提交时间早*/ /*插入到当前进程前面*/ p-link=second; first-link=p; second=NULL; insert=1; else /* 插入进程提交时间最晚,则插入到队尾*/ first=first-link; second=second-link; if(insert=0) first-link=p; void pl() JCB *pr; pr=ready; if(p-link!=NULL) if(ready-ttimelink!

5、=NULL) pr=pr-link; pr-link=p; p-link=NULL; p=ready; ready=p-link; else ready=p; void input() /* 建立进程控制块函数*/ int i; clrscr(); /*清屏*/ printf(n input the number of process?); scanf(%d,&num); n=num; for(i=0;iname); printf(n input the submit time :); scanf(%d,&p-ttime); printf(n input the processtime nee

6、d to run:); scanf(%d,&p-ntime); printf(n); p-rtime=0;p-state=w; p-link=NULL; sort(); /* 调用sort函数*/ void disp(JCB * pr) /*建立进程显示函数,用于显示当前进程*/ printf(n qname t state t ndtime t runtime t ttime n ); printf(|%st,pr-name); printf(|%ct,pr-state); printf(|%dt,pr-ntime); printf(|%dt,pr-rtime); void check()

7、/* 建立进程查看函数 */ JCB* pr; printf(n * now the running process is:%s,p-name); /*显示当前运行进程*/ disp(p); printf(|%dt,p-ttime); pr=ready; printf(n *the ready team are:n); /*显示就绪队列状态*/ while(pr!=NULL)&(p-link!=NULL) if(pr-ttimelink; void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/ num-; p-etime=ertime; p-zztime=ertime

8、-p-ttime; p-dqtime=p-zztime/p-ntime; pztime=pztime+p-zztime; pqtime=pqtime+p-dqtime; printf(n process %s have been achieved.n,p-name); printf(turnaround time is %-3.1f,turnaround time with the right is %-3.1f,p-zztime,p-dqtime); free(p); if(p-link=NULL)ready=NULL; if(ready-link!=NULL) p=ready; ready

9、=p-link; elsep=ready; void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态)*/ if(p-ttimertime)+; check(); srtime+; ertime+; pl(); if(p-rtime=p-ntime) destroy(); /* 调用destroy函数*/ else p-state=w; else printf(the process isnt come into merroy,wait the next time); srtime+; ertime+; void main() /*主函数*/ int h=0; char

10、 ch; input(); p=ready; ready=p-link; while(num!=0)&(ready!=NULL) ch=getchar(); h+; printf(n The execute number:%d n,h); p-state=R; running(); printf(n press any key to continue.); ch=getchar(); pztime=pztime/n; pqtime=pqtime/n; printf(nn process have been achieved.n); printf(the average turnaround t

11、ime is %-3.1f,the average turnaround time with right is %-3.1fn,pztime,pqtime); ch=getchar(); 最短作业优先(SJF):#include stdio.h #include #include #define getpch(type) (type*)malloc(sizeof(type) #define NULL 0 struct jcb /* 定义作业控制块PCB */ char name10; char state; int ttime; int ntime; int rtime; int otime;

12、 int etime; float zztime; float dqtime; struct jcb* link; *ready=NULL,*p;int num,n;int srtime=0,ertime=1;float pztime=0.0,pqtime=0.0; typedef struct jcb JCB; void sort() /* 建立对进程进行提交时间排列函数*/ JCB *first, *second; int insert=0; if(ready=NULL)|(p-ttime)ttime) /*提交时间最大者,插入队首*/ p-link=ready; ready=p; els

13、e /* 进程比较提交时间,插入适当的位置中*/ first=ready; second=first-link; while(second!=NULL) if(p-ttime)ttime) /*若插入进程比当前提交时间早*/ /*插入到当前进程前面*/ p-link=second; first-link=p; second=NULL; insert=1; else /* 插入进程提交时间最晚,则插入到队尾*/ first=first-link; second=second-link; if(insert=0) first-link=p; void pl() JCB *pr1,*pr2,*prx

14、,*pry; float x,y; prx=p; pry=prx-link; x=(srtime-p-otime)+(p-ntime); y=x/p-ntime; if(p-link!=NULL) while(pry!=NULL) if(yntime-pry-rtime) y=pry-ntime-pry-rtime;pr1=prx;pr2=pry; prx=prx-link;pry=prx-link; if(prx!=p&pr2-ttimelink=pr2-link;pr2-link=p;p=pr2;ready=p-link; else ready=p; void input() /* 建立进

15、程控制块函数*/ int i; clrscr(); /*清屏*/ printf(n input the number of process?); scanf(%d,&num); n=num; for(i=0;iname); printf(n input the submit time :); scanf(%d,&p-ttime); printf(n input the processtime need to run:); scanf(%d,&p-ntime); printf(n); p-rtime=0;p-state=w;p-otime=0; p-link=NULL; sort(); /* 调

16、用sort函数*/ void disp(JCB * pr) /*建立进程显示函数,用于显示当前进程*/ printf(n qname t state t ndtime t runtime t ttime n ); printf(|%st,pr-name); printf(|%ct,pr-state); printf(|%dt,pr-ntime); printf(|%dt,pr-rtime); void check() /* 建立进程查看函数 */ JCB* pr; p-otime=srtime; printf(n * now the running process is:%s,p-name);

17、 /*显示当前运行进程*/ disp(p); printf(|%dt,p-ttime); pr=ready; printf(n *the ready team are:n); /*显示就绪队列状态*/ while(pr!=NULL)&(p-link!=NULL) if(pr-ttimestate=w; disp(pr); pr=pr-link; void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/ num-; p-etime=ertime; p-zztime=ertime-p-ttime; p-dqtime=p-zztime/p-ntime; pztime=pzti

18、me+p-zztime; pqtime=pqtime+p-dqtime; printf(n process %s have been achieved.n,p-name); printf(turnaround time is %-3.1f,turnaround time with the right is %-3.1f,p-zztime,p-dqtime); free(p); if(p-link=NULL) ready=NULL;if(ready-link!=NULL) p=ready; ready=p-link; elsep=ready; void running() /* 建立进程就绪函数

19、(进程运行时间到,置就绪状态)*/ if(p-ttimertime)+; check(); srtime+; ertime+; pl(); if(p-rtime=p-ntime) destroy(); /* 调用destroy函数*/ else p-state=w; else printf(the process isnt come into merroy,wait the next time); srtime+; ertime+; void main() /*主函数*/ int h=0; char ch; input(); p=ready; ready=p-link; while(num!=

20、0)&(ready!=NULL) ch=getchar(); h+; printf(n The execute number:%d n,h); p-state=R; running(); printf(n press any key to continue.); ch=getchar(); pztime=pztime/n; pqtime=pqtime/n; printf(nn process have been achieved.n); printf(the average turnaround time is %-3.1f,the average turnaround time with r

21、ight is %-3.1fn,pztime,pqtime); ch=getchar(); 响应比高者优先(HRN):#include stdio.h #include #include #define getpch(type) (type*)malloc(sizeof(type) #define NULL 0 struct jcb /* 定义作业控制块PCB */ char name10; char state; int ttime; int ntime; int rtime; int otime; int etime; float zztime; float dqtime; struct

22、jcb* link; *ready=NULL,*p;int num,n;int srtime=0,ertime=1;float pztime=0.0,pqtime=0.0; typedef struct jcb JCB; void sort() /* 建立对进程进行提交时间排列函数*/ JCB *first, *second; int insert=0; if(ready=NULL)|(p-ttime)ttime) /*提交时间最大者,插入队首*/ p-link=ready; ready=p; else /* 进程比较提交时间,插入适当的位置中*/ first=ready; second=fi

23、rst-link; while(second!=NULL) if(p-ttime)ttime) /*若插入进程比当前提交时间早*/ /*插入到当前进程前面*/ p-link=second; first-link=p; second=NULL; insert=1; else /* 插入进程提交时间最晚,则插入到队尾*/ first=first-link; second=second-link; if(insert=0) first-link=p; void pl() JCB *pr1,*pr2,*prx,*pry; int x; prx=p; pry=prx-link; x=(p-ntime)-

24、(p-rtime); if(p-link!=NULL) while(pry!=NULL) if(xpry-ntime-pry-rtime) x=pry-ntime-pry-rtime;pr1=prx;pr2=pry; prx=prx-link;pry=prx-link; if(prx!=p&pr2-ttimelink=pr2-link;pr2-link=p;p=pr2;ready=p-link; else ready=p;void input() /* 建立进程控制块函数*/ int i; clrscr(); /*清屏*/ printf(n input the number of proces

25、s?); scanf(%d,&num); n=num; for(i=0;iname); printf(n input the submit time :); scanf(%d,&p-ttime); printf(n input the processtime need to run:); scanf(%d,&p-ntime); printf(n); p-rtime=0;p-state=w; p-link=NULL; sort(); /* 调用sort函数*/ void disp(JCB * pr) /*建立进程显示函数,用于显示当前进程*/ printf(n qname t state t n

26、dtime t runtime t ttime n ); printf(|%st,pr-name); printf(|%ct,pr-state); printf(|%dt,pr-ntime); printf(|%dt,pr-rtime); void check() /* 建立进程查看函数 */ JCB* pr; printf(n * now the running process is:%s,p-name); /*显示当前运行进程*/ disp(p); printf(|%dt,p-ttime); pr=ready; printf(n *the ready team are:n); /*显示就绪

27、队列状态*/ while(pr!=NULL)&(p-link!=NULL) if(pr-ttimestate=w; disp(pr); pr=pr-link; void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/ num-; p-etime=ertime; p-zztime=ertime-p-ttime; p-dqtime=p-zztime/p-ntime; pztime=pztime+p-zztime; pqtime=pqtime+p-dqtime; printf(n process %s have been achieved.n,p-name); printf(

28、turnaround time is %-3.1f,turnaround time with the right is %-3.1f,p-zztime,p-dqtime); free(p); if(p-link=NULL)ready=NULL; if(ready-link!=NULL) p=ready; ready=p-link; elsep=ready; void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态)*/ if(p-ttimertime)+; check(); srtime+; ertime+; pl(); if(p-rtime=p-ntime) dest

29、roy(); /* 调用destroy函数*/ else p-state=w; else printf(the process isnt come into merroy,wait the next time); srtime+; ertime+; void main() /*主函数*/ int h=0; char ch; input(); p=ready; ready=p-link; while(num!=0)&(ready!=NULL) ch=getchar(); h+; printf(n The execute number:%d n,h); p-state=R; running();

30、printf(n press any key to continue.); ch=getchar(); pztime=pztime/n; pqtime=pqtime/n; printf(nn process have been achieved.n); printf(the average turnaround time is %-3.1f,the average turnaround time with right is %-3.1fn,pztime,pqtime); ch=getchar(); (4) 每个函数的流程图:先来先服务(FCFS):1. void sort()函数:该函数用于对

31、进程进行提交时间排列。 insert=0开始 ready是否为空或者(p-ttime)是否小于(ready-ttime)? 是 否 first=ready;second=first-link进程比较提交时间,插入适当的位置中 p-link=ready;ready=p即提交时间最大者插入队首second是否为空? 否 是(p-ttime)是否小于(second-ttime)insert是否为0 是 否 是 first=first-link second=second-link插入进程提交时间最晚,则插入到队尾first-link=pp-link=second;first-link=p secon

32、d=NULL;insert=1若插入进程比当前提交时间早,则插入到当前进程前面结束2. void pl()函数开始pr=readyp-link是否为空? 否 是ready-ttime是否小于等于srtimeready=p 是 否 pr-link是否为空是 pr-link=pp-link=NULL p=readyready=p-linkpr=pr-link结束3. void input()函数:该函数用于建立进程控制块。开始清屏输入进程数目n=numi是否小于num 是 否p=getpch(JCB)结束输入进程名、提交时间和进程所需运行时间 p-rtime=0 p-state=w;p-link=

33、NULL调用sort函数4. void disp(JCB * pr)函数:建立进程显示函数,用于显示当前进程。开始显示当前进程(进程名、进程状态、所需要的时间、运行时间和提交时间)结束5. void check()函数:该函数用于进程查看。开始显示当前运行进程显示就绪队列状态pr和p-link是否同时为空 否 是pr-ttime是否小于等于srtime 否 是 disp(pr)pr=pr-link结束开始6. void destroy()函数:建立进程撤消函数(进程运行结束,撤消进程)。 num- p-etime=ertime p-zztime=ertime-p-ttimep-dqtime=p

34、-zztime/p-ntime pztime=pztime+p-zztime pqtime=pqtime+p-dqtime 进程已经完成输出周转时间和带权周转时间释放pp-link是否为空? 是 否ready=NULLready-link是否为空?否 是p=ready p=readyready=p-link结束7. void running()函数:建立进程就绪函数(进程运行时间到,置就绪状态)。开始p-ttime是否小于等于srtime 是 否进程未进入内存,等待下一时间(p-rtime)+ check() srtime+ ertime+调用pl()函数srtime+ertime+p-rti

35、me是否等于p-ntime?是 否p-state=w调用destroy函数结束8. 主函数开始 h=0 input() p=readyready=p-linknum是否为0且同时ready是否为空? 否 是pztime=pztime/npqtime=pqtime/n ch=getchar() h+ 进程完成输出平均周转时间和平均带权周转时间p-state=R running()按任意键继续ch=getchar()ch=getchar()结束最短作业优先(SJF):1. void sort()函数:该函数用于对进程进行提交时间排列。开始inser=0ready是否为空或者(p-ttime)是否小

36、于(ready-ttime)? 是 否 first=ready;second=first-link进程比较提交时间,插入适当的位置中 p-link=ready;ready=p即提交时间最大者插入队首second是否为空? 否 是(p-ttime)是否小于(second-ttime)insert是否为0 是 否 是 first=first-link second=second-link插入进程提交时间最晚,则插入到队尾first-link=pp-link=second;first-link=p second=NULL;insert=1若插入进程比当前提交时间早,则插入到当前进程前面结束2. vo

37、id pl()函数开始 prx=p pry=prx-linkx=(srtime-p-otime)+(p-ntime) y=x/p-ntimep-link是否为空? 否 是pry是否为空?ready=p 否 是y是否小于等于pry-ntime-pry-rtimeprx!=p&pr2-ttimentime-pry-rtime pr1=prx pr2=pry否pr1-link=pr2-link pr2-link=p p=pr2 ready=p-linkprx=prx-linkpry=prx-link结束3. void input()函数:该函数用于建立进程控制块。开始清屏输入进程数目n=numi是否

38、小于num 是 否p=getpch(JCB)结束输入进程名、提交时间和进程所需运行时间 p-rtime=0 p-state=wp-otime=0p-link=NULL调用sort函数4. void disp(JCB * pr)函数:建立进程显示函数,用于显示当前进程。开始显示当前进程(进程名、进程状态、所需要的时间、运行时间和提交时间)结束5. void check()函数:该函数用于进程查看。开始显示当前运行进程显示就绪队列状态pr和p-link是否同时为空 否 是pr-ttime是否小于等于srtime 否 是pr-state=w disp(pr)pr=pr-link结束6. void d

39、estroy()函数:建立进程撤消函数(进程运行结束,撤消进程)。开始 num- p-etime=ertime p-zztime=ertime-p-ttimep-dqtime=p-zztime/p-ntime pztime=pztime+p-zztime pqtime=pqtime+p-dqtime 进程已经完成输出周转时间和带权周转时间释放pp-link是否为空? 是 否ready=NULLready-link是否为空?否 是p=ready p=readyready=p-link结束7. void running()函数:建立进程就绪函数(进程运行时间到,置就绪状态)。开始p-ttime是否小于等于srtime 是 否进程未进入内存,等待下一时间(p-rtime)+ check() srtime+ ertime+调用pl()函数srtime+ertime+p-rtime是否等于p-ntime?是 否p-state=w调用destroy函数结束8. 主函数开始 h=0 input() p=readyready=p-linknum是否为0且同时ready是否为空? 否 是pztime=pztime/npqtime=pqtime/n ch=getchar() h+ 进程完成输出平均周转时间和平均带权周转时间p-state=R running()按任意键继续ch=getchar()

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!