银行家-操作系统

实验项目名称: 银行家算法
一、实验目的
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();
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,我们可以得知银行家算法是一种避免死锁的算法,它以银行借贷系统的分配策略为基础,判断并保证系统的安全运行。面是一个简单的银行家算法的C++代码实现: ```c++ #include <iostream> using namespace std; const int P = 5; // 进程数 const int R = 3; // 资源数 int available[R] = {3, 3, 2}; // 可用资源数 int maxm[P][R] = {{7, 5, 3}, {3, 2, 2}, {9, 0, 2}, {2, 2, 2}, {4, 3, 3}}; // 最大需求矩阵 int allocation[P][R] = {{0, 1, 0}, {2, 0, 0}, {3, 0, 2}, {2, 1, 1}, {0, 0, 2}}; // 已分配矩阵 int need[P][R]; // 需求矩阵 bool isSafe() { int work[R]; bool finish[P] = {0}; for (int i = 0; i < R; i++) { work[i] = available[i]; } int count = 0; while (count < P) { bool found = false; for (int i = 0; i < P; i++) { if (!finish[i]) { int j; for (j = 0; j < R; j++) { if (need[i][j] > work[j]) { break; } } if (j == R) { for (int k = 0; k < R; k++) { work[k] += allocation[i][k]; } finish[i] = true; found = true; count++; } } } if (!found) { return false; } } return true; } int main() { for (int i = 0; i < P; i++) { for (int j = 0; j < R; j++) { need[i][j] = maxm[i][j] - allocation[i][j]; } } if (isSafe()) { cout << "Safe state" << endl; } else { cout << "Unsafe state" << endl; } return 0; } ``` 上述代码实现了银行家算法的安全性检查,其中包括了进程数、资源数、可用资源数、最大需求矩阵、已分配矩阵和需求矩阵等。在主函数中,我们首先计算出需求矩阵,然后调用isSafe()函数进行安全性检查,最后输出检查结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值