银行家算法模拟

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define PRO 5	//进程数 
#define RES 3	//资源数 

int maximum[PRO][RES], al[PRO][RES], need[PRO][RES], ava[RES];//最大需求矩阵、分配矩阵、需求矩阵、可利用资源向量
char PName[5] = { 'a', 'b', 'c', 'd', 'e' }; //进程名初始化
int finish[PRO] = { 0 }, work[PRO], safe[PRO]; //进程是否已分配资源标识位、工作向量、安全序列

void safety();  //安全性算法
void init();    //初始化
void show();    //输出资源申请及变化情况
void init()
{
	cout << "请依次输入每个进程对每种资源的最大需求:\n";
	for (int i = 0; i < PRO; i++)
	{
		for (int j = 0; j < RES; j++)
			cin >> maximum[i][j];
	}
	cout << "请依次输入已分配给每个进程的每种资源数目:\n";
	for (int i = 0; i < PRO; i++)
	{
		for (int j = 0; j < RES; j++)
			cin >> al[i][j];
	}
	cout << "请依次输入每个进程目前对每种资源的需求:\n";
	for (int i = 0; i < PRO; i++)
	{
		for (int j = 0; j < RES; j++)
			cin >> need[i][j];
	}
	cout << "请依次输入每种资源的可利用资源数目:\n";
	for (int i = 0; i < RES; i++)
	{
		cin >> ava[i];
		work[i] = ava[i];     //给工作向量赋初值
	}
}
void show()
{
	cout << "\tMax\tAllocation\tNeed\tAvailable\n";
	cout << "      A  B  C \t A  B  C      A  B  C \t A  B  C \n";
	for (int i = 0; i < PRO; i++)
	{
		cout << PName[i] << "   ";
		for (int j = 0; j < RES; j++)
			cout << "  " << maximum[i][j];
		cout << "  ";
		for (int j = 0; j < RES; j++)
			cout << "  " << al[i][j];
		cout << "    ";
		for (int j = 0; j < RES; j++)
		{
			cout << "  " << need[i][j];
		}
		cout << "\n";
	}
	cout << "  ";
	for (int i = 0; i < RES; i++)
	{
		cout << "  " << ava[i];
	}
	cout << "\n";
}
void safety()
{
	int num = 0; //分配资源完成的进程数 
	for (int i = 0; i < PRO; i++)
	{
		int n = 0;	//给该进程已分配的资源种类数 
		for (int j = 0; j < RES; j++)
		{
			if (finish[i] == 0 && work[j] >= need[i][j])//若此进程未分配资源且申请资源数小于等于需要的资源数 ,则分配 
			{
				n++;
				if (n == RES)//该进程获得资源,可顺利执行 
				{
					for (int k = 0; k < RES; k++)
					{
						work[k] = work[k] + al[i][k];//释放资源
					}
					finish[i] = 1;
					safe[num] = i;//给安全序列赋值 
					i = -1;
					num++;//分配资源完成的进程数加一 
					cout<<num<<" "; 
				}
			}
		}
	}
	for (int i = 0; i<PRO; i++)  
	{
		if (finish[i] == 0)
		{
			cout << "不安全" << endl;
			return;
		}
	}
	cout << "安全,当前系统的一个安全序列为:" << endl;
	for (int i = 0; i<PRO; i++)
		cout << " " << PName[safe[i]];
	cout << "." << endl;
}
int main()
{
	char con = 'y', APName;
	int n, request[RES], a = 0;
	init();
	show();
	safety();
	while (con == 'y')
	{
		finish[PRO] = { 0 };
		for (int j = 0; j<PRO; j++)
		{
			finish[j] = 0;
		}
		cout << "继续提出申请?" << "\n" << "y为是;n为否。" << endl;
		cin >> con;
		if (con == 'y')
		{

			for (int i = 0; i<PRO; i++)
				cout << PName[i] << "\t";
			cout << "\n" << "输入提出资源申请的进程名:" << endl;
			cin >> APName;
			while (APName<'a' || APName>'e')
			{
				cout << "\n" << "无此进程,请重新输入提出资源申请的进程名:" << endl;
				cin >> APName;
			}
			for (int i = 0; i<PRO; i++)
				{
					if (APName == PName[i])
						n = i;
				}
				cout << "输入申请各类资源的数量:" << endl;
				for (int j = 0; j<RES; j++)
				{
					cin >> request[j];
				}
				for (int j = 0; j<RES; j++)
				{
					if (request[j]>need[n][j])  //请求资源量大于所需资源量
					{
						a = 1;
					}
					else if (request[j]>ava[j]) //请求资源量大于可利用资源量
					{
						a = 2;
					}
					else
					{
						a = 3;
					}
				}
				if (a == 1)
				{
					cout << "申请大于需求,wrong!" << endl;
					return 0;
				}
				else if (a == 2)
				{
					cout << "无足够资源分配,阻塞!" << endl;
					return 0;
				}
				else if (a == 3)
				{
					for (int j = 0; j < RES; j++)  //资源变化
					{
						ava[j] -= request[j];
						work[j] = ava[j];
						al[n][j] += request[j];
						need[n][j] -= request[j];
					}
					show();     //输出资源变化情况         
					safety();   //调用安全性算法
				}
		}
		else return 0;
	}
	return 0;
}  
    原文作者:银行家问题
    原文地址: https://blog.csdn.net/c_y_w_/article/details/80848557
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞