银行家算法 C语言实现

银行家算法 C语言实现C语言实现银行家算法这几天老师要求使用C语言实现银行家算法,数据可以自定义。想来想去还是照着书现成的数据来模拟一下。教材使用的是西安电子科大出版社的《计算机操作系统》汤小丹第四版。模拟数据使用的是P121页第4题的数据。听到老师布置题目的第一时间我还是有点懵,看了下书更懵了,这条条框框的判断条件怎么这么多。。沉下心来慢慢看,其实还是挺简单的算法。/*Author:Cnkizy…

大家好,又见面了,我是你们的朋友全栈君。

C语言实现银行家算法

这几天老师要求使用C语言实现银行家算法,数据可以自定义。想来想去还是照着书现成的数据来模拟一下。

教材使用的是西安电子科大出版社的《计算机操作系统》汤小丹 第四版。模拟数据使用的是P121页第4题的数据。

听到老师布置题目的第一时间我还是有点懵,看了下书更懵了,这条条框框的判断条件怎么这么多。。沉下心来慢慢看,其实还是挺简单的算法。

/*Author:Cnkizy
数据参考 P121 4.银行家算法之例
*/
#include<stdio.h>
#define Pcount 5 //5个进程
#define Scount 3 //3类资源
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();

	//进程P1 申请资源{1,0,2}
	int apply[Scount] = { 1,0,2 };
	RequestShowMsg(1, apply);

	//进程P4 申请资源{1,0,2}
	int apply2[Scount] = { 3,3,0 };
	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;

	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[Pcount]) {
	for (int i = 0; i < Pcount; 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;
}

银行家算法 C语言实现

偷懒for循环所以使用了C++编译器。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/132891.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(1)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号