?? 銀行家算法.cpp
字號:
#include "string.h"
#include "iostream"
#include "iomanip"
using namespace std;
#define FALSE 0
#define TRUE 1
#define W 10
#define R 20
int FINISH[W];
int M ; //總進程數
int N ; //資源種類
int ALL_RESOURCE[W];//各種資源的數目總和
int MAX[W][R]; //M個進程對N類資源最大資源需求量
int AVAILABLE[R]; //系統可用資源數
int ALLOCATION[W][R]; //M個進程已經得到N類資源的資源量
int NEED[W][R]; //M個進程還需要N類資源的資源量
int Request[R]; //請求資源個數
int WORK[100];
int Q[100];
int L=0;
//int FINISH[100];
void showdata() //函數showdata,輸出資源分配情況
{
int i,j;
cout<<" ————————————————————————"<<endl;
cout<<"各種資源的總數量(all):"<<endl;
cout<<" ";
for (j=0;j<N;j++)cout<<" 資源"<<j<<": "<<ALL_RESOURCE[j];
cout<<endl<<endl;
cout<<"系統目前各種資源可用的數為(available):"<<endl;
cout<<" ";
for (j=0;j<N;j++)cout<<" 資源"<<j<<": "<<AVAILABLE[j];
cout<<endl<<endl;
cout<<"—————————————————————";
cout<<" 各進程還需要的資源量(need):"<<endl<<endl;
cout<<" ";
for (j=0;j<N;j++)cout<<setw(8)<<"資源"<<j;
//cout<<" 資源0"<<" 資源1"<<" 資源2"<<endl;
//for (i=0;i<M;i++)
for (i=0;i<M;i++)
{
cout<<endl<<"進程p"<<i<<":";
for (j=0;j<N;j++)cout<<setw(8)<<NEED[i][j];//<<" ";//;
//cout<<endl;
}
cout<<endl;
cout<<"—————————————————————";
cout<<" 各進程已經得到的資源量(allocation): "<<endl<<endl;
cout<<" ";
for (j=0;j<N;j++)cout<<setw(8)<<"資源"<<j;
//cout<<" 資源0"<<" 資源1"<<" 資源2"<<endl;
for (i=0;i<M;i++) //0
{
cout<<endl<<"進程p"<<i<<":";
for (j=0;j<N;j++)cout<<setw(8)<<ALLOCATION[i][j];//<<" ";
//cout<<endl;
}
cout<<endl;
/*
for (i=0;i<M;i++)
for (j=0;j<N;j++){
if (NEED[i][j]!=0)
cout<<"進程p"<<i<<"狀態: 運行結束"<<endl;
else
cout<<"進程p"<<i<<"狀態: 等待調用"<<endl;}
*/
}
void changdata(int k) //函數changdata,改變可用資源和已經拿到資源和還需要的資源的值
{
int j;
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];
//WORK[j]=AVAILABLE[j];
}
}
void rstordata(int k) //函數rstordata,恢復可用資源和已經拿到資源和還需要的資源的值
{ int j;
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];
//WORK[j]=AVAILABLE[j];
}
}
int chkerr(int s) //函數chkerr,檢查是否安全
{ int WORKrest;//FINISH[W]
int i,j,k=0;
for(i=0;i<M;i++)FINISH[i]=FALSE;
for(j=0;j<N;j++)
{
WORKrest=AVAILABLE[j];
i=s;
do
{
if(FINISH[i]==FALSE&&NEED[i][j]<=WORKrest)
{
WORKrest=WORKrest+ALLOCATION[i][j];
FINISH[i]=TRUE;
i=0;
}
else
{
i++;
}
}while(i<M);
for(i=0;i<M;i++)
if(FINISH[i]==FALSE)
{
cout<<endl;
cout<<" 系統不安全!!! 本次資源申請不成功!!!"<<endl;
cout<<endl;
return 1;
cout<<"進程p"<<i<<"狀態: 阻塞"<<endl;
//else
//cout<<"進程p"<<i<<"狀態: 等待調用"<<endl;
}
}
cout<<endl;
cout<<" 經安全性檢查,系統安全,本次分配成功。"<<endl;
cout<<endl;
return 0;
}
void set()
{
int t[10];
for(int i=0;i<M;i++){
t[i]=0;
for(int j=0;j<N;j++){if(NEED[i][j]==0) t[i]+=1;}
}
for(i=0;i<M;i++)
if(t[i]==N)
{cout<<"進程p"<<i<<"狀態: 運行結束"<<endl;
for(int k=0;k<N;k++)
AVAILABLE[k]=AVAILABLE[k]+ALLOCATION[i][k];
ALLOCATION[i][k]=0;
}
else
cout<<"進程p"<<i<<"狀態: 等待調用"<<endl;
}
void safe() //對進程進行安全行檢查,此時是否存在安全性序列
{ cout<<endl<<"···現在計算機自動檢測是否存在安全序列···"<<endl;
int i,j;
for(j=0;j<N;j++)
WORK[j]=AVAILABLE[j];//cout<<"a"<<WORK[j];
//int q[100];
int flag=0;
for(i=0; i<M;i++)
FINISH[i]=FALSE;//chushi
for(i=0; i<M;i++)
{
if(FINISH[i]==FALSE)
{
flag=0;
for(j=0; j<N; j++)
{ //cout<<WORK[i]<<" ";
//WORK[j]=AVAILABLE[j];cout<<"a"<<WORK[j];
if(WORK[j]>=NEED[i][j])
flag=flag+1;
else
break;
}
if(flag==N)
{
Q[L]=i; //cout<<Q[L]<<"ql"<<L<<endl;
FINISH[i]=TRUE;
for(j=0; j<N;j++)
{
WORK[j]=WORK[j]+ALLOCATION[i][j];//cout<<"b"<<WORK[j];
}
L=L+1; //cout<<L<<endl;
cout<<"進程p"<<i<<"資源釋放后系統目前各種資源可用的數為(available):"<<endl;// cout<<"*********************"; cout<<"l"<<L<<endl;
cout<<endl;
for (j=0;j<N;j++)
cout<<" "<<"資源"<<j<<": "<<WORK[j];
cout<<endl<<endl;
i=0;
}
}
else;
//i++;
//cout<<"i"<<i<<endl;//cout<<q[l]<<"ql"<<i<<endl;
}
}
int test()
{
if (L<M)
{
cout<<"\n當前狀態不安全!!!!!";
cout<<"不存在安全序列 "<<endl;
cout<<"^^^您不能向系統申請資源^^^"<<endl;
cout<<"………計算機檢測結束………"<<endl;
return 0;
}
else
{
int i;
//l=0;
cout<<"\n此時處于安全的狀態!!!"<<endl;
cout<<endl;
cout<<"安全序列是: ";
for (i=0; i<M; i++)
{
cout<<"進程"<<"p"<<Q[i]<<" --> ";
FINISH[i]=FALSE;
}
cout<<endl;
cout<<"………計算機檢測結束………"<<endl;
return 1;
}
//cout<<endl<<"計算機檢測結束"<<endl;
}
void bank() //銀行家算法
{
int i=0,j=0;
//i=0;intj=0;
char flag='Y';
while(flag=='Y'||flag=='y')
{
i=-1;
while(i<0||i>=M)
{ cout<<endl;
cout<<" 請輸入需申請資源的進程號(從P0到P"<<M-1<<",否則重輸入!):";
cout<<"p";cin>>i;
if(i<0||i>=M)cout<<" 輸入的進程號不存在,重新輸入!"<<endl;
}
cout<<" 請輸入進程P"<<i<<"申請的資源數:"<<endl;
for (j=0;j<N;j++)
{
cout<<" 資源"<<j<<": ";
cin>>Request[j];
if(Request[j]>NEED[i][j]) //若請求的資源數大于進程還需要i類資源的資源量j
{
cout<<" 進程P"<<i<<"申請的資源數大于進程P"<<i<<"還需要"<<j<<"類資源的資源量!";
cout<<"申請不合理,出錯!請重新選擇!"<<endl<<endl;
flag='N';
break;
}
else
{
if(Request[j]>AVAILABLE[j]) //若請求的資源數大于可用資源數
{
cout<<" 進程P"<<i<<"申請的資源數大于系統可用"<<j<<"類資源的資源量!";
cout<<"申請不合理,出錯!請重新選擇!"<<endl<<endl;
flag='N';
break;
}
}
}
if(flag=='Y'||flag=='y')
{
changdata(i); //調用changdata(i)函數,改變資源數
if(chkerr(i)) //若系統不安全
{
rstordata(i); //調用rstordata(i)函數,恢復資源數
showdata(); //輸出資源分配情況
}
else { //若系統安全
showdata(); //輸出資源分配情況
/* super buge 沒有釋放擁有的資源*/
set();
}
}
else //若flag=N||flag=n
showdata();
cout<<endl;
cout<<" 是否繼續銀行家算法演示,按'Y'或'y'鍵繼續,按'N'或'n'鍵退出演示: ";
cin>>flag;
}
}
/*void set()
{for(int i=0;i<=M;i++)
if (flag=='y'||flag=='Y')
cout<<"進程p"<<i<<"狀態: 運行結束"<<endl;
else
cout<<"進程p"<<i<<"狀態: 等待調用"<<endl;
}*/
void main() //主函數
{
cout<<"********************************"<<endl;
cout<<"歡迎使用銀行家安全性算法模擬程序"<<endl;
cout<<endl;
cout<<" 本程序由曉光編制完成 "<<endl;
cout<<" gyzhangxiaoguang@126.com "<<endl;
cout<<" 學號061114031 班級2006教育 "<<endl;
cout<<endl;
cout<<"********************************"<<endl;
int i=0,j=0,p;
do{cout<<"請輸入總進程數:"<<endl;
cin>>M;}while(M<0);
do{cout<<"請輸入總資源種類:"<<endl;
cin>>N;}while(N<0);
cout<<"請輸入總資源數(all_resource):"<<endl;
for(i=0;i<N;i++)
{cout<<"資源"<<i<<":"; cin>>ALL_RESOURCE[i];}
cout<<"依次輸入各進程所需要的最大資源數量(max):"<<endl;
for (i=0;i<M;i++)//M是總進程個數
{
for (j=0;j<N;j++)//N是總資源種類
{ // cout<<"進程"<<i<<"所需";
do
{
cout<<"進程"<<i<<"所需";
cout<<j<<"類資源的數目:";
cin>>MAX[i][j];
if (MAX[i][j]>ALL_RESOURCE[j]) //最大需求量小于資源的總數
cout<<endl<<"所需資源超過了聲明的該資源總數,請重新輸入"<<endl;
}while (MAX[i][j]>ALL_RESOURCE[j]);
}
}
cout<<"依次輸入各進程已經占據的資源數量(allocation):"<<endl;
for (i=0;i<M;i++)
{ //cout<<"進程"<<i+1<<"已占有";
for (j=0;j<N;j++)
{//cout<<"進程"<<i<<"已占有";
do
{cout<<"進程"<<i<<"已占有";
cout<<j<<"類資源的數目:";
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];
safe();
//cout<<endl<<"手動申請資源"<<endl;
//showdata();
// safe();
if (test()==0);
// showdata();
else{
char letter;
cout<<endl<<"如果需要手動為進程申請資源,\n請輸入字母 k :";
cin>>letter;
if (letter=='k')
{showdata();
bank();};
//set();
};
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -