操作系統:銀行家算法——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()寫錯了啊

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章