#include <iostream>
#define MAXPROCESS 50 /*最大进程数*/
#define MAXRESOURCE 100 /*最大资源数*/
using namespace std;
int AVAILABLE[MAXRESOURCE];/*可用资源数组*/
int MAX[MAXPROCESS][MAXRESOURCE];/*最大需求矩阵*/
int ALLOCATION[MAXPROCESS][MAXRESOURCE];/*分配矩阵*/
int NEED[MAXPROCESS][MAXRESOURCE];/*需求矩阵*/
int REQUEST[MAXPROCESS][MAXRESOURCE];/*进程需要资源数*/
bool FINISH[MAXPROCESS];/*系统是否有足够的资源分配*/
int p[MAXPROCESS];/*记录序列*/
int m, n; /*m个进程,n个资源*/
void Init();
bool Safe();
void Bank();
int main()
{
Init();
Safe();
Bank();
}
void Bank()/*银行家算法*/
{
int i, j;
char btn;
while (1)
{
cout << "请输入要申请资源的进程号(注:第1个进程号为0,依次类推)" << endl;
cin >> j;
cout << "请输入进程所请求的各资源的数量" << endl;
for (i = 0;i<n;i++)
{
cin >> REQUEST[j][i];
}
for (i = 0;i<n;i++)
{
if (REQUEST[j][i]>NEED[j][i])
{
cout << "您输入的请求数超过进程的需求量!" << endl;
continue;
}
if (REQUEST[j][i]>AVAILABLE[i])
{
cout << "您输入的请求数超过系统有的资源数!" << endl;
continue;
}
}
for (i = 0;i<n;i++)
{
AVAILABLE[i] -= REQUEST[j][i];
ALLOCATION[j][i] += REQUEST[j][i];
NEED[j][i] -= REQUEST[j][i];
}