ImageVerifierCode 换一换
格式:DOCX , 页数:18 ,大小:31.32KB ,
资源ID:9795783      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/9795783.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(进程调度算法实验报告.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

进程调度算法实验报告.docx

1、进程调度算法实验报告操作系统实验报告(二)实验题目:进程调度算法实验环境:C+实验目的:编程模拟实现几种常见的进程调度算法,通过对几组进程分别使用不同的调度算法,计算进程的平均周转时间和平均带权周转时间,比较各种算法的性能优劣。实验内容:编程实现如下算法:1.先来先服务算法;2.短进程优先算法;3.时间片轮转调度算法。设计分析:程序流程图:1.先来先服务算法2.短进程优先算法3.时间片轮转调度算法实验代码:1. 先来先服务算法#include #define n 20typedef struct ?int id;? /进程名?int atime;? /进程到达时间?int runtime;?

2、/进程运行时间fcs;void main()?int amount,i,j,diao,huan;? fcs fn;?cout请输入进程个数:amount;?for(i=0;iamount;i+)? cout请输入进程名,进程到达时间,进程运行时间:fi.id;? cinfi.atime;? cinfi.runtime;?for(i=0;iamount;i+)? /按进程到达时间的先后排序? /如果两个进程同时到达,按在屏幕先输入的先运行? for(j=0;jfj+1.atime)? diao=fj.atime;? ?fj.atime=fj+1.atime;? fj+1.atime=diao;?

3、 huan=fj.id;? fj.id=fj+1.id;? fj+1.id=huan;? ? ?for(i=0;iamount;i+)? cout进程:fi.id从fi.atime开始,在? fi.atime+fi.runtime之前结束。endl;? fi+1.atime=fi.atime+fi.runtime;?2. 短进程优先算法#include#define n 5#define num 5#define max 65535typedef struct pro int PRO_ID; int arrive_time; int sum_time; int flag;Pro;/整数排序 i

4、nt bubble(int temp) int i,j,tem=0; for(i=1;inum;i+) int lastX=1; for(j=0;jtempj+1) tem=tempj; tempj=tempj+1; tempj+1=tem; lastX=0; if(lastX=1) break; return temp0; /进程排序 Pro bubble(Pro p) int i,j; Pro temp=0; Pro snum; for(i=0;inum;i+) si=pi; for(i=1;inum;i+) int lastX=1; for(j=0;jsj+1.sum_time) tem

5、p=sj; sj=sj+1; sj+1=temp; lastX=0; if(lastX=1) break; return s0; void SPF(int p) if(n0) int i,j,k,l,tc=0; Pro seqn; Pro temp_seqn; printf(短进程优先调度算法SPFn); printf(请依次输入5个进程的进程号、到达时间和执行时间n); printf(成员变量用逗号隔开;进程间用回车隔开n); for(i=0;in;i+) scanf(%d,%d,%d,&seqi.PRO_ID,&seqi.arrive_time,&seqi.sum_time); print

6、f(调度顺序是:n); /初始化tc int tempnum; for(i=0;inum;i+) tempi=seqi.arrive_time; tc=bubble(temp);/tc是断点啊 /flag 表示对应i的pro的队列情况/-1表示未进入过队列,0表示在队列中,1表示被清除了 for(i=0;in;i+) seqi.flag=-1; for(i=0;in;i+) for(j=0;jn;j+) if(seqj.flag!=1&seqj.arrive_time=tc) seqj.flag=0; for(j=0;jn;j+) temp_seqj=seqj; if(seqj.flag!=0

7、) temp_seqj.sum_time=max; l=bubble(temp_seq).PRO_ID; for(j=0;jn;j+) if(l=seqj.PRO_ID) k=j; tc=tc+bubble(temp_seq).sum_time; seqk.flag=1; printf(%d,l); printf(n); void main() SPF(n);3. 时间片轮转调度算法头文件RR.h#include#include#include#include#include#define MaxNum 100typedef struct pcb /定义进程控制块 char NameMaxNu

8、m; /进程名 int arrivetime; /到达时间 int runtime; /运行时间 int wholetime; /固定运行时间 int FinishTime; /完成时间 double WeightTime; /周转时间 double WeightWholeTime; /带权周转时间 char state; /运行后的状态 struct pcb *next;PCB;/全局变量int N; /实际进程数double SumWT; /周转时间之和double SumWWT; /带权周转时间之和double AverageWT; /平均周转时间double AverageWWT; /

9、平均带权周转时间typedef struct /定义队列,封装头结点,指针分别指向队头和队尾 PCB *front,*rear;queue;queue *init() /进程队列置空 queue *head; head=(queue*)malloc(sizeof(queue); head-front=NULL; head-rear=NULL; return head;int empty(queue *head) /检验队列是否为空 return (head-front?0:1);queue *append(queue *head,char cMaxNum,int a,int r,char s)

10、 /进程队列入队,往后插入 PCB *p; p=(PCB *)malloc(sizeof(PCB); strcpy(p-Name,c); p-arrivetime=a; p-runtime=r; p-wholetime=r; p-state=s; /p-FinishTime=0; /p-WeightTime=0; /p-WeightWholeTime=0; p-next=NULL; if(empty(head) head-front=head-rear=p; else head-rear-next=p; head-rear=p; return head;queue *creat(queue *

11、head) /创建进程队列 char cMaxNum; char s=R; int a,r,i; printf(请输入共有几个进程:n); scanf(%d,&N); for(i=1;ifront; if(!p) printf(时间片轮转调度队列为空!n); while(p) printf(Name=%s arrivetime=%d runtime=%d state=%c,p-Name,p-arrivetime,p-runtime,p-state); printf(n); p=p-next; /*时间片轮转法调度算法的实现*/void RR(queue *head,int q) int t=h

12、ead-front-arrivetime, lt=head-rear-arrivetime; if(head-front-runtimefront-runtime; else t=t+q; /*进程队列为不空才可调度*/ while(!empty(head) PCB *p1,*p2; printf(n时刻 进程 运行后的状态n); /*第一种情况:当前运行的时间小于最后一个进程到达时间做一下操作*/ while(tfront; printf(%2d %s,t,p1-Name); p1-runtime=p1-runtime-q; /1.运行时间小于0,删除队首 if(p1-runtimestat

13、e=C; printf( %cn,p1-state); p1-FinishTime=t; p1-WeightTime=p1-FinishTime-p1-arrivetime; p1-WeightWholeTime=p1-WeightTime/p1-wholetime; SumWT+=p1-WeightTime; SumWWT+=p1-WeightWholeTime; printf(时刻%2d进程%s运行结束,进程%s周转时间=%5.2f,带权周转时间=%5.2fn,t,p1-Name,p1-Name,p1-WeightTime,p1-WeightWholeTime); head-front=p

14、1-next; free(p1); /2.运行时间大于0,向后找位置插入 else printf( %cn,p1-state); p2=p1-next; while(p2-next & p2-arrivetime != t) p2=p2-next; /此时无新进入队列的进程,有两种情况:1.不用找位置往后插入,队首不变,不做操作 /2.找位置往后插入 if(p2-arrivetime != t) PCB *p3=p1,*p4; while(p3-next & p3-arrivetimenext; if(p3-arrivetimet) if(p4!=p1) /p1插在p4后,头为p1-next

15、head-front=p1-next; p1-next=p4-next; p4-next=p1; else /不做操作 p4=p3=p2=NULL; else p4=p3=p2=NULL; /此时有新进入队列的进程时:p1插在新进入队列的进程p2后,队首为p1-next else head-front=p1-next; p1-next=p2-next; p2-next=p1; /时刻变化 if(head-front-runtimefront-runtime; else t=t+q; /*第一种情况结束*/ /*第二种情况:当期运行的时间大于最后一个进程到达的时间做以下操作*/ while(t=

16、lt) p1=head-front; printf(%2d %s,t,p1-Name); p1-runtime=p1-runtime-q; /1.运行时间小于0,删除队首 if(p1-runtimestate=C; printf( %cn,p1-state); p1-FinishTime=t; p1-WeightTime=p1-FinishTime-p1-arrivetime; p1-WeightWholeTime=p1-WeightTime/p1-wholetime; SumWT+=p1-WeightTime; SumWWT+=p1-WeightWholeTime; printf(时刻%2d

17、进程%s运行结束,进程%s周转时间=%5.2f,带权周转时间=%5.2fn,t,p1-Name,p1-Name,p1-WeightTime,p1-WeightWholeTime); /printf(时刻%2d进程%s运行结束,t,p1-pname); head-front=p1-next; free(p1); /2.运行时间大于0,直接插在队尾 else printf( %cn,p1-state); /若原队列只有一个进程,不必往队尾插 if(!p1-next) head-front=p1; /若原队列有多个进程 else head-front=p1-next; head-rear-next=

18、p1; head-rear=p1; p1-next=NULL; /时刻变化,队列为空时不做时刻变化 if(empty(head) return; else if(head-front-runtimefront-runtime; else t=t+q; /*第二种情况结束*/ 主程序Main.cpp#include#include#include#include#include#include RR.hvoid main() queue *head; int q; head=init(); head=creat(head); printf(n您输入的时间片轮转进程队列为:n); print(he

19、ad); printf(n请输入时间片轮转调度的时间片为:); scanf(%d,&q); /时间片轮转调度 RR(head,q); AverageWT=SumWT/N; AverageWWT=SumWWT/N; printf(平均周转时间=%5.2f,平均带权周转时间=%5.2f,AverageWT,AverageWWT);运行结果:先来先服务:短进程:时间片轮:心得体会:这次的实验与上次的实验相比,在很多方面都有更多的难度,所以我们参考了别人很多的程序然后稍作了修改。但是由于自身知识不够,所以没能将三个算法都弄到一个大程序中,只能通过三个程序来实现,这一点是我们的不足。虽然如此,我们还是有了一定的收获,比如更加深刻了解到了先来先服务、短进程、时间片轮这三种作业的原理,而且这一过程中我们觉得时间片轮调度算法更具优势。

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1