#include <iostream>
using namespace std;
#define MaxPro 50 /*最大进程数*/
#define MaxRes 100 /*最大资源数*/
int Available[MaxRes]; /*可用资源数组*/
int Max[MaxPro][MaxRes]; /*最大需求矩阵*/
int Allocation[MaxPro][MaxRes]; /*分配矩阵*/
int Need[MaxPro][MaxRes]; /*需求矩阵*/
int Request[MaxPro][MaxRes]; /*进程需要资源数*/
bool Finish[MaxPro]; /*系统是否有足够的资源分配*/
int p[MaxPro]; /*记录序列*/
int m, n; /*m个进程,n个资源*/
void Init() /*初始化算法*/
{
int i, j;
cout << "请输入进程的数目:";
cin >> m;
cout << "请输入资源的种类:";
cin >> n;
cout << "请输入每个进程最多所需的各资源数,按照" << m << "x" << n << "矩阵输入" << endl;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
cin >> Max[i][j];
}
cout << "请输入每个进程已分配的各资源数,按照" << m << "x" << n << "矩阵输入" << endl;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
cin >> Allocation[i][j];
Need[i][j] = Max[i][j] - Allocation[i][j];
if (Need[i][j] < 0)
{
cout << "您输入的第" << i << "个进程所拥有的第" << j << "个资源数错误,请重新输入:" << endl;
j--;
continue;
}
}
}
cout << "请输入各个资源现有的数目:" << endl;
for (i = 0; i < n; i++)
{
cin >> Available[i];
}
}
void ShowData(int m, int n) //显示
{
int i, j, count;
cout << endl;
cout << "-------------------------------------------------------------" << endl;
cout << "系统可用资源数为:";
for (j = 0; j < n; j++)
{
cout << " " << Available[j];
}
cout << endl;
cout << "各进程仍需资源量:" << endl;
for (i = 0; i < m; i++)
{
count = 0;
for (j = 0; j < n; j++)
{
if (Allocation[i][j] == 0 && Need[i][j] == 0)
{
count++;
}
}
if (count == n)
{
continue;
}
cout << " 进程" << i << ":";
for (j = 0; j < n; j++)
{
cout << " " << Need[i][j];
}
cout << endl;
}
cout << endl;
cout << "各进程已得到的资源量: " << endl
<< endl;
for (i = 0; i < m; i++)
{
count = 0;
for (j = 0; j < n; j++)
{
if (Allocation[i][j] == 0 && Need[i][j] == 0)
{
count++;
}
}
if (count == n)
{
continue;
}
cout << " 进程" << i << ":";
for (j = 0; j < n; j++)
{
cout << " " << Allocation[i][j];
}
cout << endl;
}
cout << endl;
}
bool Safe() /*安全性算法*/
{
int i, j, k, count, l = 0;
int Work[MaxRes]; /*工作数组*/
for (i = 0; i < n; i++)
{
Work[i] = Available[i];
}
for (i = 0; i < m; i++)
{
count = 0;
for (j = 0; j < n; j++)
{
if (Allocation[i][j] == 0 && Need[i][j] == 0)
{
count++;
}
}
if (count == n)
{
Finish[i] = true;
}
else
{
Finish[i] = false; //Finish记录每个进程是否安全
}
}
circle1:
while (1)
{
count = 0;
circle2:
for (i = 0; i < m; i++) //循环查找第i个进程需要的各个资源数是否超过系统现有的对应的资源数
{
if (Finish[i] == true)
{
count++;
continue;
}
for (j = 0; j < n; j++)
{
if (Need[i][j] > Work[j]) //第i个进程需要的第j个资源>系统现有的第j个资源数
{
if (i == m - 1)
{
break circle1;
}
continue circle2;
}
}
if (j == n) //如果第i个进程所需的各个资源数都没有超过系统现有的对应资源数
{
Finish[i] = true; //给该进程的Finish记为true
count++;
for (k = 0; k < n; k++)
{
Work[k] += Allocation[i][k]; //进程运行结束,已分配资源全部归还系统
}
p[l++] = i; //记录进程号
}
}
if (count == m)
{
break;
}
}
count = 0;
for (i = 0; i < m; i++)
{
if (Finish[i] == true)
{
count++;
}
}
if (count == m) //当所有进程都能够被满足运行时
{
cout << "系统是安全的" << endl;
cout << "安全序列:" << endl;
for (i = 0; i < l; i++)
{
cout << p[i];
if (i != l - 1)
{
cout << "-->";
}
}
cout << "" << endl;
return true;
}
else
{
cout << "系统是不安全的" << endl;
return false;
}
}
void Bank() /*银行家算法*/
{
int i, CusNeed, count, flag = 0, number;
char again;
while (1)
{
ShowData(n, m);
cout << endl;
nummber = 0;
for (i = 0; i < m; i++)
{
if (Finish[i] == false)
{
number++;
}
}
if (number == m)
{
cout << "所有进程都以执行完毕!" << endl;
return;
}
cout << "请输入需要申请资源的进程号(注第一个进程号为0,依次类推)" << endl;
input1:
cin >> CusNeed;
if (CusNeed >= m)
{
cout << "非法输入,请重新输入:";
goto input1;
}
count = 0;
for (i = 0; i < n; i++)
{
if (Allocation[CusNeed][i] == 0 && Need[CusNeed][i] == 0)
{
count++;
}
}
if (count == n)
{
Finish[CusNeed] = false;
cout << "该进程已被执行完成,请重新选择进程:";
goto input1;
}
count = 0;
for (i = 0; i < n; i++)
{
if (Need[CusNeed][i] == 0)
{
count++;
}
}
if (count == n)
{
cout << "该进程的资源已满足执行需求!" << endl;
}
else
{
cout << "请输入进程所请求的各资源的数量" << endl;
for (i = 0; i < n; i++)
{
input2:
cin >> Request[CusNeed][i];
}
for (i = 0; i < n; i++)
{
if (Request[CusNeed][i] > Need[CusNeed][i]) //如果用户选择的线程数的第i个资源请求数>该线程资源所需的数量
{
cout << "请求数非法!请重新输入:" << endl;
goto input2;
}
}
}
for (i = 0; i < n; i++) //如果请求合理,那么下面
{
Available[i] -= Request[CusNeed][i]; //系统资源减去申请了的
Allocation[CusNeed][i] += Request[CusNeed][i]; //线程已分配的资源加上申请了的
Need[CusNeed][i] -= Request[CusNeed][i]; //线程还需要的减去已申请到的
}
if (Safe()) //Available Allocation Need发生变动后是否会导致不安全
{
cout << "同意分配请求!" << endl;
}
else
{
cout << "您的请求已被拒绝!" << endl;
for (i = 0; i < n; i++)
{
Available[i] += Request[CusNeed][i];
Allocation[CusNeed][i] -= Request[CusNeed][i];
Need[CusNeed][i] += Request[CusNeed][i];
}
}
for (i = 0; i < n; i++)
{
if (Need[CusNeed][i] == 0)
{
flag++;
}
}
if (flag == n) //如果该进程各资源都已满足条件,则释放资源
{
for (i = 0; i < n; i++)
{
Available[i] += Allocation[CusNeed][i];
Allocation[CusNeed][i] = 0;
Need[CusNeed][i] = 0;
}
cout << "进程" << CusNeed << "占有的资源都被释放!" << endl;
flag = 0;
}
for (i = 0; i < m; i++) //分配好了以后将进程的标识Finish改成false
{
Finish[i] = false;
}
cout << "您还想再次请求分配吗?是请按y/Y,否请按任意键!" << endl;
cin >> again;
if (again = 'y' || again = 'Y')
{
continue;
}
else
{
return;
}
}
}
int main()
{
Init();
Safe();
Bank();
}
银行家算法(C++)
最新推荐文章于 2021-11-29 22:29:10 发布