操作系统实验银行家算法Word格式.docx

上传人:b****4 文档编号:16540724 上传时间:2022-11-24 格式:DOCX 页数:17 大小:111.36KB
下载 相关 举报
操作系统实验银行家算法Word格式.docx_第1页
第1页 / 共17页
操作系统实验银行家算法Word格式.docx_第2页
第2页 / 共17页
操作系统实验银行家算法Word格式.docx_第3页
第3页 / 共17页
操作系统实验银行家算法Word格式.docx_第4页
第4页 / 共17页
操作系统实验银行家算法Word格式.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

操作系统实验银行家算法Word格式.docx

《操作系统实验银行家算法Word格式.docx》由会员分享,可在线阅读,更多相关《操作系统实验银行家算法Word格式.docx(17页珍藏版)》请在冰豆网上搜索。

操作系统实验银行家算法Word格式.docx

能执行完标志

已占资源量表示进程目前已经得到但还未归还的资源量。

因此,进程在以后还需要的剩余资源量等于资源需求总量减去已占资源量。

显然每个进程的资源需求总量不应超过系统拥有的资源总量。

(2)银行家算法分配资源的原则是:

当某个进程提出资源请求时,假定先分配资源给它,然后查找各进程的剩余请求,检查系统的剩余资源量是否由于进程的分配而导致系统死锁。

若能,则让进程等待,否则,让进程的假分配变为真分配。

①查找各进程的剩余请求,检查系统的剩余资源量是否能满足其中一进程。

如果能,则转②②将资源分配给所选的进程,这样,该进程已获得资源量最大请求,最终能运行完。

标记这个进程为终止进程,并将其占有的全部资源归还给系统。

重复第①步和第②步,直到所有进程都标记为终止进程,或直到一个死锁发生。

若所有进程都标记为终止进程,则梯田的初始状态是安全的,否则为不安全的。

若安全,则正式将资源分配给它,否则,假定的分配作废,让其等待。

③由于银行家算法可以避免死锁,为了观察死锁现象的发生,要求采用两个算法:

银行家算法和随机算法。

随机算法的分配原则是:

当进程申请资源时,如果系统现存资源数能满足进程的当前申请量,就把资源分配给进程,否则,让其等待。

这样,随机算法可能引起死锁。

资源分配模拟算法总框图

四、源代码

#include<

iostream>

#include<

vector>

fstream>

usingnamespacestd;

constintTASK_RUNNING=0;

constintTASK_SUCCEED=1;

constintTASK_WAITTING=2;

constintTASK_RLength=10;

intRcs-left=RLength;

ofstreamff("

result.txt"

);

classpcb

{

public:

intp_pid;

intp_stat;

intp_apply;

intp_occupy;

boolp_issuc;

intp_require;

pcb(intid,intrequire)

{

p_pid=id;

p_require=require;

p_stat=TASK-RUNNING;

p_occupy=0;

p_issuc=false;

p_apply=0;

}

friendostream&

operator<

<

(ostream&

cout,constpcb&

p)

cont<

p.p_pid<

'

t'

p.p_stat<

\t'

p.p_require<

p.p_occupy<

endl;

returncout;

};

voidrand(vector<

int>

&

resource,vector<

pcb>

pgrp);

voidbanker(vector<

intmain()

vector<

resource;

pgrp;

:

iteratorr;

iteratorp;

cout<

"

ENTERTHEMAXNUMBERFORTHEREQUESTEDRESOURCE:

ID\tREQUESTED"

ff<

intid,qty;

for(inti

(1);

i<

=4;

i++)

do

{

cout<

;

ff<

cin>

>

qty;

qty<

}while(qty>

Rcs_left||qty<

1);

pgrp.insert(pgrp.begin(),pcb(i,qty));

}

//输入各进程申请资源的总量,以及初始化各进程;

ALOGRITHM"

endl

<

Random(R)"

Banker(B)"

ANYOTHERKEYTOQUIT"

charchoice;

cin>

choice;

choice<

if(choice=='

R'

||choice=='

r'

rand(resource,pgrp);

elseif(choice=='

B'

b'

banker(resource,pgrp);

else

return(0);

rerurn

(1);

vector<

iteratorp,q;

iteratorcurrent;

inttemp;

cout<

NOW-----BANKERALOGRITHM"

ff<

for(;

//selectaTASK_RUNNIGprocess,maybedifferentfromtheformerone;

for(p=pgrp.begin();

p!

=pgrp.end();

p++)

if(p-p_stat=TASK-RUNNING;

current=p;

break;

}

if(current->

p_apply==0)

ENTERTHEAPPLYFORTHEPROCESS\n"

current->

p_pid<

cin>

temp;

temp<

while(temp>

p->

p_require-p->

p_occupy)

beyondtherealneed!

p->

p_apply=temp;

//inputtheapplyforthecurrentprocess;

p_apply>

Rcs_left)

{//hasproblem

//applytoomuch,pleasewait---

current->

p_stat=TASK_WAITTING;

endl<

iswaitting\n"

for(p=pgrp.begin();

if(p->

p_stat==TASK_RUNNING)break;

if(p==pgrp.end())

LOCKED!

!

exit

(1);

//满足该进程当前的申请

resource.insert(resource.begin(),current->

p_apply,current->

p_pid);

\tresourcesareacceptedfor"

Rcs_left-=current->

p_apply;

p_occupy+=current->

p_occupy=0;

//看该进程是否已满足

p_occupy<

p_require)

pcbproc(*current);

pgrp.erase(current);

pgrp.insert(pgrp.end(),proc);

//current->

p_apply=0;

//deletecurrentandinsertintotheend

continue;

//goonandshouldselectanotherprocess

//succeed

process\t"

p-p_pid<

\thassucceed!

Rcs_left+=current->

resource.clear();

p_stat=TASK_SUCCEED;

//

p_stat==TASK_WAITTING)

for(q=pgrp.begin();

q!

=pgrp,end();

q++)

if(q->

p_stat==TASK_RUNNING)

if(q==pgrp,end())

SUCCEED!

exit(0);

elsecontinue;

//thereisaprocessinthequeue;

p_stat==TASK_WAITTING&

Rcs_left>

=p->

p_apply)

if(p!

=pgrp.end())

p_stat==TASK_RUNNING;

pcbproc(*p);

pgrp.erase(p);

else

iteratorcurrent,q;

pcbproc(0,0);

intlength;

p_apply==0)

p_apply<

while(current->

p_apply>

p_require>

Rcs_left;

proc=*current;

pgrp.insert(pgrp.end()),proc);

iswaiting!

//假定对申请资源的进程分配资源

pcbbackup(*current);

//backup

length=Rcs_left;

length-=current->

p_occupy==current->

p_require

p_issuc=true;

p_stat==TASK_SUCCEED)CONTINUE;

if(p==current&

p-p_issuc==true)

if((p->

p_occupy)>

length)continue;

p_issue=true;

length+=p->

p_occupy;

//检查是否还有标志位未设置的进程

p_issuc==false&

p_stat!

=TASK_SUCCEED)break;

p_occupy=backup.p_occupy;

iswaiting."

p_issuc==false;

//分配安全,可对程序进行实际的分配

resource.insert(resource.end(),current->

apply;

get"

resource(s)!

}

p_stat=TASK_SUCCEED

hasfinished!

//归还全部系统资源

p_require;

q_stat==TASK_RUNNING)

if(q==pgrp.end())

proc=*p;

p_stat=TASK_RUNNING;

五、系统测试及调试

1、实际测试数据

/*程序演示结果如下:

*/

ENTERTHEMAXNUMERFORTHEREQUESTEDRESOURCE:

IDREQUESTED

13

25

37

49

ALOGRITHM

Random(R)Banker(B)

ANYOTHERKEYTOQUIT

r

NOW------BANKERALOGRITHM

ENTERTHEAPPLYFORTHEPROCESS

42

2resourceareacceptedfor4

33

3resourceareacceptedfor3

5resourceareacceptedfor2

process2hassucceed!

12

2resourceareacceptedfor1

44

4iswaiting

32

2resourceareacceptedfor3

beyondtherealneed!

11

1resourceareacceptedfor1

process1hassucceed!

LOCKED!

*/

六、心得体会

程序基本实现了银行家算法的功能,并在其基础上考虑了输出显示的格式的美观性、输入错误要求重新输入等几个方面,尽可能的使程序完整一些。

由于能力有限,所以在紧张的的编写和调试过程中遇到了许多的问题,通过查询资料、翻阅

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

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

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