银行家算法C++代码实现

银行家算法C++代码实现,第1张

银行家算法C++代码实现
#include 
#include 
using namespace std;

const int MAX_PROCESS = 10;//最大进程数
const int MAX_SOURCE = 50;//最大每种资源数
const int MAX_TYPE = 5;//最大资源类型数

class Banker
{
private :
	int available[MAX_TYPE];//可用资源向量
	int maxx[MAX_PROCESS][MAX_TYPE];//最大需求矩阵
	int allocation[MAX_PROCESS][MAX_TYPE];//分配矩阵
	int need[MAX_PROCESS][MAX_TYPE];//需求矩阵
	bool isfinish[MAX_PROCESS];
	int type;//资源类型数目
	int process;//进程数目

public :
	Banker(){}//无参构造函数
	void init();//初始化函数
	void apply();//申请资源函数
	bool safety(int p, int t[]);//安全检测函数
	void display();//显示信息函数
};

void Banker::init()
{
	cout << "ttt初始化" << endl;
	cout << "=======================================================" << endl;
	cout << "请输入资源类型数目 : ";
	cin >> type;
	cout << "请输入进程数目 : ";
	cin >> process;
	cout << "请分别输入每种资源的数量 : ";
	for (int i = 0; i < type; i++) cin >> available[i];
	cout << "请分别输入每个进程对每种资源的最大需求数量 : " << endl;
	for (int i = 0; i < process; i++)
	{
		cout << "进程" << i + 1 << " : ";
		for (int j = 0; j < type; j++) cin >> maxx[i][j];
		memcpy(need, maxx, sizeof maxx);
	}
	memset(allocation, 0, sizeof allocation);
	memset(isfinish, false, sizeof isfinish);
	cout << endl << "tt   初始化完成!" << endl;
	cout << "=======================================================" << endl;
	display();
}

void Banker::display()
{
	cout << "系统当前剩余资源 : ";
	for (int i = 0; i < type; i++)
		cout << "资源" << i + 1 << " : " << available[i] << 't';
	cout << endl << endl;
	cout << "各进程所需的最大资源 : " << endl << endl;
	for (int i = 0; i < type; i++)
		cout << "t" << "资源" << i + 1;
	cout << endl << "--------------------------------------" << endl;
	for (int i = 0; i < process; i++)
	{
		cout << "进程" << i + 1 << " : ";
		for (int j = 0; j < type; j++)
			cout << maxx[i][j] << 't';
		cout << endl;
	}
	cout << endl;
	cout << "各进程已分配的资源 : " << endl << endl;
	for (int i = 0; i < type; i++)
		cout << "t" << "资源" << i + 1;
	cout << endl << "--------------------------------------" << endl;
	for (int i = 0; i < process; i++)
	{
		cout << "进程" << i + 1 << " : ";
		for (int j = 0; j < type; j++)
			cout << allocation[i][j] << 't';
		cout << endl;
	}
	cout << endl;
	cout << "各进程所需的剩余资源 : " << endl << endl;
	for (int i = 0; i < type; i++)
		cout << "t" << "资源" << i + 1;
	cout << endl << "--------------------------------------" << endl;
	for (int i = 0; i < process; i++)
	{
		cout << "进程" << i + 1 << " : ";
		for (int j = 0; j < type; j++)
			cout << need[i][j] << 't';
		cout << endl;
	}
	cout << endl;
}

void Banker::apply()
{
	cout << endl << "ttt申请资源" << endl;
	cout << "=======================================================" << endl;
	cout << "请输入申请资源的进程 : ";
	int p;
	cin >> p;//注意进程i在数组中下标为i - 1,因此后面需要用p - 1
	cout << "请输入申请资源的数量 : ";
	int temp[MAX_TYPE];
	for (int i = 0; i < type; i++) cin >> temp[i];
	if (safety(p - 1, temp))//检查该次分配是否能满足
	{
		for (int i = 0; i < type; i++)//进行分配
		{
			available[i] -= temp[i];
			allocation[p - 1][i] += temp[i];
			need[p - 1][i] -= temp[i];
		}
		for (int i = 0; i < process; i++)//检查是否有程序运行完毕,若有则回收其资源
		{
			bool st = true;
			for (int j = 0; j < type; j++)
				if (need[i][j]) st = false;
			if (st && !isfinish[i])
			{
				cout << "进程" << i + 1 << "运行完毕!" << endl;
				for (int j = 0; j < type; j++)
					available[j] += allocation[i][j];
				isfinish[i] = true;
			}
		}
		bool flag = true;
		for (int i = 0; i < process; i++)
			for (int j = 0; j < type; j++)
				if (need[i][j]) flag = false;
		if (flag)
			cout << "所有进程运行完毕!" << endl;
	}
}

bool Banker::safety(int p, int t[])
{
	for(int i = 0; i < type; i++)
		if (need[p][i] < t[i])
		{
			cout << "申请失败!申请资源数目过多" << endl << endl;
			return false;
		}
	int work[MAX_TYPE];
	int tempneed[MAX_PROCESS][MAX_TYPE];
	memcpy(work, available, sizeof available);
	memcpy(tempneed, need, sizeof need);
	bool finish[MAX_PROCESS];
	memset(finish, false, sizeof finish);
	for (int i = 0; i < type; i++)//暂时尝试分配
	{
		work[i] -= t[i];
		tempneed[p][i] -= t[i];
	}
	bool flag1 = false;
	for (int i = 0; i < process; i++)//检查分配后是否合法
	{
		bool flag2 = true;
		for (int j = 0; j < type; j++)
			if (tempneed[i][j] > work[j])
				flag2 = false;
		if (flag2) flag1 = true;
	}
	if (flag1)
	{
		cout << "分配成功!" << endl << endl;
		return true;
	}
	else
	{
		cout << "分配失败!分配后系统将进入不安全状态" << endl << endl;
		return false;
	}
}

int main()
{
	Banker b;
	b.init();
	while (1)
	{
		cout << "1.申请分配资源" << endl;
		cout << "2.查看资源情况" << endl;
		cout << "3.退出" << endl;
		cout << "请输入要执行的操作 : ";
		int choice;
		cin >> choice;
		switch (choice)
		{
		case 1: b.apply(); break;
		case 2: b.display(); break;
		case 3: return 0;
		}
	}
	return 0;
}

运行结果图





欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/3970205.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-21
下一篇 2022-10-21

发表评论

登录后才能评论

评论列表(0条)

保存