Dijkstra在1965年提出的银行家算法是著名的死锁避免算法,这个用于一个银行家给多个顾客贷款的算法可以直接用于操作系统给进程分配资源,这时只要把银行家换成操作系统,把顾客换成进程,把资金换成资源,把银行家决定是否放贷时所用的判断过程(即判断顾客是否有信誉和偿还能力)换成操作系统决定是否分配资源时所用的判断过程(即判断进程是否能及时归还资源)即可。
记录实现银行家算法的C++代码
#include <iostream>
#include <vector>
using namespace std;
void input(vector<vector<int>>& maxs, vector<vector<int>>& allocation, vector<vector<int>>& need, vector<int>& available, int p, int r) {
cout << "请输入最大需求矩阵 max:\n";
for (int i = 0; i < p; i++) {
for (int j = 0; j < r; j++) {
cin >> maxs[i][j];
}
}
cout << "请输入分配矩阵 allocation:\n";
for (int i = 0; i < p; i++) {
for (int j = 0; j < r; j++) {
cin >> allocation[i][j];
}
}
cout << "请输入需求矩阵 need:\n";
for (int i = 0; i < p; i++) {
for (int j = 0; j < r; j++) {
cin >> need[i][j];
}
}
cout << "请输入可用资源向量 available:\n";
for (int i = 0; i < r; i++) {
cin >> available[i];
}
}
bool compare(const vector<int>& m, const vector<int>& n, int r) {
for (int i = 0; i < r; i++) {
if (m[i] < n[i]) {
return false;
}
}
return true;
}
bool isSafeState(const vector<vector<int>>& allocation, const vector<vector<int>>& need, vector<int> available, int p, int r) {
vector<bool> finish(p, false);
vector<int> work = available;
cout << "分配序列:\n";
cout << " allocation need available\n";
for (int count = 0; count < p; count++) {
bool found = false;
for (int i = 0; i < p; i++) {
if (!finish[i] && compare(work, need[i], r)) {
for (int j = 0; j < r; j++) {
work[j] += allocation[i][j];
}
finish[i] = true;
found = true;
cout << "\n进程" << i + 1 << "\t";
for (int j = 0; j < r; j++) {
cout << " " << allocation[i][j] << " ";
}
cout << " ";
for (int j = 0; j < r; j++) {
cout << " " << need[i][j] << " ";
}
cout << " ";
for (int j = 0; j < r; j++) {
cout << " " << work[j] << " ";
}
break;
}
}
if (!found) {
cout << "\n";
return false; // No safe sequence found
}
}
cout << "\n";
return true; // Safe sequence found
}
void requestResource(vector<vector<int>>& allocation, vector<vector<int>>& need, vector<int>& available, const vector<int>& request, int n, int p, int r) {
if (compare(available, request, r) && compare(need[n], request, r)) {
for (int j = 0; j < r; j++) {
allocation[n][j] += request[j];
need[n][j] -= request[j];
available[j] -= request[j];
}
if (isSafeState(allocation, need, available, p, r)) {
cout << "允许进程 " << n + 1 << " 申请资源!\n";
}
else {
cout << "不允许进程 " << n + 1 << " 申请资源!系统恢复到之前的状态。\n";
for (int j = 0; j < r; j++) {
allocation[n][j] -= request[j];
need[n][j] += request[j];
available[j] += request[j];
}
}
}
else {
cout << "申请资源量越界!\n";
}
}
int main() {
int p, r;
cout << "请输入进程数:";
cin >> p;
cout << "请输入资源种类数:";
cin >> r;
vector<vector<int>> maxs(p, vector<int>(r));
vector<vector<int>> allocation(p, vector<int>(r));
vector<vector<int>> need(p, vector<int>(r));
vector<int> available(r);
input(maxs, allocation, need, available, p, r);
if (isSafeState(allocation, need, available, p, r)) {
cout << "存在安全序列,初始状态安全。\n";
}
else {
cout << "不存在安全序列,初始状态不安全。\n";
}
int n;
cout << "请输入发出请求向量 request 的进程编号:";
cin >> n;
n--; // To convert to 0-based index
vector<int> request(r);
cout << "请输入请求向量 request:\n";
for (int i = 0; i < r; i++) {
cin >> request[i];
}
requestResource(allocation, need, available, request, n, p, r);
return 0;
}