银行家算法是一种预防死锁的算法。具体算法步骤可以参考百度百科:银行家算法
例子:某系统有A、B、C、D , 4类资源共5个进程(P0、P1、P2、P3、P4)共享,各进程对资源的需求和分配情况如下表所示。
现在系统中A、B、C、D 4类资源分别还剩1、5、2、0个,请按银行家算法回答下列问题:
(1)现在系统是否处于安全状态?
(2)如果现在进程P1提出需求(0、4、2、0)个资源的请求,系统能否满足它的请求?
代码:
#include <iostream>
#define maxP 10
#define maxS 10
using namespace std;
int Available[maxS];
int Max[maxP][maxS];
int Allocation[maxP][maxS];
int Need[maxP][maxS];
int Request[maxS];
int Finish[maxP];
int path[maxP] = { 0 };
int PNum, RNum;
void outPath() {
cout << "系统安全序列是:\n";
cout << "P" << path[0] - 1;
for (int i = 1; path[i] != 0; i++) {
cout << "->P" << path[i] - 1;
}
for (int i = 0; i < PNum; i++) path[i] = 0;
cout << endl;
}
int BankSafe() {
int i, j, l = 0;
int Work[maxS];
for (i = 0; i < RNum; i++) Work[i] = Available[i];
for (i = 0; i < PNum; i++) Finish[i] = 0;
for (i = 0; i < PNum; i++) {
if (Finish[i] == 1)
continue;
else {
for (j = 0; j < RNum; j++) {
if (Need[i][j] > Work[j])
break;
}
if (j == RNum) {
Finish[i] = 1;
for (int k = 0; k < RNum; k++)
Work[k] += Allocation[i][k];
path[l++] = i + 1;
i = -1;
}
else continue;
}
if (l == PNum) {
return 1;
}
}
return 0;
}
void input(int PNum, int RNum) {
cout << "输入每个进程最多所需的各类资源数:\n";
for (int i = 0; i < PNum; i++) {
cout << "P" << i << " : ";
for (int j = 0; j < RNum; j++)
cin >> Max[i][j];
}
cout << "输入每个进程已经分配的各类资源数:\n";
for (int i = 0; i < PNum; i++) {
cout << "P" << i << " : ";
for (int j = 0; j < RNum; j++) {
cin >> Allocation[i][j];
Need[i][j] = Max[i][j] - Allocation[i][j];
if (Need[i][j] < 0) {
cout << "你输入的进程P" << i << "所拥有的第" << j + 1 << "个资源错误,请重新输入:\n";
j--;
continue;
}
}
}
cout << "请输入各个资源现有的数目:\n";
for (int i = 0; i < RNum; i++)
cin >> Available[i];
}
int requestP() {
int Pi;
cout << "输入要申请资源的进程号(0-4):";
cin >> Pi;
Pi;
cout << "输入进程所请求的各资源的数量:";
for (int i = 0; i < RNum; i++)
cin >> Request[i];
for (int i = 0; i < RNum; i++) {
if (Request[i] > Need[Pi][i]) {
cout << "所请求资源数超过进程的需求量!\n";
return 1;
}
if (Request[i] > Available[i]) {
cout << "所请求资源数超过系统所有的资源数!\n";
return 1;
}
}
for (int i = 0; i < RNum; i++) {
Available[i] -= Request[i];
Allocation[Pi][i] += Request[i];
Need[Pi][i] -= Request[i];
}
if (BankSafe()) {
cout << "系统安全!\n";
outPath();
cout << "同意分配请求!\n";
}
else {
for (int i = 0; i < RNum; i++) {
Available[i] += Request[i];
Allocation[Pi][i] -= Request[i];
Need[Pi][i] += Request[i];
}
cout << "请求后,系统不安全,你的请求被拒!\n";
}
return 0;
}
void outDATA() {
int i, j;
cout << "\n系统可用的资源数为 :";
for (j = 0; j < RNum; j++)
cout << " " << Available[j];
cout << endl << "各进程还需要的资源量:" << endl;
for (i = 0; i < PNum; i++) {
cout << "进程 P" << i << " :";
for (j = 0; j < RNum; j++)
cout << " " << Need[i][j];
cout << endl;
}
cout << endl << "各进程已经得到的资源量:" << endl;
for (i = 0; i < PNum; i++) {
cout << "进程 P" << i << " :";
for (j = 0; j < RNum; j++)
cout << " " << Allocation[i][j];
cout << endl;
}
cout << endl;
}
int main() {
cout << "输入进程的数目:";
cin >> PNum;
cout << "输入资源的种类:";
cin >> RNum;
input(PNum, RNum);
if (BankSafe()) {
cout << "当前系统安全!\n";
outPath();
}
else {
cout << "当前系统不安全!\n";
return 0;
}
while (1) {
requestP();
outDATA();
char chose;
cout << "是否再次请求分配?是请按Y/y,否请按N/n:\n";
while (1) {
cin >> chose;
if (chose == 'Y' || chose == 'y' || chose == 'N' || chose == 'n')
break;
else {
cout << "请按要求重新输入:\n";
continue;
}
}
if (chose == 'Y' || chose == 'y')
continue;
else break;
}
return 0;
}
运行结果:
输入进程的数目:5
输入资源的种类:4
输入每个进程最多所需的各类资源数:
P0 : 0 0 1 2
P1 : 1 7 5 0
P2 : 2 3 5 6
P3 : 0 6 5 2
P4 : 0 6 5 6
输入每个进程已经分配的各类资源数:
P0 : 0 0 1 2
P1 : 1 0 0 0
P2 : 1 3 5 4
P3 : 0 6 3 2
P4 : 0 0 1 4
请输入各个资源现有的数目:
1 5 2 0
当前系统安全!
系统安全序列是:
P0->P2->P1->P3->P4
输入要申请资源的进程号(0-4):1
输入进程所请求的各资源的数量:0 4 2 0
系统安全!
系统安全序列是:
P0->P2->P1->P3->P4
同意分配请求!
系统可用的资源数为 : 1 1 0 0
各进程还需要的资源量:
进程 P0 : 0 0 0 0
进程 P1 : 0 3 3 0
进程 P2 : 1 0 0 2
进程 P3 : 0 0 2 0
进程 P4 : 0 6 4 2
各进程已经得到的资源量:
进程 P0 : 0 0 1 2
进程 P1 : 1 4 2 0
进程 P2 : 1 3 5 4
进程 P3 : 0 6 3 2
进程 P4 : 0 0 1 4
是否再次请求分配?是请按Y/y,否请按N/n:
N