资源描述:
《银行家算法源程序.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#includeusingnamespacestd;#definefalse0#definetrue!false#definejcsl5//系统中所有进程数量typedefstruct{intA;intB;intC;}jc;jcMax[jcsl]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};//最大需求矩阵jcAllocation[jcsl]={{0,1,0},{2,0,0}
2、,{3,0,2},{2,1,1},{0,0,2}};//已分配资源数矩阵jcNeed[jcsl]={{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};//需求矩阵jcAvailable={3,3,2};//可用资源向量intsafe[jcsl];voidProbeAlloc(intprocess,jc*res)//试探分配{Available.A-=res->A;Available.B-=res->B;Available.C-=res->C;Allocation[proc
3、ess].A+=res->A;Allocation[process].B+=res->B;Allocation[process].C+=res->C;Need[process].A-=res->A;Need[process].B-=res->B;Need[process].C-=res->C;}voidRollBack(intprocess,jc*res)//若试探分配后进入不安全状态,将分配回滚{Available.A+=res->A;Available.B+=res->B;Available.C+=
4、res->C;Allocation[process].A-=res->A;Allocation[process].B-=res->B;Allocation[process].C-=res->C;Need[process].A+=res->A;Need[process].B+=res->B;Need[process].C+=res->C;}//安全性检查boolSafeCheck(){jcWork=Available;boolFinish[jcsl]={false,false,false,false,fa
5、lse};inti;intj=0;for(i=0;i6、e;safe[j++]=i;i=-1;//重新进行遍历}}}for(i=0;iA<=Need[process].A&&res->B<=Need[process].B&&res->C<=Need[process].C)//
7、request向量需小于Need矩阵中对应的向量{if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C)//request向量需小于Available向量{ProbeAlloc(process,res);//试探分配if(SafeCheck())//如果安全检查成立,则请求成功,否则将分配回滚并返回失败{returntrue;}else{cout<<"安全性检查失败。原因:系统将进入不安全状态,有可能引起死锁."<8、ollBack(process,res);}}else{cout<<"安全性检查失败。原因:请求向量大于可利用资源向量。"<