资源描述:
《银行家算法源程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
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},{3,0,2},{2,1,1},{0,0,2}};//已分配资源数矩
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[process].A+=res->A;Allocation[process].B+=res->B;Allocation[process].C+=re
3、s->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+=res->C;Allocation[process].A-=res->A;Allocation[process].B-=res->B;Allocation[process].C-=res->C;Need[pro
4、cess].A+=res->A;Need[process].B+=res->B;Need[process].C+=res->C;}//安全性检查boolSafeCheck(){jcWork=Available;boolFinish[jcsl]={false,false,false,false,false};inti;intj=0;for(i=0;i5、程{//有则使其执行完成,并将已分配给该进程的资源全部回收Work.A+=Allocation[i].A;Work.B+=Allocation[i].B;Work.C+=Allocation[i].C;Finish[i]=true;safe[j++]=i;i=-1;//重新进行遍历}}}for(i=0;i6、if(res->A<=Need[process].A&&res->B<=Need[process].B&&res->C<=Need[process].C)//request向量需小于Need矩阵中对应的向量{if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C)//request向量需小于Available向量{ProbeAlloc(process,res);//试探分配if(SafeCheck())//如果安全检查成立,则请求成功,否则将分配回滚并返回失败{returntrue;}else{co
7、ut<<"安全性检查失败。原因:系统将进入不安全状态,有可能引起死锁."<