用C++實現銀行家算法(代碼)

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

void Max(vector<vector<int>>& max, const int m, const int n) {
	cout << "請輸入各進程最多還需要的資源數 按照" << m << "*" << n << "矩陣輸入" << endl;

	for (int i = 0; i < m; i++) {
		vector<int> b;//每一個進程的還需資源數組
		int resource;//還需的資源
		for (int j = 0; j < n; j++) {
			cin >> resource;
			b.push_back(resource);
		}
		max.push_back(b);//將資源數組放入所需資源素組
	}
}

void Allocation(vector<vector<int>>& allocation, const int m, const int n) {
	cout << endl<<"請輸入各進程已分配的資源數 按照" << m << "*" << n << "矩陣輸入" << endl;

	for (int i = 0; i < m; i++) {
		vector<int> b;//每一個進程的還需資源數組
		int resource;//還需的資源
		for (int j = 0; j < n; j++) {
			cin >> resource;
			b.push_back(resource);
		}
		allocation.push_back(b);//將資源數組放入所需資源素組
	}
}

void Available(vector<int> &available,const int n) {
	cout << endl << "請輸入每個資源現有的數目 " << endl;
	int resource;
	for (int i = 0; i < n; i++)
	{
		cin >> resource;
		available.push_back(resource);
	}
}

void Need(vector<vector<int>> &max, vector<vector<int>> &allocation,vector<vector<int>> &need,int m,int n) {
	need.resize(m * n);
	vector<int>t(n, 0);
	vector<vector<int>>T(m,t);
	need = T;
	for (int i = 0; i < m; i++) {
		for (int j = 0; j < n; j++) {
			int f = max[i][j] - allocation[i][j];
			need[i][j];
			need[i][j] = max[i][j] - allocation[i][j];
		}
	}

}

void safe(vector<int> &available, vector<vector<int>>& allocation, vector<vector<int>>& need, const int m, const int n) {
	
	while(true){
	while(true){
	vector<int> work = available;
	vector<bool>Finsh(m, false);
	int mark1=0;
	int mark2=0;
	int mark3 = 0;
	int *route=new int[m];
	for(int z=0;z<m;z++){
	for (int i = 0; i < m; i++) {
		if ( Finsh[i]==true){//判斷是否需要資源
			continue;
		}
		mark1 = 0;
		for (int j = 0; j < n; j++) {//用於判斷work是否大於need
			
			if (work[j] < need[i][j]) {
				continue;
			}
			mark1 += 1;
		}
		if (mark1 == n) {//當work大於need

			Finsh[i] = true;//狀態
			for (int f = 0; f < n; f++) {
				work[f] += allocation[i][f];//剩餘資源增加
				allocation[i][f] = 0;
				need[i][f] = 0;//該進程所需資源修改爲零
			}
			
			
			route[mark3] = i;//記錄路線
			mark3 += 1;
		}
	}
	
	}

	for (int i = 0; i < m; i++) {

		if (Finsh[i] == false) {
			cout << endl << "線程不是安全的! ";
			break;
		}
		mark2 += 1;
	}
	if (mark2 == m) {
		cout << endl << "線程是安全的!"<<endl ;
		cout << "系統安全序列是" << endl;
		for (int i = 0; i < m; i++) {
			if (i == m - 1) {
				cout << route[i];
				break;
			}
			cout << route[i]  << "----->";
		}
	}
	int x;
	cout <<endl<<"請輸入要申請資源的進程號(從0開始,輸入小於0結束)"<<endl ;
	cin >> x ;
	if (x < 0) {
		break;
	}
	cout << "依次輸入各個資源的數量"<<endl;
	for (int i = 0; i < n; i++) {
	
		cin >> need[x][i];
	}
}
	char jud;
	cout << "是否再次請求分配Y/N" << endl;;
	cin >> jud;
	if (jud != 'Y' || jud != 'y') {
		break;
	}
	}
}

int main() {
	int m;//進程數
	int n;//資源種類數
	cout<< "請輸入進程數 ";
	cin >> m;
	cout << endl<<"請輸入資源種類數 ";
	cin >> n;
	vector<vector<int>> max;//所需資源素組
	vector<vector<int>> allocation;//已分配資源數組
	vector<int> available;//各個資源現有數目
	vector<vector<int>> need;//各個進程還需資源
	Max(max, m, n);//輸入max
	Allocation(allocation, m, n);//輸入allocation
	Available(available, n);//輸入available
	Need(max,allocation,need,m,n);//用於計算need
	safe(available,allocation, need, m, n);
}

在這裏插入圖片描述
在這裏插入圖片描述

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