//
//操作系统实验:
//利用银行家算法避免死锁
//
#include "iostream.h"
#include "iomanip.h"
#define max_source 20
#define max_process 100
int available[max_source];
int max[ max_process ][ max_source ];
int allocation[ max_process ][max_source ];
int need[ max_process ][ max_source ];
int request[ max_source ];
int requestPID;
int source; //当前系统所具有的资源种类
int process; //当前系统的进程数
//返回值
//1:表示请求的资源>还分配的资源
//2:表示请求的资源>系统剩余的资源
//3:表示不存在安全序列
//0:成功
int banker( int securitySequence[] )
{
int i , j , k;
for( i = 0 ; i < source ; i++ )
{
if( request[i] > need[requestPID][i] )
return 1;
}
for( i = 0 ; i < source ; i++ )
{
if( request[i] > available[i] )
return 2;
}
for( i = 0 ; i < source ; i++ )
{
available[i] -= request[i];
allocation[requestPID][i] += request[i];
need[requestPID][i] -= request[i];
}
int finish[ max_process ] = { 0 };
int work[ max_source ];
int currSelect;
for( i = 0 ; i < source ; i++ )
work[i] = available[i];
for( k = 0 ; k < process ; k++ )
{
bool flag = false;
输出过程
//cout<<"work"<<endl;
//for( i = 0 ; i < source ; i++ )
// cout<<setw(5)<<work[i];
//
//cout<<endl<<"need"<<endl;
for( i = 0 ; i < process && !flag ; i++ )
{
if( finish[i] == 0 )
{
flag = true;
for( j = 0 ; j < source && flag; j++ )
{
//
//cout<<setw(5)<<need[i][j];
if( need[i][j] > work[j] )
flag = false;
}
//
//cout<<endl;
}
currSelect = i;
}
if( !flag )
{
for( j = 0 ; j < source ; j++ )
{
available[j] += request[j];
allocation[requestPID][j] -= request[j];
need[requestPID][j] += request[j];
}
return 3;
}
finish[currSelect] = k+1;
//
//cout<<"allocation"<<endl;
for( j = 0 ; j < source ; j++ )
{
work[j] += allocation[currSelect][j];
//
//cout<<setw(5)<<allocation[currSelect][j];
}
//
//cout<<endl<<"当前选择"<<currSelect<<endl;
//cout<<"----------------------------------------"<<endl;
}
for( i = 0 ; i < process ; i++ )
{
j = 0;
while( finish[j] != i + 1 )
j++;
securitySequence[i] = j;
}
return 0;
}
//setCurrentState
void setCurrentState()
{
source = 3;
process = 5;
int a[] = { 3 , 3 , 2 };
int m[][ max_source ] = {
{ 7 , 5 , 3 } ,
{ 3 , 2 , 2 } ,
{ 9 , 0 , 2 } ,
{ 2 , 2 , 2 } ,
{ 4 , 3 , 3 } ,
};
int al[][ max_source ] = {
{ 0 , 1 , 0 } ,
{ 2 , 0 , 0 } ,
{ 3 , 0 , 2 } ,
{ 2 , 1 , 1 } ,
{ 0 , 0 , 2 }
};
int re[] = { 1 , 0 , 2 };
int i , j;
cout<<"当前可用资源数:"<<endl;
for( i = 0 ; i < source ; i++ )
{
available[i] = a[i];
cout<<setw(5)<<available[i];
}
cout<<endl<<endl;
cout<<"当前进程所需资源的最大数:"<<endl;
for( i = 0 ; i < process ; i++ )
{
for( j = 0 ; j < source ; j++ )
{
max[i][j] = m[i][j];
cout<<setw(5)<<max[i][j];
}
cout<<endl;
}
cout<<endl;
cout<<"当前进程已分配到资源数:"<<endl;
for( i = 0 ; i < process ; i++ )
{
for( j = 0 ; j < source ; j++ )
{
allocation[i][j] = al[i][j];
cout<<setw(5)<<allocation[i][j];
}
cout<<endl;
}
cout<<endl;
cout<<"当前进程还需分配的资源数:"<<endl;
for( i = 0 ; i < process ; i++ )
{
for( j = 0 ; j < source ; j++ )
{
need[i][j] = max[i][j] - allocation[i][j];
cout<<setw(5)<<need[i][j];
}
cout<<endl;
}
cout<<endl;
requestPID = 1;
cout<<"进程 "<<requestPID<<" 请求的资源向量:"<<endl;
for( i = 0 ; i < source ; i++ )
{
request[i] = re[i];
cout<<setw(5)<<request[i];
}
cout<<endl<<endl;
}
void main()
{
//为进行一次模拟实验
//设置系统的当前状态
setCurrentState();
int securitySequence[ max_process ] = { 0 };
cout<<"调用银行家算法..."<<endl;
int error = banker( securitySequence );
if( !error )
{
cout<<"可以响应该请求,响应后存在安全序列:"<<endl;
for( int i = 0 ; i < process ; i++ )
cout<<setw(5)<<securitySequence[i];
cout<<endl;
}
else
{
cout<<"无法响应该请求."<<endl;
}
}