操作系统:银行家算法——c/c++实现______适合小白参考(简单)

注意:本代码只适合用于参考(因为有点小错误)
本人刚开始写的时候有些懵就找了很多相关材料,最后写出此代码;
最适合小白和初学者参考(涉及的知识点少)

解题思路
首先你会苦恼这么多参数如何分清楚谁是谁啊:
最开始我想到了二位数组,但最后实现起来比较麻烦-----特别是实现资源的运算上;(本人认为的最难点)
解决方法:
利用class设置资源属性,在运算上直接用指针就能是实现个资源的加减乘除;hhhhh

特别欢迎各位大佬来指导
到底是哪里错了呢。。。。。。。
ok上源码

#include <iostream>
#include<string>
constexpr auto prc_num = 5;;
using namespace std;
class banker {
	public:
		int a;//资源a
		int b;//资源b
		int c;//资源c
};
banker max[prc_num] = {{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3} };
banker allocation[prc_num] = { {0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,2,2} };//已获得资源
banker need[prc_num] = { {7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1} };//还需要多少资源
banker ava={3,2,2} ;//可以分配的资源
int safe[prc_num];	//安全序列
//安全检测
bool safe_check() {
	banker work = ava;
	bool finish[prc_num] = { false,false,false,false,false };
	int i;
	int j = 0;
	for (i = 0; i < prc_num; i++) {
		if (finish[i] == false) {
			if (need[i].a <= work.a&&need[i].b <= work.b&&need[i].c <= work.c) {
				work.a += allocation[i].a;
				work.b+= allocation[i].b;
				work.c+= allocation[i].c;
				finish[i] = true;
				safe[j++] = i;
				i = -1;//重新遍历
			}
		}
	}
	for (i = 0; i < prc_num; i++) {
		if (finish[i] == false){
			return false;}
	}
	return true;
}
//试探分配请求
void prob_res(int prc, banker *res) {
	ava.a -= res->a;
	ava.b -= res->b;
	ava.c -= res->c;

	allocation[prc].a += res->a;
	allocation[prc].b+= res->b;
	allocation[prc].c += res->c;

	need[prc].a -= res->a;
	need[prc].b -= res->b;
	need[prc].c -= res->c;
}
//request分配失败,将已分配的还原
void return_res(int prc, banker *res) {
	ava.a += res->a;
	ava.b += res->b;
	ava.c += res->c;

	allocation[prc].a -= res->a;
	allocation[prc].b -= res->b;
	allocation[prc].c -= res->c;

	need[prc].a += res->a;
	need[prc].b += res->b;
	need[prc].c += res->c;	
}
//请求资源分配
bool request(int prc, banker *res) {
		//如果请求的资源小于需求
	if (res->a <= need[prc].a&&res->b <= need[prc].b&&res->c <= need[prc].c) {
		//如果请求的资源小于可分配资源
		if (res->a <= ava.a&&res->b <= ava.b&&res->c <= ava.c) {
		//试探分配
			prob_res(prc, res);
			if (safe_check())
				return true;
			else {
				cout << "安全性检查失败,原因;系统将进入不安全状态有可能引起死锁" << endl;
				cout << "正在还原已分配的请求数据............." << endl;
				//资源回滚
				return_res(prc, res);
			}
		}
		else cout << "安全性检查失败,原因;请求量大于可利用资源" << endl;
	}
	else cout << "安全性检查失败,原因;请求量大于需求量" << endl;
	return false;
}
void show() {
	cout << "\t\t********资源分配表********" << endl;
	cout << "Process \t Max \t Allocation \t Need \t Available" << endl;
	cout << "\t\t a b c \t a b c \t\t a b c \t a b c" << endl;
	//笨方法的输出,不知道如何直接一次性的将类里的所有属性全部输出
	cout << "P0" << "\t\t " << max[0].a << " " << max[0].b << " " << max[0].c << "\t " << allocation[0].a << " " << allocation[0].b << " " << allocation[0].c << "\t\t " << need[0].a << " " << need[0].b << " " << need[0].c << "\t " << ava.a << " " <<ava.b << " " <<ava.c <<  endl;
	for (int i = 1; i < 5; i++) {
		cout << "P" << i << "\t\t " << max[i].a << " " << max[i].b << " " << max[i].c << "\t " << allocation[i].a << " " << allocation[i].b << " " << allocation[i].c << "\t\t " << need[i].a << " " << need[i].b << " " << need[i].c <<endl;
	}
}
int main() {
	string choose;
	cout << "检查初始状态是否安全........" << endl;
	if (safe_check) {
		cout << "系统处于安全状态" << endl;
		cout << "安全序列:";
		for (int i = 0; i < 5; i++) {
			cout << safe[i];
		}
		cout << endl; cout << endl;
	}
	else {
		cout << "系统处于不安全状态,程序将退出......" << endl;
		goto over;
	}
	do {
		int prc;
		banker res;
		show();
		cout << "请分别输入请求分配的进程名和三类资源的数量" << endl;
		cin >> prc >> res.a >> res.b >> res.c;
		if (request(prc, &res)) {
			cout << "分配成功" << endl;
			cout << "安全序列为:" << endl;
		}
		else {
			cout << "分配失败" << endl;
		}
		cout << "是否继续分配(Y/N)";
		cin >>choose;
	} while (choose == "Y" || choose == "y");
over: 
	cout << "执行完毕" << endl;	
	return 0;
}



无论怎么请求资源安全序列都是00000
但我没感觉我safe_check()写错了啊

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值