银行家算法

一、算法介绍

银行家算法(Banker’s Algorithm)是一个避免死锁(Deadlock)的著名算法,它以银行借贷系统的分配策略为基础,判断并保证系统的安全运行。为了了解系统的资源分配情况,假定系统的任何一种资源在任意时刻只能被一个进程使用,任何进程已经占用的资源只能由进程自己释放,而不能由其他进程抢占,当进程申请的资源不能满足时,必须等待。因此只要资源分配算法能保证进程的资源请求,且不出现循环等待,则系统不会出现死锁。

二、算法原理

在该方法中把系统的状态分为安全状态和不安全状态,只要能使系统始终都处于安全状态,便可以避免发生死锁。银行家算法的基本思想是分配资源之前,判断系统是否是安全的;若是,才分配。它是最具有代表性的避免死锁的算法。

三、伪码描述

设request­­i为进程p[i]的请求向量,如果 requesti[j]=K,表示进程p[i]需要K个Rj资源。
当系统发出请求后,系统按下述步骤开始检查:
1)如果requesti[j]<=need[i][j],转向步骤2;否则报告出错,申请的资源已经大于它需要的最大值。
2)如果requesti[j]<=available[j],转向步骤3;否则报告出错,尚无足够的资源。
3)系统试探着把资源分配给p[i],并修改下列数据 结构中的值:
available[j]=available[j]­request[j]
allocation[i][j]=allocation[i][j]+request[j]
need[i][j]=need[i][j]­request[j]
4)系统进行安全性算法,检查此次分配后,系统是否还处于安全状态,若安全,把资源分配给进程p[i]; 否则,恢复原来的资源分配状态,让进程p[i]等待。安全性算法如下:
int work[RESOURCE_NUMBER];
bool finish[PROCESS_NUMBER];

  1. Work=Available;
    Finish=false;
  2. 寻找满足条件的i:
    A、Finish[i]=false;
    B、Need[i]≤Work;
    如果不存在,则转4)
  3. Work:=Work+Allocation[i];
    Finish[i]:=true;转2)
  4. 若对所有i,Finish[i]=true,则系统处于安全状态,否则处于不安全状态

四、 相关说明

int Available[Scount];//可利用资源向量
int Max[Pcount][Scount];//最大需求矩阵 可以通过Need+Allocation算出来
int Allocation[Pcount][Scount];//分配矩阵
int Need[Pcount][Scount];//需求矩阵
void InitializeData();//初始化数据
void ShowData(int line); //查看当前资源分配表
void CalcMaxMatrix();//计算最大需求数量
int Equals(int a[Scount], int b[Scount]); //资源比较 a<=b 返回1 a>b 返回0
int CheckSafe();//安全性算法,当前是否处于安全状态
int CheckFinish(int Finish[Scount]); //检查标志所有都为True,是返回1 不是返回0
void Add(int* a, int b[Scount]); //向量相加 a = a+b
void Minus(int* a, int b[Scount]); //向量相减 a = a-b
int Request(int P, int Request[Scount]); //进程资源请求 P:进程i,r申请资源数{1,1,1} 返回1成功 0失败
void RequestShowMsg(int P, int R[Scount]); //带命令提示符提示的请求

五、具体实现

#include<iostream>
#include<stdio.h>

#define Pcount 5 //5个进程
#define Scount 3 //3类资源

using namespace std;

int Available[Scount];//可利用资源向量
int Max[Pcount][Scount];//最大需求矩阵 可以通过Need+Allocation算出来
int Allocation[Pcount][Scount];//分配矩阵
int Need[Pcount][Scount];//需求矩阵
//int SouresMax[Scount] = { 10,5,7 };//这里给ABC三类资源的数量为10,5,7
/*资源分配表,必要的一些数据如下
	Max		Allocation	Need	Available
	P0		0 1 0		7 4 3	3 3 2
	P1		2 0 0		1 2 2
	P2		3 0 2		6 0 0
	P3		2 1 1		0 1 1
	P4		0 0 2		4 3 1
*/
//初始化数据
void InitializeData();
//查看当前资源分配表
void ShowData(int line);
//计算最大需求数量
void CalcMaxMatrix();
//资源比较   a<=b 返回1     a>b 返回0
int Equals(int a[Scount], int b[Scount]);
//安全性算法,当前是否处于安全状态
int CheckSafe();
//检查标志所有都为True,是返回1 不是返回0
int CheckFinish(int Finish[Scount]);
//向量相加 a = a+b
void Add(int* a, int b[Scount]);
//向量相减 a = a-b
void Minus(int* a, int b[Scount]);
//进程资源请求 P:进程i,r申请资源数{1,1,1} 返回1成功 0失败
int Request(int P, int Request[Scount]);
//带命令提示符提示的请求
void RequestShowMsg(int P, int R[Scount]);
int main() {
	//初始化银行家算法的数据,详见上表
	InitializeData();
	printf("=============初始数据如下=============\n");
	ShowData(0);
	//安全性检查
	CheckSafe();
	while(true){
	 	int a,b,c,pi;
	 	
 		cout << "请选择你想分配资源的进程:P";
		cin >> pi;
		cout << endl << "请输入你想给进程P" << pi << "分配的资源(A,B,C)空格隔开:";	
		cin >> a >> b >> c;
		cout << endl; 
		
		int apply[Scount] = {a,b,c};
		RequestShowMsg(pi, apply);
		cout<<"是否继续分配资源,是(1)、否(0):";
		int cont ; cin >> cont;
		if(!cont) break; 
	} 
	/*
	//进程P1 申请资源{1,0,2}
	int apply[Scount] = { 1,0,2 };
	RequestShowMsg(1, apply);

	//进程P4 申请资源{1,0,2}
	int apply2[Scount] = { 1,0,2 };
	RequestShowMsg(4, apply2);
 
	//进程P0 申请资源{0,2,0}
	int apply3[Scount] = { 0,2,0 };
	RequestShowMsg(0, apply3);
 	*/
	return 0;
}
//初始化数据,资源分配表
void InitializeData() {
 
	Allocation[0][0] = 0, Allocation[0][1] = 1, Allocation[0][2] = 0;
	Allocation[1][0] = 2, Allocation[1][1] = 0, Allocation[1][2] = 0;
	Allocation[2][0] = 3, Allocation[2][1] = 0, Allocation[2][2] = 2;
	Allocation[3][0] = 2, Allocation[3][1] = 1, Allocation[3][2] = 1;
	Allocation[4][0] = 0, Allocation[4][1] = 0, Allocation[4][2] = 2;
 
	Need[0][0] = 7, Need[0][1] = 4, Need[0][2] = 3;
	Need[1][0] = 1, Need[1][1] = 2, Need[1][2] = 2;
	Need[2][0] = 6, Need[2][1] = 0, Need[2][2] = 0;
	Need[3][0] = 0, Need[3][1] = 1, Need[3][2] = 1;
	Need[4][0] = 4, Need[4][1] = 3, Need[4][2] = 1;
 	cout<<"请输入初始初始可用资源(A,B,C)空格隔开:" ;
	cin>> Available[0] >> Available[1] >> Available[2];
	cout <<endl; 
	//Available[0] = 3, Available[1] = 3, Available[2] = 2;
 
	CalcMaxMatrix();
}
//进程资源请求 P:进程i,r申请资源数{1,1,1} 返回1成功 0失败
int Request(int P,int Request[Scount]) {
	printf("进程P%d申请资源%d %d %d:\n",P, Request[0], Request[1], Request[2]);
	//步骤1 进行资源检查Request <= Need才能执行步骤2
	if (!Equals(Request, Need[P])) {
		printf("进程P%d,Request:%d %d %d > Need:%d %d %d 申请失败,所需资源数超过宣布最大值!\n", P, Request[0], Request[1], Request[2], Need[P][0], Need[P][1], Need[P][2]);
		return 0;
	}
	//步骤2 进行资源检查Request <= Available才能执行步骤3
	if (!Equals(Request, Available)) {
		printf("进程P%d,Request:%d %d %d > Available:%d %d %d 申请失败,尚无足够资源,该进程需要等待!\n", P, Request[0], Request[1], Request[2], Available[0], Available[1], Available[2]);
		return 0;
	}
	printf("进程P%d,Request:%d %d %d <= Need:%d %d %d\n", P, Request[0], Request[1], Request[2], Need[P][0], Need[P][1], Need[P][2]);
	printf("进程P%d,Request:%d %d %d <= Available:%d %d %d \n", P, Request[0], Request[1], Request[2], Available[0], Available[1], Available[2]);
	//步骤3 试分配资源给进程P
	Minus(Available, Request);//Available -= Request
	Add(Allocation[P],Request);//Allocation += Request
	Minus(Need[P], Request);//Need -= Request
	//步骤4 安全性检查
	int Safestate = CheckSafe();
	if (Safestate) {
		return Safestate;//分配后处于安全状态 分配成功
	}
	//分配后处于不安全状态 分配失败,本次分配作废,回复原来的资源分配状态
	Add(Available, Request);//Available += Request
	Minus(Allocation[P], Request);//Allocation -= Request
	Add(Need[P], Request);//Need += Request
	return Safestate;
}
//带命令提示符提示的请求
void RequestShowMsg(int P, int R[Scount]) {
	//进程P 申请资源Request{1,0,2}
	printf("\n模拟分配资源:P%d申请资源 %d %d %d\n======================\n",P, R[0], R[1], R[2]);
	int State = Request(P, R);
	if (State) {
		printf("本次资源分配成功!\n");
		ShowData(0);
	}else {
		printf("本次资源分配失败!进程P%d需要等待\n",P);
	}
}
//安全性算法,当前是否处于安全状态
int CheckSafe() {
	printf("开始安全性检查:\n");
	//步骤1 设置两个向量
	int Finish[Pcount] = { 0 };//是否被处理过,初始值全为False,被检查过才置为True
	int Work[Scount] = { 0 };//工作向量	
	Add(Work, Available);//首先让Work = Available
	//步骤2 从进程集合寻找符合下列条件的进程
	//Finish[i] =  false;
	//Need[i,j] <= Work[j];
	for (int i = 0; i < Pcount; i++) {
		if (Finish[i])continue;//已经标记为True就跳过
		if (!Equals(Need[i], Work))continue;//Need[i,j] > Work[j] 就跳过。
		//上述条件成立,执行步骤3
		Add(Work, Allocation[i]);//Work += Allocation;
		Finish[i] = 1;//Finish[i]=True;	
		printf("P%d进程,Work=%d %d %d,Finish=true,安全状态\n", i, Work[0], Work[1], Work[2]);
		i = -1;//返回步骤2
	}
	//步骤4 判断Finish
	if (CheckFinish(Finish)) {
		printf("安全状态检查完毕:【Finish全为true,系统处于安全状态】\n");
		return 1;//全为True		
	}
	printf("安全状态检查完毕:【Finish存在False,系统处于不安全状态】\n");
	return 0;//存在False
}
//检查标志所有都为True,是返回1 不是返回0
int CheckFinish(int Finish[Scount]) {
	for (int i = 0; i < Scount; i++) {
		if (Finish[i] == 0) return 0;
	}
	return 1;
}
//查看当前资源分配表
void ShowData(int line) {
	printf("	Max	Alloca	Need	Available\n");
	for (int i = 0; i < Pcount; i++) {
		printf("p%d:\t", i);
		for (int j = 0; j < Scount; j++) {
			printf("%d ", Max[i][j]);
		}
		printf("\t");
		for (int j = 0; j < Scount; j++) {
			printf("%d ", Allocation[i][j]);
		}
		printf("\t");
		for (int j = 0; j < Scount; j++) {
			printf("%d ", Need[i][j]);
		}
 
		if (line == i) {
			printf("\t");
			for (int j = 0; j < Scount; j++) {
				printf("%d ", Available[j]);
			}
		}
 
		printf("\n");
	}
 
}
//计算最大需求数量
void CalcMaxMatrix() {
	for (int i = 0; i < Pcount; i++) {
		for (int j = 0; j < Scount; j++) {
			Max[i][j] = Need[i][j] + Allocation[i][j];
		}
	}
}
//向量相加 a = a+b
void Add(int* a, int b[Scount]) {
	for (int i = 0; i < Scount; i++) {
		a[i] = a[i] + b[i];
	}
}
//向量相减 a = a-b
void Minus(int* a, int b[Scount]) {
	for (int i = 0; i < Scount; i++) {
		a[i] = a[i] - b[i];
	}
}
//资源比较   a<=b 返回1     a>b 返回0
int Equals(int a[Scount], int b[Scount]) {
	for (int i = 0; i < Scount; i++) {
		if (a[i] > b[i]) return 0;
	}
	return 1;
}

六、实验示例

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

  • 13
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值