資源簡介
銀行家算法是死鎖避免的經典算法,其核心思想是:進程動態地申請資源,每次申請資源時系統都執行安全狀態檢查算法判斷本次申請是否會造成系統處于不安全狀態,如果不安全則阻塞進程;如果安全狀態,則完成資源分配。
安全狀態檢查算法的思想是找到一個安全序列,使所有進程都能執行完畢。如果找到,則處于安全狀態,否則為不安全狀態。

代碼片段和文件信息
#include“stdafx.h“
#include?“string.h“?
#include?“iostream“?
using?std::cout;
using?std::cin;
using?std::endl;
#define?M?4?//總進程數?
#define?N?3?//總資源數?
#define?FALSE?0?
#define?TRUE?1?
//M個進程對N類資源最大資源需求量?
int?MAX[M][N]={{867}{678}{452}{463}};?
//系統可用資源數?
int?AVAILABLE[N]={12109};?
//M個進程已經得到N類資源的資源量?
int?ALLOCATION[M][N]={{000}{000}{000}{000}};?
//M個進程還需要N類資源的資源量?
int?NEED[M][N]={{867}{678}{452}{463}};?
int?Request[N]={000};?
void?showdata();?
void?changdata(int);?
void?backData(int);?
bool?IsNotSafe(int);
void?main()?
{?
int?i=0;//進程i
int?j=0;//資源j?
char?flag=‘Y‘;?
?
?
showdata();?
while(flag==‘Y‘||flag==‘y‘)?//while?1
{?
?????i=-100;?//先賦值一個負數,在用戶輸入第i個進程數時,再改變其值
?????while(i<0||i>=M)?//while2
?????{?
?????????cout<<“?請輸入需要申請資源的進程號(從0到“< ?????????cin>>i;?
?????????if(i<0||i>=M)
?????????{
????????????cout<<“?輸入的進程號不存在,系統退出!“< ????????????return;
?????????}
?
?????}?//end?while2
?
????//第二步:輸入進程i申請的資源數
?????cout<<“?請輸入進程“<?????for?(j=0;j ?????{?
?????????cout<<“?需要資源“< ?????????cin>>Request[j];?
?????????if(Request[j]>NEED[i][j])?
?????????{?
??????????????cout<<“?進程“<??????????????cout<<“申請不合理,出錯!請重新選擇!“< ??????????????flag=‘N‘;?
??????????????break;?
?????????}?
?????????else?
?????????{?
??????????????if(Request[j]>AVAILABLE[j])?
??????????????{?
???????????????????cout<<“?進程“<???????????????????cout<<“由于可能會產生死鎖,所以銀行家算法不予分配,請重新輸入!“< ???????????????????flag=‘N‘;?
???????????????????break;?//重新顯示數據,(退回到原來的)
??????????????}?
?????????}
?????}//end?for
?????if(flag==‘Y‘||flag==‘y‘)?
?????{?
?????????changdata(i);?
?????????if(IsNotSafe(i))?
?????????{?
??????????????backData(i);?
??????????????showdata();?
?????????}?
?????????else?
??????????????showdata();?
?????}?
?????else?//flag=‘N‘
?????????showdata();?
?????????cout< ?????????cout<<“?是否繼續銀行家算法演示按‘Y‘或‘y‘鍵繼續按‘N‘或‘n‘鍵退出演示:?“;?
?????????cin>>flag;?
?????}?
}
?
void?showdata()?
{?
int?ij;?
cout<<“?系統可用的資源數為:“< cout<<“?“;?
for?(j=0;j cout< ?
cout< cout<<“?各進程還需要的資源量:“< for?(i=0;i {?
cout<<“進程“<for?(j=0;j cout< }?
cout< cout<<“?各進程已經得到的資源量:?“< for?(i=0;i {?
cout<<“進程“<for?(j=0;j cout< }?
cout< }
?
void?changdata(int?k)?
{?
?????int?j;?
?????for?(j=0;j ?????{?
?????????AVAILABLE[j]=AVAILABLE[j]-Request[j];?
?????????ALLOCATION[k][j]=ALLOCATION[k][j]+Request[j];?
?????????NEED[k][j]=NEED[k][j]-Request[j];?
?????}?
}?
?
void?backData(int?k)?
{?
?????int?j;?
?????for?(j=0;j ?????{?
?????????AVAILABL
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????????0??2011-04-07?13:42??Bank\Allocated_list.txt
?????文件???????5462??2011-04-07?13:59??Bank\Bank.cpp
?????文件???????4512??2011-04-07?13:41??Bank\Bank.dsp
?????文件????????516??2011-04-07?13:41??Bank\Bank.dsw
?????文件??????33792??2011-04-07?14:07??Bank\Bank.ncb
?????文件??????48640??2011-04-07?14:07??Bank\Bank.opt
?????文件????????242??2011-04-07?14:06??Bank\Bank.plg
?????文件??????????0??2011-04-07?13:42??Bank\Initiate.txt
?????文件???????1196??2011-04-07?13:41??Bank\ReadMe.txt
?????文件????????291??2011-04-07?13:41??Bank\StdAfx.cpp
?????文件????????667??2011-04-07?13:41??Bank\StdAfx.h
?????目錄??????????0??2011-04-07?14:07??Bank
-----------?---------??----------?-----??----
????????????????95318????????????????????12
評論
共有 條評論