#include<iostream>
using namespace std;
int N = 3; // 资源种类
int M = 5; // 进程数
int AllResource[3] = {10,5,7}; // 各资源总数
int Max[5][3] = {{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}}; // 最大资源需求量
int Allocation[5][3] = {{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}}; // 已得到各类资源量
int Available[3]; // 系统可用资源数
int Need[5][3]; // 还需资源量
bool Finish[5]; // 进行完成情况
int Work[3]; // 工作向量
int Request[5][3]; // 请求资源个数
// 初始化
void init() {
for(int i = 0; i < N; i++) {
int sum = 0;
for(int j = 0; j < M; j++) {
sum += Allocation[j][i];
Need[j][i] = Max[j][i] - Allocation[j][i];
}
Available[i] = AllResource[i] - sum;
sum = 0;
}
for(int i = 0; i < M; i++) Finish[i] = false;
for(int i = 0; i < N; i++) Work[i] = Available[i];
}
void showData() //函数showdata,输出资源分配情况
{
cout<<"All:"<<" (";
for(int j = 0; j < N; j++) cout<<" "<<AllResource[j];
cout<<" )"<<endl<<"Avaliable:"<<" (";
for (int j = 0;j < N; j++)cout<<" "<<Available[j];
cout<<" )"<<endl;
cout<<"Allocation: "<<endl<<" \t";
for(int i = 0; i < N; i++) cout<<"资源"<<i<<" \t";
cout<<endl;
for(int i = 0; i < M; i++) {
cout<<"进程p"<<i<<": ";
for(int j = 0; j < N; j++) cout<<Allocation[i][j]<<" ";
cout<<endl;
}
cout<<"Need:"<<endl<<" \t";
for(int i = 0; i < N; i++) cout<<"资源"<<i<<"\t";
cout<<endl;
for(int i = 0; i < M; i++){
cout<<"进程p"<<i<<": ";
for (int j = 0; j < N; j++) cout<<Need[i][j]<<" ";
cout<<endl;
}
cout<<endl;
}
// 检查当前系统是否安全
bool checkSafe() {
init(); // 初始化
for(int i = 0; i < M; i++) {
if(Finish[i] == false) {
int flag = 0;
for(int j = 0; j < N; j++) {
if(Need[i][j] > Work[j]) {
flag = 1; // 系统无法有充足的资源分配给该进程
break;
}
}
// 如果可以分配
if(flag == 0) {
Finish[i] = true;
// 运行完回收资源
for(int k = 0; k < N; k++) {
Work[k] += Allocation[i][k];
}
i = -1; // 从头继续搜索
}
}
}
bool isSafe = 1;
cout<<"安全序列为:";
for(int i = 0; i < M; i++) {
if(Finish[i] == false) isSafe = 0;
else cout<<i<<" ";
}
if(!isSafe) return false;
return true;
}
// 银行家算法
void bankAlgorithm(int p) {
init(); // 初始化
if(p < 0 || p > M) {
cout<<"输入的进程数有误!"<<endl;
return;
}
// 输入请求的资源数量
cout<<"请输入请求的资源数量: ";
for(int i = 0; i < N; i++) cin>>Request[p][i];
// 检查是否可以分配这些资源
for(int i = 0; i < N; i++) {
if(Request[p][i] > Need[p][i] || Request[p][i] > Available[i]) {
cout<<"无法分配!"<<endl;
return;
}
}
// 如果可以分配,假定分配
for(int i = 0; i < N; i++) {
Available[i] -= Request[p][i];
Allocation[p][i] += Request[p][i];
Need[p][i] -= Request[p][i];
}
// 检查系统安全性
if(checkSafe()) {
cout<<"分配成功!新的配置列表如下:"<<endl;
// 若成功运行,回收资源
int f = 0;
for(int i = 0; i < N; i++) {
if(Need[p][i] != 0) {
f = 1;
break;
}
}
if(f == 0) {
for(int i = 0; i < N; i++) {
Available[i] += Allocation[p][i];
Allocation[p][i] = 0;
Max[p][i] = 0;
}
}
showData();
return;
}
else {
cout<<"分配失败!"<<endl;
// 分配失败,恢复数据
for(int i = 0; i < N; i++) {
Available[i] += Request[p][i];
Allocation[p][i] -= Request[p][i];
Need[p][i] += Request[p][i];
}
return;
}
}
int main() {
cout<<"初次检查系统:";
if(checkSafe()) cout<<" 系统安全"<<endl<<endl;
else cout<<" 系统不安全"<<endl<<endl;
showData();
// 循环调用银行家算法
while(true) {
cout<<"请输入需要分配的进程号(输入-1退出):";
int p;
cin>>p;
if(p == -1) break;
bankAlgorithm(p);
}
return 0;
}
模拟实现银行家算法
最新推荐文章于 2022-12-06 17:57:35 发布