c实现银行家算法_第1页
c实现银行家算法_第2页
c实现银行家算法_第3页
免费预览已结束,剩余5页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、c实现银行家算法银行家算法是一种最有代表性的避免死锁的算法。要解释银行家算法,必须先解释操作系统安全状态和不安全状态。安全状态:如果存在一个由系统中所有进程构成的安全序列P1,,Pn,则系统处于安全状态。安全状态一定是没有死锁发生。不安全状态:不存在一个安全序列。不安全状态不一定导致死锁。那么什么是安全序列呢?安全序列:一个进程序列P1,,Pn是安全的,如果对于每一个进程Pi(1vi关日它以后尚需要的资源量不超过系统当前剩余资源量与所有进程Pj(j<i)当前占有资源量之和。银行家算法:我们可以把操作系统看作是银行家,操作系统管理的资源相当于银行家管理的资金,进程向操作系统请求分配资源相当

2、于用户向银行家贷款。操作系统按照银行家制定的规则为进程分配资源,当进程首次申请资源时,要测试该进程对资源的最大需求量,如果系统现存的资源可以满足它的最大需求量则按当前的申请量分配资源,否则就推退分配。当进程在执行中继续申请资源时,先测试该进程已占用的资源数与本次申请的资源数之和是否超过了该进程对资源的最大需求量。若超过则拒绝分配资源,若没有超过则再测试系统现存的资源能否满足该进程尚需的最大资源量,若能满足则按当前的申请量分配资源,否则也要推退分配。算法:n:系统中进程的总数m:资源类总数Available:ARRAY1.mofinteger;Max:ARRAY1.n,1.mofinteger;

3、Allocation:ARRAY1.n,1.mofinteger;Need:ARRAY1.n,1.mofinteger;Request:ARRAY1.n,1.mofinteger;符号说明:Available可用剩余资源Max最大需求Allocation已分配资源Need需求资源Request请求资源当进程pi提出资源申请时,系统执行下列步骤:(“切赋值符号,“=翊等号)step(1)若Request<=Need,gotostep(2);否则错误返回step(2)若Request<=Available,gotostep(3);否则进程等待step(3)假设系统分配了资源,贝U有:A

4、vailable=Available-Request;Allocation=Allocation+Request;Need=Need-Request若系统新状态是安全的,则分配完成若系统新状态是不安全的,则恢复原状态,进程等待为进行安全性检查,定义数据结构:Work:ARRAY1.mofinteger;Finish:ARRAY1.nofBoolean;安全性检查的步骤:step(1):Work=Available;Finish=false;step(2)寻找满足条件的i:a. Finish=false;b. Need<=Work;如果不存在,gotostep(4)step(3)Work=

5、Work+Allocation;Finish=true;gotostep(2)step(4)若对所有i,Finish=true,则系统处于安全状态,否则处于不安全状态/*银行家算法,操作系统概念(OSconceptsSixEdition)reeditbyJohnnyhagen,SCAU,runatvc6.0*/#include"malloc.h"#include"stdio.h"#include"stdlib.h"#definealloclensizeof(structallocation)#definemaxlensizeof(st

6、ructmax)#defineavalensizeof(structavailable)#defineneedlensizeof(structneed)#definefinilensizeof(structfinish)#definepathlensizeof(structpath)structallocationintvalue;structallocation*next;structmaxintvalue;structmax*next;structavailable/*可用资源数*/intvalue;structavailable*next;structneed/*需求资源数*/(intv

7、alue;structneed*next;;structpath(intvalue;structpath*next;structfinish(intstat;structfinish*next;intmain()(introw,colum,status=0,i,j,t,temp,processtest;structallocation*allochead,*alloc1,*alloc2,*alloctemp;structmax*maxhead,*maxium1,*maxium2,*maxtemp;structavailable*avahead,*available1,*available2,*

8、workhead,*work1,*work2,*worktemp,*worktemp1;structneed*needhead,*need1,*need2,*needtemp;structfinish*finihead,*finish1,*finish2,*finishtemp;structpath*pathhead,*path1,*path2;printf("n请输入系统资源的种类数:");scanf("%d”,&colum);printf("请输入现时内存中的进程数:,scanf("%d”,&row);printf(&quo

9、t;请输入已分配资源矩阵:n");for(i=0;i<row;i+)(for(j=0;j<colum;j+)(printf("请输入已分配给进程p%d的%c种系统资源:”,i,'A'+j);if(status=0)(allochead=alloc1=alloc2=(structallocation*)malloc(alloclen);alloc1->next=alloc2->next=NULL;scanf("%d”,&allochead->value);status+;else(alloc2=(structal

10、location*)malloc(alloclen);scanf("%d,%d”,&alloc2->value);if(status=1)(allochead->next=alloc2;status+;alloc1->next=alloc2;alloc1=alloc2;alloc2->next=NULL;status=0;printf("请输入最大需求矩阵:n");for(i=0;i<row;i+)(for(j=0;j<colum;j+)(printf(-请输入进程p%d种类%c系统资源最大需求:",i,

11、9;A'+j);if(status=0)(maxhead=maxium1=maxium2=(structmax*)malloc(maxlen);maxium1->next=maxium2->next=NULL;scanf("%d",&maxium1->value);status+;else(maxium2=(structmax*)malloc(maxlen);scanf("%d,%d",&maxium2->value);if(status=1)(maxhead->next=maxium2;status

12、+;maxium1->next=maxium2;maxium1=maxium2;maxium2->next=NULL;status=0;printf("请输入现时系统剩余的资源矩阵:n");for(j=0;j<colum;j+)printf("种类%c的系统资源剩余:”,'A'+j);if(status=0)avahead=available1=available2=(structavailable*)malloc(avalen);workhead=work1=work2=(structavailable*)malloc(aval

13、en);available1->next=available2->next=NULL;work1->next=work2->next=NULL;scanf("%d”,&available1->value);work1->value=available1->value;status+;elseavailable2=(structavailable*)malloc(avalen);work2=(structavailable*)malloc(avalen);scanf("%d,%d",&available2-&

14、gt;value);work2->value=available2->value;if(status=1)avahead->next=available2;workhead->next=work2;status+;available1->next=available2;available1=available2;work1->next=work2;work1=work2;available2->next=NULL;work2->next=NULL;status=0;alloctemp=allochead;maxtemp=maxhead;for(i

15、=0;i<row;i+)for(j=0;j<colum;j+)(if(status=0)(needhead=need1=need2=(structneed*)malloc(needlen);need1->next=need2->next=NULL;need1->value=maxtemp->value-alloctemp->value;status+;else(need2=(structneed*)malloc(needlen);need2->value=(maxtemp->value)-(alloctemp->value);if(s

16、tatus=1)(needhead->next=need2;status+;need1->next=need2;need1=need2;maxtemp=maxtemp->next;alloctemp=alloctemp->next;need2->next=NULL;status=0;for(i=0;i<row;i+)(if(status=0)(finihead=finish1=finish2=(structfinish*)malloc(finilen);finish1->next=finish2->next=NULL;finish1->st

17、at=0;status+;else(finish2=(structfinish*)malloc(finilen);finish2->stat=0;if(status=1)(finihead->next=finish2;status+;finish1->next=finish2;finish1=finish2;finish2->next=NULL;/*Initializationcompleated*/status=0;processtest=0;for(temp=0;temp<row;temp+)alloctemp=allochead;needtemp=needh

18、ead;finishtemp=finihead;worktemp=workhead;for(i=0;i<row;i+)worktemp1=worktemp;if(finishtemp->stat=0)for(j=0;j<colum;j+,needtemp=needtemp->next,worktemp=worktemp->next)if(needtemp->value<=worktemp->value)processtest+;if(processtest=colum)for(j=0;j<colum;j+)worktemp1->value+=alloctemp->value;worktemp1=worktemp1->next;alloctemp=alloctemp->next;if(status=0)pathhead=path1=path2=(structpath*)malloc(pathlen);path1->next=path2->next=NULL;path1->value=i;status+;elsepath2=(structpath*)malloc(pathlen);path2->value=i;if(status=1)(pathhead->

温馨提示

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

最新文档

评论

0/150

提交评论