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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

实验2进程调度.docx

1、实验2进程调度实验2:进程调度要求:1、 设计进程控制块PCB的结构,分别适用于优先权调度算法和时间片轮转调度算法。2、 建立进程就绪队列。3、 编制两种进程调度算法:优先权调度(实现动态优先级)和时间片轮转调度。提示:1、 假设利用两种算法对五个进程进行调度,每个进程有运行、就绪、阻塞三种状态,初始状态为就绪态。2、 为了便于处理,程序中的某进程运行时间以时间片为单位计算。各进程的优先数或轮转时间数以及进程需运行的时间片数的初值由用户给定。3、 在优先权调度算法中,优先数可以先取50,进程每执行一次,优先数减3,CPU时间片数加1,进程还需要的时间片数减1。在时间片轮转调度算法中,采用固定时

2、间片,即每执行一次进程,该进程的执行时间片数为已执行了2个单位,这时CPU时间片数加2,进程还需要的时间片数减2,并将该进程排列到就绪队列的队尾。4、 对于优先数一致的情况,采用FIFO策略解决。源程序代码:#include stdio.h#include stdlib.h#include iostream.h#define NULL 0#define false 0#define true 1bool _state=0;struct PCB int ID; int priority; int CPUtime; int ALLtime; int State; PCB* next;void in

3、it(); /*产生idle进程,输入用户进程数目,调用insert()*/void print(PCB *pcb); /*输出进程属性信息*/void print_init(PCB *pcb);/*输出所有PCB的初始值*/void insert(); /*生成进程属性信息,插入进程就绪队列*/void Run_priority(PCB *pcb); /*运行进程,随机阻塞进程、产生新进程,插入就绪队列,唤醒阻塞进程*/void block(PCB *pcb); /*调用destroy()将进程插入阻塞队列*/void wakeup(); /*唤醒进程,插入就绪队列*/void proc_p

4、riority();/*优先权调度算法模拟*/void proc_loop(); /*轮转法调度算法模拟*/void update(PCB *pcb); /*更新进程信息*/void pushback_queue(PCB *queue,PCB *item); /*将item插入到队列的尾部*/void insert_queue(PCB *queue,PCB *item); /*将item插入到队列中,使得插入后,队列中按照优先级从高到低有序*/void sort_queue(PCB *&queue); /*对queue中的结点进行排序,按照优先级从大到小*/PCB *ready_queue,*

5、block_queue,*idleprocess; /*就绪队列,阻塞队列及闲逛进程指针变量*/int main(int argc, char* argv) int i=0; while(1) cout*PROCESS*/; cout(n Please select a num in(1,2,0) ); cout(n 1 - priority ); cout(n 2 - loop ); cout(n 0 - exitn); cout- ; coutendl; couti; while(i) if(i=1) cout(nThis is a example for priority process

6、ing ! n ); init(); proc_priority(); else if (i=2) cout(nThis is a example for round robin processing ! n ); init(); proc_loop(); else coutPlease select a num in(1,2,0)!n; couti; return 0; /输出所有PCB的初始值void print_init(PCB *pcb) PCB* temp=pcb-next ; cout(nID priority CPUtime ALLtime State); while(temp!

7、=NULL) coutnID priority CPUtime ALLtime; if(temp-State=0) coutState =1) cout( running); else coutnext; /输出进程属性信息void print(PCB *pcb) PCB *temp; temp=pcb; if(pcb-ID=0) cout(nThe idle peocess id running!); else coutnID priority CPUtime ALLtime; if(temp-State=0) coutState =1) cout( running); else coutn

8、ext; while(p!=0&p-priority=item-priority) q=p; p=p-next; if(p=0) item-next =0; q-next=item; else item-next =p; q-next =item; /将item插入到阻塞队列的尾部void pushback_queue(PCB *queue,PCB *item) PCB *p,*q; q=queue,p=q-next; while(p!=0) q=p; p=p-next; item-next =q-next ; q-next =item;/对queue中的结点进行排序,按照优先级从大到小voi

9、d sort_queue(PCB *&queue) PCB *temp=new PCB; temp-next =0; while(queue-next ) PCB *p; p=queue-next; queue-next =p-next ; insert_queue(temp,p); queue-next =temp-next ; delete temp;/生成进程属性信息,插入进程就绪队列,显示进程信息void insert() PCB *newp=0; static long id =0; newp=new PCB; id+; newp-ID =id; newp-State=0; newp

10、-CPUtime=0; newp-priority=rand()%3+1; newp-ALLtime=rand()%3+1; newp-next =NULL; pushback_queue(ready_queue,newp); /print(newp); /cout readyn);/生成n个进程属性信息,插入进程就绪队列,显示进程信息void insert(int n) for(int i=0;inext=0; ready_queue=new PCB; ready_queue-next=0; int i=0,pcb_number=-1;/闲逛进程放入就绪队列 idleprocess=NULL

11、; idleprocess=(PCB *)malloc(sizeof(PCB); idleprocess-ID=0; idleprocess-State=0; idleprocess-CPUtime=0; idleprocess-priority=0; idleprocess-ALLtime=0; idleprocess-next=NULL; idleprocess-next=ready_queue-next;/闲逛进程放入就绪队列 ready_queue-next=idleprocess; /也可以假定初始时系统中只有一个idle进程 /输入初始进程的个数 while(pcb_number0

12、) coutpcb_number; cout(nID priority CPUtime ALLtime Staten); for(;ipcb_number;i+) insert(); cout就绪队列初始化成功!endl; :print_init(ready_queue); coutState=2; pcb-CPUtime-=2; if(pcb-CPUtimeCPUtime+=2; coutnThe process noID is blocked!; print(pcb); cout blockedn); pcb-next=block_queue-next; block_queue-next

13、=pcb;/更新进程信息,就绪队列中进程的优先级均增加1void update(PCB *pcb) PCB *temp=pcb-next; while(temp&temp-next ) temp-priority+; temp=temp-next; /唤醒进程,插入就绪队列void wakeup() if(block_queue-next=0)/*此时没有阻塞的进程,无所谓的唤醒*/ return ; PCB *q,*p; while(true) q=block_queue; p=q-next; while(p&rand()%20!=1) q=p; p=p-next; if(p!=0) q-n

14、ext =p-next ; break; p-State=0; coutendl; print(p); cout readyID=0) insert_queue(ready_queue,pcb); print(pcb); cout runningn; else pcb-State=1; pcb-CPUtime+=4; pcb-priority=pcb-priority -3;/*每运行一个时间片,其优先数减3*/ if(pcb-priority priority=1; print(pcb); printf(变迁1: ready - runningn); if(rand()%3=1)/*PCB不

15、是闲逛进程,满足条件侧阻塞此进程*/ if(pcb-CPUtime-2ALLtime) block(pcb); else/*已执行完毕,应该销毁进程*/ coutn; coutThe process noIDDestroyCPUtime=pcb-ALLtime)/*释放*/ coutn; coutThe process no ID DestroryID=0) insert_queue(ready_queue,pcb); print(pcb); cout runningn; else pcb-State=1; pcb-CPUtime=pcb-ALLtime; print(pcb); printf

16、(变迁1: ready - runningn); if(rand()%3=1)/*PCB不是闲逛进程,满足条件侧阻塞此进程*/ _state=1; block(pcb); else coutn; coutThe process no ID Destroryendl; delete pcb; if(rand()%5=1) insert(3); if(rand()%7=1) wakeup();/优先权调度算法模拟void proc_priority() sort_queue(ready_queue); PCB *temp=0,*running=0; int times=0; coutn调度前:;

17、:print_init(ready_queue); for(;timesnext; ready_queue-next =running-next ; coutendl; coutn调度开始:endl; Run_priority(running); coutn本次调度结束。endl;/轮转调度算法模拟void proc_loop() PCB *temp=0,*running=0; int times=10; coutnext; ready_queue-next =running-next ; cout0) times=times-running-ALLtime;/每次运行一个进程减去ALLtim

18、e; if(times=0) Run_loop(running); else if(_state) /如果运行时被阻塞,则运行2个时间单位 times=times+2; _state=0; cout5487584574389574 fgfgfgfgf gfgfg38954378954375894378954375; else pushback_queue(block_queue,running);/时间不过,则阻塞该进程,放到阻塞队列 else if(times=0) coutn本次调度时间片到!; break; coutn本次调度结束。endl;优先权调度(1)输入1选择优先权调度算法模拟。

19、(2)输入开始进程个数n,创建n个PCB并加入就绪队列ready_queue中。(3)就绪队列ready_queue不为空,调度就绪队列中第一个进程运行,否则,从闲逛队列idleprocess中调度闲逛进程运行。(4)在运行过程中,当遇到阻塞,则该进程插入到阻塞队列block_queue中,且将该进程从ready_queue中删除。(5)如果运行时间CPUtime大于等于Alltime,该进程运行完毕,释放该进程;否则插入到就绪队列中。(6)更新就绪队列中的优先级数。(7)随机对阻塞队列block_queue中的进程PCB询问是否要唤醒,唤醒,即从唤醒队列中选择第一个进程,且插入就绪队列中;阻

20、塞队列中没有阻塞进程返回。(8)重复上述步骤,直到本次调度结束。时间片轮转调度(1)输入2选择优先权调度算法模拟。(2)输入开始进程个数n,创建n个PCB并加入就绪队列ready_queue中。(3)就绪队列ready_queue不为空,调度就绪队列中第一个进程运行,否则,从闲逛队列idleprocess中调度闲逛进程运行。(4)在运行过程中,当遇到阻塞,则该进程插入到阻塞队列block_queue中,且将该进程从ready_queue中删除。(5)如果运行时间CPUtime大于等于Alltime,该进程运行完毕,释放该进程;否则插入到就绪队列中。(6)随机对阻塞队列block_queue中的进程PCB询问是否要唤醒,唤醒,即从唤醒队列中选择第一个进程,且插入就绪队列中;阻塞队列中没有阻塞进程返回。(7)如果时间到,本次调度结束,否则重复上述步骤,直到本次调度结束。

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

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