C语言模拟银行家算法
小弟写了一个模拟银行家算法的c程序,感觉思路没问题,但是就是得不出正确的结果,纠结4、5天了,实在没辙了,所以来求助各位大神们。帮忙看看到底是什么问题,感激不尽
源代码:
#include <iostream>
#include <cstring>
using namespace std;
#define false 0
#define true 1
int l, m, n, p, s = 0; //l:计数器,m:系统资源种数,n:进程数,p:申请资源的进程号 s:标记InitPRequest是否执行过
int Max[10][10]; //最大需求矩阵
int Allocation[10][10]; //已分配资源矩阵
int Available[10]; //当前资源剩余向量
int Need[10][10]; //需求矩阵
int Request[10][10]; //资源请求向量
int Safe[10]; //记录进程执行顺序
void Menu() //菜单界面
{
cout << "--------------------银行家算法测试----------------------" << endl;
cout << "| 1.输入系统的资源数、申请进程数、每个类资源的实例数 |" << endl;
cout << "| 2.输入进程的资源申请 |" << endl;
cout << "| 3.输出系统状态 |" << endl;
cout << "| 4.退出程序 |" << endl;
cout << "|______________________________________________________|" << endl;
}
void InitResource() //输入系统的资源数、申请进程数、每个类资源的实例数
{
int i, j;
cout << "请输入系统资源种数" << endl;
cin >> m;
cout << "请输入进程数" << endl;
cin >> n;
cout << "-----Max-----" << endl; //初始化 Max 最大需求矩阵
for(i = 0; i < m; i++) //资源列名的显示
cout << " " << (char)('A' + i);
cout << endl;
for(i = 0; i < n; i++)
{
cout << "P" << i << " "; //进程行名的显示
for(j = 0; j < m; j++)
cin >> Max[i][j];
}
cout << " -----Allocation----" << endl; //初始化 Allocation 已分配资源矩阵
for(i = 0; i < m; i++) //资源列名的显示
cout << " " << (char)('A' + i);
cout << endl;
for(i = 0; i < n; i++)
{
cout << "P" << i << " "; //进程行名的显示
for(j = 0; j < m; j++)
cin >> Allocation[i][j];
}
for(i = 0; i < n; i++) //初始化 Need 需求矩阵
for(j = 0; j < m; j++)
Need[i][j] = Max[i][j] - Allocation[i][j];
cout << "-----Available-----" << endl; //初始化 Available 当前资源剩余向量
for(i = 0; i < m; i++) //资源列名的显示
cout << (char)('A' + i) << ' ';
cout << endl;
for(i = 0; i < m; i++)
cin >> Available[i];
}
void InitPRequest() //输入进程的资源申请
{
int i;
cout << "输入申请资源的进程号:";
cin >> p;
cout << "输入该进程的资源申请:" << endl;
for(i = 0; i < m; i++) //资源列名的显示
cout << (char)('A' + i) << ' ';
cout << endl;
for(i = 0; i < m; i++)
cin >> Request[p][i];
s = 1; //s标记 InitPRequest 已成功执行,Request向量已成功初始化
}
void ProbeAlloc(int p) //试探分配
{
int i;
for(i = 0; i < m; i++)
{
Available[i] -= Request[p][i];
Need[p][i] -= Request[p][i];
Allocation[p][i] += Request[p][i];
}
}
void RollBack(int p) //若试探分配后进入不安全状态,将分配回滚
{
int i;
for(i = 0; i < m; i++)
{
Available[i] += Request[p][i];
Need[p][i] += Request[p][i];
Allocation[p][i] -= Request[p][i];
}
}
bool SafeCheck() //安全性检查函数
{
int Work[10], i, j, k = 0;
bool Finish[5] = {false,false,false,false,false};//进程是否执行过标志
for(i = 0; i < m; i++)
Work[i] = Available[i]; //Work数组为Aailable的一个临时值
for (i = 0; i < n; i++)
{
l = 0;
//该进程是否已执行完毕
if(Finish[i] == false)
{
//是否有足够的资源分配给该进程
for(j = 0; j < m; j++)
{
if(Need[i][j] <= Work[i])
l++;
}
if(l == m)
{
Finish[i] = true;
Safe[k++] = i; //顺便记录下来安全序列
i = -1; //需要从开始重新进行遍历
for(j = 0; j < m; j++)
Work[j] += Allocation[i][j];
}
}
}
//如果所有进程的Finish向量都为true则处于安全状态,否则为不安全状态
for (i = 0; i < n; i++)
{
if (Finish[i] == false)
{
return false;
}
}
return true;
}
bool request(int p)//资源分配请求(以上三个函数的基础上)
{
//Request向量需小于Need矩阵中对应的向量
int i, j, k = 0, l = 0; //k、l为计数器
for(i = 0; i < m; i++)
{
if(Request[p][i] <= Need[p][i]) //Request(A, B, C, …) <= Available(A, B, C, …) <= Need(A, B, C, …)
k++; //通过计数器来统计的判断逻辑值,以此判断是否每种资源都满足上面的关系
if(Request[p][i] <= Available[i])
l++;
}
if(k == m)
{
if(l == m)
{
//试探分配
ProbeAlloc(p);
//如果安全检查成立,则请求成功,否则将分配回滚并返回失败
if(SafeCheck())
{
return true;
}
else
{
cout << "安全性检查失败。原因:系统将进入不安全状态,有可能引起死锁。" << endl;
cout << "正在回滚...\n" << endl;
RollBack(p);
}
}
else
{
cout << "安全性检查失败。原因:请求向量大于可利用资源向量。" << endl;
}
}
else
{
cout << "安全性检查失败。原因:请求向量大于需求向量。" << endl;
}
return false;
}
void SystemOut() //输出系统状态(建立在request函数基础上)
{
char ch;
int i;
if (SafeCheck())
{
cout << "系统处于安全状态." << endl;
cout << "安全序列是{";
for(i = 0; i < n - 1; i++)
cout << "P" << Safe[i] << ",";
cout << "P" << Safe[n-1] << "}" << endl;
}
else
{
cout << "系统处于不安全状态。程序将退出..." << endl;
return;
}
do
{ if(s)
{
if (request(p))
{
cout << "分配成功。" << endl;;
cout << "安全序列是{";
for(i = 0; i < n - 1; i++)
cout << "P" << Safe[i] << ",";
cout << "P" << Safe[n-1] << "}" << endl;
}
else
{
cout << "分配失败。" << endl;
}
cout << "是否继续分配?(Y/N):" << endl;
cin >> ch;
}
else
{
cout << "请先初始化 Request 进程资源请求向量!" << endl;
InitPRequest();
}
} while (ch == 'Y' || ch == 'y');
}
int main()
{
int i = 1, j;
char k;
while(i)
{
Menu(); //菜单显示
cout << "命令:";
cin >> k;
switch(k) //菜单选择
{
case '1':
InitResource();
break;
case '2':
InitPRequest();
break;
case '3':
SystemOut();
break;
case '4':
i = 0; //i由1变为0,while循环退出
cout << "再见!" << endl;
break;
default:
cout << "请输入正确的命令!" << endl;
}
}
return 0;
}
测试数据参考
---MAX-- --Allocation-
A B C A B C
P0 5 5 9 2 1 2
P1 5 3 6 4 0 2
P2 4 0 11 4 0 5
P3 4 2 5 2 0 4
P4 4 2 4 3 1 4
---Available----
A B C
2 3 3
当前系统状态是安全的,安全序列为{P3,P4,P0,P1,P2},但是我的程序输出是不安全的