資源描述:
《銀行家算法源程序.docx》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、#include#include#includeusingnamespacestd;#definefalse0#definetrue!false#definejcsl5//系統(tǒng)中所有進(jìn)程數(shù)量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}};//已分配資源數(shù)矩陣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)//若試探分配后進(jìn)入不安全狀態(tài),將分配回滾{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;//重新進(jìn)行遍歷}}}for(i=0;iA<=Need[process].A&&res->B<=Need[process].B&&res->C<=Need[process].C)//
7、request向量需小于Need矩陣中對應(yīng)的向量{if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C)//request向量需小于Available向量{ProbeAlloc(process,res);//試探分配if(SafeCheck())//如果安全檢查成立,則請求成功,否則將分配回滾并返回失敗{returntrue;}else{cout<<"安全性檢查失敗。原因:系統(tǒng)將進(jìn)入不安全狀態(tài),有可能引起死鎖."<8、ollBack(process,res);}}else{cout<<"安全性檢查失敗。原因:請求向量大于可利用資源向量。"<