銀行家算法是一種最有代表性的避免死鎖的算法。在避免死鎖方法中允許進程動態地申請資源,但系統在進行資源分配之前,應先計算此次分配資源的安全性,若分配不會導致系統進入不安全狀態,則分配,否則等待。為實現銀行家算法,系統必須設置若干數據結構。
銀行家算法程序代碼如下:
#include<string.h>
#include<stdio.h>
#include<iostream.h>
#defineFALSE0
#defineTRUE1
#defineW10
#defineR10
intM;//總進程數
intN;//資源種類
intALL_RESOURCE[W];//各種資源的數目總和
intMAX[W][R];//M個進程對N類資源最大資源需求量
intAVAILABLE[R];//系統可用資源數
intALLOCATION[W][R];//M個進程已經得到N類資源的資源量
intNEED[W][R];//M個進程還需要N類資源的資源量
intRequest[R];//請求資源個數
voidoutput()
{
inti,j;
cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"各種資源的總數量:"<<endl;
for(j=0;j<N;j++)
cout<<"資源"<<j<<":"<<ALL_RESOURCE[j];
cout<<endl;
cout<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"目前各種資源可利用的數量為:"<<endl;
for(j=0;j<N;j++)
cout<<"資源"<<j<<":"<<AVAILABLE[j];
cout<<endl;
cout<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"各進程還需要的資源數量:"<<endl<<endl;
for(i=0;i<N;i++)
cout<<"資源"<<i;
cout<<endl;
for(i=0;i<M;i++)
{
cout<<"進程"<<i<<":";
for(j=0;j<N;j++)
cout<<NEED[i][j]<<"";
cout<<endl;
}
cout<<endl;
cout<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"各進程已經得到的資源量:"<<endl<<endl;
for(i=0;i<N;i++)
cout<<"資源"<<i;
cout<<endl;
for(i=0;i<M;i++)
{
cout<<"進程"<<i<<":";
for(j=0;j<N;j++)
cout<<ALLOCATION[i][j]<<"";
cout<<endl;
}
cout<<endl;
}
voiddistribute(intk)
{
intj;
for(j=0;j<N;j++)
{
AVAILABLE[j]=AVAILABLE[j]-Request[j];
ALLOCATION[k][j]=ALLOCATION[k][j]+Request[j];
NEED[k][j]=NEED[k][j]-Request[j];
}
}
voidrestore(intk)
{
intj;
for(j=0;j<N;j++)
{
AVAILABLE[j]=AVAILABLE[j]+Request[j];
ALLOCATION[k][j]=ALLOCATION[k][j]-Request[j];
NEED[k][j]=NEED[k][j]+Request[j];
}
}
intcheck()
{
intWORK[R],FINISH[W];
inti,j;
for(j=0;j<N;j++)WORK[j]=AVAILABLE[j];
for(i=0;i<M;i++)FINISH[i]=FALSE;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
if(FINISH[i]==FALSE&&NEED[i][j]<=WORK[j])
{
WORK[j]=WORK[i]+ALLOCATION[i][j];
}
}
FINISH[i]=TRUE;
}
for(i=0;i<M;i++)
{
if(FINISH[i]==FALSE)
{
cout<<endl;
cout<<"系統不安全!!!本次資源申請不成功!!!"<<endl;
cout<<endl;
return1;
}
else
{
cout<<endl;
cout<<"經安全性檢查,系統安全,本次分配成功。"<<endl;
cout<<endl;
return0;
}
}
}
voidbank()//銀行家算法
{
inti=0,j=0;
charflag='Y';
while(flag=='Y'||flag=='y')
{
i=-1;
while(i<0||i>=M)
{
cout<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<endl<<"請輸入需申請資源的進程號:";
cin>>i;
if(i<0||i>=M)cout<<"輸入的進程號不存在,重新輸入!"<<endl;
}
cout<<"請輸入進程"<<i<<"申請各類資源的數量:"<<endl;
for(j=0;j<N;j++)
{
cout<<"資源"<<j<<":";
cin>>Request[j];
if(Request[j]>NEED[i][j])//若請求的資源數大于進程還需要i類資源的資源量j
{
cout<<endl<<"進程"<<i<<"申請的資源數大于進程"<<i<<"還需要"<<j<<"類資源的數量!";
cout<<"若繼續執行系統將處于不安全狀態!"<<endl;
flag='N';
break;
}
else
{
if(Request[j]>AVAILABLE[j])//若請求的資源數大于可用資源數
{
cout<<endl<<"進程"<<i<<"申請的資源數大于系統可用"<<j<<"類資源的數量!";
cout<<"若繼續執行系統將處于不安全狀態!"<<endl;
flag='N';
break;
}
}
}
if(flag=='Y'||flag=='y')
{
distribute(i);//調用change(i)函數,改變資源數
if(check())//若系統安全
{
restore(i);//調用restore(i)函數,恢復資源數
output();//輸出資源分配情況
}
else//若系統不安全
output();//輸出資源分配情況
}
else//若flag=N||flag=n
cout<<endl;
cout<<"是否繼續銀行家算法演示,按'Y'或'y'鍵繼續,按'N'或'n'鍵退出演示:";
cin>>flag;
}
}
voidversion()
{
cout<<endl;
cout<<"\t 銀行家算法 "<<endl;
}
voidmain()//主函數
{
inti=0,j=0,p;
version();
getchar();
cout<<endl<<"請輸入總進程數:";
cin>>M;
cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"請輸入總資源種類:";
cin>>N;
cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"請輸入各類資源總數:(需要輸入數為"<<N<<"個)";
for(i=0;i<N;i++)
cin>>ALL_RESOURCE[i];
cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"輸入各進程所需要的各類資源的最大數量:(需要輸入數為"<<M*N<<"個)";
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
do
{
cin>>MAX[i][j];
if(MAX[i][j]>ALL_RESOURCE[j])
cout<<endl<<"占有資源超過了聲明的該資源總數,請重新輸入"<<endl;
}
while(MAX[i][j]>ALL_RESOURCE[j]);
}
}
cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"輸入各進程已經占據的各類資源的數量:(需要輸入數為"<<M
*N<<"個)";
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
do
{
cin>>ALLOCATION[i][j];
if(ALLOCATION[i][j]>MAX[i][j])
cout<<endl<<"占有資源超過了聲明的最大資源,請重新輸入"<<endl;
}
while(ALLOCATION[i][j]>MAX[i][j]);
}
}
for(j=0;j<N;j++)//初始化資源數量
{
p=ALL_RESOURCE[j];
for(i=0;i<M;i++)
{
p=p-ALLOCATION[i][j];//減去已經被占據的資源
AVAILABLE[j]=p;
if(AVAILABLE[j]<0)
AVAILABLE[j]=0;
}
}
for(i=0;i<M;i++)
for(j=0;j<N;j++)
NEED[i][j]=MAX[i][j]-ALLOCATION[i][j];
output();
bank();
}