用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);
}

在这里插入图片描述
在这里插入图片描述

  • 6
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值