使用C++实现银行家算法避免进程死锁

**最近事比较多 直接按书上来 **
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
.h

#pragma once
#include<iostream>
using namespace std;
void Dis_char(int i, char ch);
void Init();//初始化矩阵
void Display();//显示
void Display_2();
void Display_Security();//显示安全序列
void Require();//请求资源进程
int Banker();//银行家算法实现

.cpp

#include"Banker.h"
#define MAX_SOURCE 6 //可打印的最大资源数
#define MAX_ROW 6//列最多为6
#define MAX_LINE 4//行最多为4
#define STATE 4 //表格中的四列
int Max[MAX_ROW][MAX_LINE] = { 0 };//最大需求矩阵
int Allocation[MAX_ROW][MAX_LINE] = { 0 };//分配矩阵
int Need[MAX_ROW][MAX_LINE] = { 0 };//需求矩阵
int Available[MAX_LINE] = { 0 };//可利用资源向量
int Flag[MAX_ROW] = {1,1,1,1,1,1};//标志
int Security[MAX_ROW] = {0,1,2,3,4,5};//安全序列数组
int EXE[MAX_LINE] = { 0 };//中间转换可利用资源
int Require_num[MAX_LINE] = { 0 };//亲求资源
int Work[MAX_ROW][MAX_LINE] = { 0 };//系统可提供给进程继续运行所需各类资源
int Work_Allocation[MAX_ROW][MAX_LINE] = {0};
int PcbNum, Source;//进程数   资源数
void Dis_char(int i, char ch)
{
	while (i--)
	{
		cout << ch;
	}
}
void Init()//初始化
{
	int i, j;
	cout << "输入进程的个数:";
	cin >> PcbNum;
	cout << "输入资源的种类:";
	cin >> Source;
	cout << "输入系统剩余资源(Available):";
	for (i = 0; i < Source; i++)
	{
		cin >> Available[i];
	}
	cout << endl;

	cout << "输入Allocation矩阵" << endl;
	for (i = 0; i < PcbNum; i++)
	{
		for (j = 0; j < Source; j++)
		{
			cin >> Allocation[i][j];
		}
	}
	cout << endl;

	cout << "输入Need矩阵" << endl;
	for (i = 0; i < PcbNum; i++)
	{
		for (j = 0; j < Source; j++)
		{
			cin >> Need[i][j];
		}
	}
	for (i = 0; i < PcbNum; i++)
	{
		for (j = 0; j < Source; j++)
		{
			Max[i][j] = Allocation[i][j] + Need[i][j];
		}
	}
	for (i = 0; i < Source; i++)
	{
		EXE[i] = Available[i];
		Work[0][i] = Available[i];
	}
	cout << "\n初始化完毕,初始化列表为:\n\n";
	Display();
	cout << "正在检测初始系统是否安全.......\n";
	int security = Banker();
	if (security == 1)
	{
		printf("该状态为安全状态");
	}
	else
	{
		printf("该状态不是安全状态,请检查数据是否正确");
	}
	cout << endl;
	cout << "安全序列为:" << endl;
	Display_Security();
	Display(); 
}
void Display()
{
	char Name[] = "ABCDEFGH";
	int n = PcbNum;//进程数
	int m = Source;//资源数
	int i, j, tmp;

	//1.打印表头
	printf("|—————————————————————————————————————————-|\n");
	printf("%-8s%-19s%-19s%-19s%-20s", "|进\\资源", "|       Max        ", "|    Allocation    ", "|       Need       ", "|     Available    |\n");
	printf("|   \\情 |—————————|—————————|—————————|—————————|\n");
	printf("|程  \\况");
	for (i = 0; i < STATE; i++)
	{
		cout << "|";
		for (j = 0; j < m; j++)
		{
			printf(" %c ", Name[j]);
		}
		tmp = MAX_SOURCE - m;//超出的用空格弥补
		Dis_char(tmp * 3, ' ');
	}
	cout << "|\n";
	printf("|-------|—————————|—————————|—————————|—————————|\n");
	//2.打印每一行
	for (i = 0; i < n; i++)
	{
		printf("|  P%-2d  ", Security[i]);
		for (j = 0; j < m; j++)//Max数据
		{
			cout << "|";
			for (j = 0; j < m; j++)
			{
				printf(" %d ", Max[Security[i]][j]);
			}
			tmp = MAX_SOURCE - m;//超出的用空格弥补
			Dis_char(tmp * 3, ' ');
		}
		for (j = 0; j < m; j++)//Allcoation数据
		{
			cout << "|";
			for (j = 0; j < m; j++)
			{
				printf(" %d ", Allocation[Security[i]][j]);
			}
			tmp = MAX_SOURCE - m;//超出的用空格弥补
			Dis_char(tmp * 3, ' ');
		}
		for (j = 0; j < m; j++)//Need数据
		{
			cout << "|";
			for (j = 0; j < m; j++)
			{
				printf(" %d ", Need[Security[i]][j]);
			}
			tmp = MAX_SOURCE - m;//超出的用空格弥补
			Dis_char(tmp * 3, ' ');
		}
		cout << "|";
		for (j = 0; j < m; j++)//Available数据
		{
			printf(" %d ", Available[j]);
		}
		tmp = MAX_SOURCE - m;//超出的用空格弥补
		Dis_char(tmp * 3, ' ');
		cout << "|\n";
		printf("|-------|—————————|—————————|—————————|—————————|\n");
	}
}
void Display_2()
{
	char Name[] = "ABCDEFGH";
	int n = PcbNum;//进程数
	int m = Source;//资源数
	int i, j, tmp;
	printf("|—————————————————————————————————————————-|\n");
	printf("%-8s%-19s%-19s%-19s%-20s", "|进\\资源", "|       Work      ", "|      Need     ", "|   Allocation    ", "|  Work+Allocation |\n");
	printf("|   \\情 |—————————|—————————|—————————|—————————|\n");
	printf("|程  \\况");
	for (i = 0; i < STATE; i++)
	{
		cout << "|";
		for (j = 0; j < m; j++)
		{
			printf(" %c ", Name[j]);
		}
		tmp = MAX_SOURCE - m;//超出的用空格弥补
		Dis_char(tmp * 3, ' ');
	}
	cout << "|\n";
	printf("|-------|—————————|—————————|—————————|—————————|\n");
	for (i = 0; i < n; i++)
	{
		printf("|  P%-2d  ", Security[i]);
		for (j = 0; j < m; j++)//Max数据
		{
			cout << "|";
			for (j = 0; j < m; j++)
			{
				printf(" %d ", Work[Security[i]][j]);
			}
			tmp = MAX_SOURCE - m;//超出的用空格弥补
			Dis_char(tmp * 3, ' ');
		}
		for (j = 0; j < m; j++)
		{
			cout << "|";
			for (j = 0; j < m; j++)
			{
				printf(" %d ", Need[Security[i]][j]);
			}
			tmp = MAX_SOURCE - m;//超出的用空格弥补
			Dis_char(tmp * 3, ' ');
		}
		for (j = 0; j < m; j++)
		{
			cout << "|";
			for (j = 0; j < m; j++)
			{
				printf(" %d ", Allocation[Security[i]][j]);
			}
			tmp = MAX_SOURCE - m;//超出的用空格弥补
			Dis_char(tmp * 3, ' ');
		}
		for (j = 0; j < m; j++)
		{
			cout << "|";
			for (j = 0; j < m; j++)
			{
				printf(" %d ", Work_Allocation[Security[i]][j]);
			}
			tmp = MAX_SOURCE - m;//超出的用空格弥补
			Dis_char(tmp * 3, ' ');
		}
		cout << "|";
		
		if (Flag[i] == 0)
		{
			printf("   true  ");
		}
		else
		{
			printf("  false  ");
		}
		
		cout << "|\n";
		printf("|-------|—————————|—————————|—————————|—————————|\n");
	}

}
void Display_Security()
{
	int arr[MAX_LINE] = { 0 };
	for (int i = 0; i < Source; i++)
	{
		arr[i] = Available[i];
	}
	for (int i = 0; i < PcbNum; i++)
	{
		cout << "P"<<Security[i] << "--->";
	}
	cout << endl;
	for (int i = 0; i < PcbNum; i++)
	{
		for (int j = 0; j < Source; j++)
		{
			Work[Security[i]][j] = arr[j];
			Work_Allocation[Security[i]][j] = Work[Security[i]][j] + Allocation[Security[i]][j];
			arr[j] = Work_Allocation[Security[i]][j];
		}
	}
}
int Security_Judge(int pid)
{
	int cur = 0;//判断是否小于need
	int ptr = 0;//判断是否小于Available
	while (cur != Source)
	{
		if (Require_num[cur] <= Need[pid][cur])
		{
			cur++;
		}
		else
		{
			return 1;
			break;
		}
	}
	while (ptr != Source)
	{
		if (Require_num[ptr]<=Available[ptr])
		{
			ptr++;
		}
		else
		{
			return 2;
			break;
		}
	}
	return 0;
}
void Require()
{
	int Pid, tmp;
	cout << "输入要想系统请求资源的进程Id:";
	cin >> Pid;
	cout << "输入要想向系统请求资源的大小:";
	for (int i = 0; i < Source; i++)
	{
		cin >> Require_num[i];
	}
	tmp = Security_Judge(Pid);
	if (tmp == 1)
	{
		cout << "\n请求出错!此进程请求资源超过它宣布的最大需求!!!\n";
		cout << "当前时刻资源分配表\n"; 
		Display();

	}
	if (tmp == 2)
	{
		cout<<"\n请求出错!此请求所需资源超过系统资源,P" << Pid << "进程等待!!!\n"; 
		cout << "当前时刻资源分配表\n"; Display(); 
	}
	if (tmp == 0)
	{
		cout << "请求成功正在检测系统是否安全!!!" << endl;
		for (int i = 0; i < Source; i++)
		{
			Available[i] = Available[i] - Require_num[i];
			Need[Pid][i] = Need[Pid][i] - Require_num[i];
			Allocation[Pid][i] += Require_num[i];
			Max[Pid][i] = Allocation[Pid][i] + Need[Pid][i];
		}
		for (int i = 0; i < MAX_ROW; i++)
		{
			Flag[i] = 1;
		}
		int security = Banker();
		if (security == 1)
		{
			cout << "该状态为安全状态!安全序列为:" << endl;
			Display_Security();
			Display();

		}
		if (security == -1)
		{
			cout << "该状态不是安全状态,请检查数据是否正确" << endl;
			Display();
		}
	}
}
int Banker()
{
	int key = 0;
	int tmp = PcbNum;
	while (tmp != 0)
	{
		int ptr = 0;
		while (ptr !=PcbNum)
		{
			if (Flag[ptr] == 0)
			{
				ptr++;
			}
			else
			{
				int i = 0;
				while (i != Source)
				{
					if (Need[ptr][i] <= EXE[i])
					{
						i++;
					}
					else
					{
						break;
					}
				}
				if (i == Source)
				{
					Flag[ptr] = 0;
					Security[key] = ptr;
					key++;
					for (int i = 0; i < Source; i++)
					{
						EXE[i] += Allocation[ptr][i];
					}
					tmp--;
					break;
				}
				else
				{
					ptr++;

				}
				
			}
		}
		if (ptr == PcbNum && Flag[ptr - 1] == 1)
		{
			return -1;
			break;
		}

	}
	return 1;
}

.cpp

#include"Banker.h"
int main()
{
	int choose;
	while (1)
	{
		cout << "           银行家算法:" << endl << endl;
		cout << "*****  1 - 初始化各矩阵        *****" << endl;
		cout << "*****  2 - 进程提出请求        *****" << endl;
		cout << "*****  3 - 显示各进程资源情况  *****" << endl;
		cout << "*****  0 - 结束                *****" << endl << endl;
		cout << "输入你的选择 : ";
		cin >> choose;
		switch (choose)
		{
		case 0:exit(0); break;
		case 1:Init(); break;
		case 2:Require(); break;
		case 3:Display_2(); break;
		default:cout << "请输入正确的序号:" << endl;
		}
	}
	return 0;
}

结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值