实验项目名称: 银行家算法
一、实验目的
1、学习最有代表性的避免死锁算法,Dijkstra提出的银行家算法。
2、利用C++实现线程创建、线程撤销、线程同步程序;
3、完成思考、设计与练习。
二、实验内容
1、输入进程个数、资源个数、进程对资源的最大需求,当前分配状态
2、输入某个进程的资源请求
3、判断是否安全
A.若安全,输出安全的进程序列
B.否则,输出unsafe
三、实验原理
银行家算法是最有代表性的避免死锁算法,是Dijkstra提出的银行家算法。这是由于该算法能用于银行系统现金贷款的发放而得名。
银行家可以把一定数量的资金供多个用户周转使用,为保证资金的安全,银行家规定:
(1)当一个用户对资金的最大需求量不超过很行家现有的资金时可接纳该用户.
(2)用户可以分期贷款,但贷款的总数不能超过最大需求量;
(3)当银行家现有的资金不能满足用户的尚需总数时,对用户的贷款可推迟支付,但总 能使用户在有限的时间里得到贷款;
(4)当用户得到所需的全部资金后,一定能在有限的时间里归还所有资金
四、程序框图
银行家算法N-S流程图:
安全性算法N-S流程图:
五、实验代码
#include
#include
#include
using namespace std;
#define MaxProcess 67
#define MaxResource 100
int Available[MaxResource];//可利用资源总数:某类可利用的资源数目
int Max[MaxProcess][MaxResource];//某个进程对某类资源的最大需求数
int Allocation[MaxProcess][MaxResource];//某类资源已分配给某进程的资源数
int Need[MaxProcess][MaxResource];//某个进程还需要的各类资源数。
int Request[MaxProcess][MaxResource];//某个进程需要资源数
bool Finish[MaxProcess];//系统是否有足够的资源分配
int p[MaxProcess];//记录序列
int m, n;//m个进程,n个资源
void Init() {
int i, j;
cout << “输入进程数” << endl;
cin >> m;
cout << “输入资源的种类” << endl;
cin >> n;
cout << “输入每个进程最多所需要的各资源数(矩阵输入)” << endl;
for(i = 0; i < m; i ++)
for(j = 0; j < n; j ++)
cin >> Max[i][j];
cout << “输入每个进程已分配的各资源数(矩阵输入)” << 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 + 1 << “个进程所拥有的第” << j + 1 << “个资源数错误,请重新输入” << endl;
j --;
continue;
}
}
}
cout << “输入各个资源现有的数目” << endl;
for(i = 0; i < n; i ++) {
cin >> Available[i];
}
}
bool SafeCheck() {
int i, j, k, l = 0;
int Work[MaxResource];
for(i = 0; i < n; i ++)
Work[i] = Available[i];
for(i = 0; i < m; i ++)
Finish[i] = false;
for(i = 0; i < m; i ++) {
if(Finish[i] == true) continue;
else {
for(j = 0; j < n; j ++) {
if(Need[i][j] > Work[j]) break;
}
}
if(j == n) {
Finish[i] = true;
for(k = 0; k < n; k ++) {
Work[k] += Allocation[i][k];
}
p[l ++] = i;
i = -1;
}
else {
continue;
}
}
if(l == 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;
char again;
while(1) {
cout << “输入要申请资源的进程号” << endl;
cin >> cusneed;
cout << “输入进程所请求各资源的数量” << endl;
for(i = 0; i < n; i ++) {
cin >> Request[cusneed][i];
}
for(i = 0; i < n; i ++) {
if(Request[cusneed][i] > Need[cusneed][i]) {
cout << “请求数超过进程的需求量,请重新输入” << endl;
continue;
}
if(Request[cusneed][i] > Available[i]) {
cout << “请求数超过系统有的资源数!请重新输入” << endl;
continue;
}
}
for(i = 0; i < n; i ++) {
Available[i] -= Request[cusneed][i];
Allocation[cusneed][i] += Request[cusneed][i];
Need[cusneed][i] -= Request[cusneed][i];
}
if(SafeCheck()) {
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 < m; i ++) Finish[i] = false;
cout << “请求再次分配请按y,否请按其他键” << endl;
cin >> again;
if(again == ‘y’) continue;
break;
}
}
int main() {
Init();
SafeCheck();
Bank();
}