栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

操作系统——银行家算法C++语言实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

操作系统——银行家算法C++语言实现

问题描述:

设计程序模拟预防进程死锁的银行家算法的工作过程。假设系统中有n个进程P1, … ,Pn,有m类可分配的资源R1, … ,Rm,在T0时刻,进程Pi分配到的j类资源为Allocationij个,它还需要j类资源Need ij个,系统目前剩余j类资源Workj个,现采用银行家算法进行进程资源分配预防死锁的发生。

程序要求:

1)判断当前状态是否安全,如果安全给出安全序列;如果不安全给出理由。

2)对于下一个时刻T1,某个进程Pk会提出请求Request(R1, … ,Rm),判断分配给P k进程请求的资源之后系统是否安全。

3)输入:进程个数n,资源种类m,T0时刻各个进程的资源分配情况(可以运行输入,也可以在程序中设置);

4)输出:如果安全,输出安全的进程序列,不安全则提示信息。

4、程序主要构成部分及其算法说明

主程序中定义了结构体RR,以及我们要通过队列的形式来进行轮转算法的实现。方法Input(),输入相关数据。Bankerinput()定义输入进程请求资源数。Security()对当下序列安全性进行检查。Banker()银行家算法的调度。Show()输出信息。

5、调试过程和运行结果

 

6、完整代码

#include 
#include 
#include 

using namespace std;
#define MaxNumber 20

static int n; //进程个数
static int m;  //资源个数
static int Available[MaxNumber];
static int Max[MaxNumber][MaxNumber];
static int Allocation[MaxNumber][MaxNumber];
static int Need[MaxNumber][MaxNumber];
static int Request[MaxNumber];
static int SafeOrder[MaxNumber];
char Name[] = { 'A','B','C','D','E','F','G' };
int RequestPi;//请求进程
bool security;

void input() {
	cout << "请输入进程数n:";
	cin >> n;
	cout << "请输入资源数m:";
	cin >> m;
	cout << endl;

	cout << "请输入各个进程的最大资源:" << endl;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> Max[i][j];
		}
	}
	cout << "请输入各个进程已经分的资源:" << endl;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> Allocation[i][j];
		}
	}
	//计算得还需资源
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			Need[i][j] = Max[i][j] - Allocation[i][j];
		}
	}
	cout << "请输入当下空闲资源:" << endl;
	for (int i = 0; i < m; i++) {
		cin >> Available[i];
	}
}
void Bankerinput() {
	cout << "请输入哪个进程发出请求:";
	cin >> RequestPi;
	cout << "请输入进程P" << RequestPi << "的请求向量:";
	for (int j = 0; j < m; j++) {
		cin >> Request[j];
	}
}

void Security() {
	security = false;
	int Work[50];
	bool Finish[50];
	int FinishCnt = 0;
	int orderNum = 0;
	//初始化Work数组
	for (int j = 0; j < m; j++) {
		Work[j] = Available[j];
	}
	//初始所有进程都没完成
	for (int i = 0; i < n; i++) {
		Finish[i] = false;
	}
	while (FinishCnt != n) {
		bool haveroad = false;//havaroad判断是否死锁的指标
		for (int i = 0; i < n; i++) {
			int temp = 0;  //判断需要的三个资源是不是都满足的指标
			for (int j = 0; j < m; j++) {
				if (Finish[i] == false && Need[i][j] <= Work[j]) {
					temp++;
				}
			}
			//如果三个资源都可以满足,进行资源释放
			if (temp == m) {
				FinishCnt += 1;
				Finish[i] = true;
				haveroad = true;
				SafeOrder[orderNum] = i;
				orderNum++;
				for (int j = 0; j < m; j++) {
					Work[j] = Work[j] + Allocation[i][j];
				}
			}
		}
		if (haveroad == false) break;
	}
	//用FinishCnt判断是不是所有都是true
	if (FinishCnt == n) {
		security = true;
	}

}
void Show() {
	cout << "当前的资源分配表如下:" << endl;
	cout << "资源情况 " << "  Max" << setw(15) << "Allocation" << setw(7) << "Need" << setw(12) << "  Available" << endl;
	cout << "	 " << setw(3);
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < m; j++) {
			cout << Name[j] << " ";
		}

		cout << setw(5);

	}
	cout << endl;
	for (int i = 0; i < n; i++) {
		cout << "P" << i << setw(5);
		for (int j = 0; j < m; j++) {
			cout << Max[i][j] << " ";
		}
		cout << setw(7);
		for (int j = 0; j < m; j++) {
			cout << Allocation[i][j] << " ";
		}
		cout << setw(5);
		for (int j = 0; j < m; j++) {
			cout << Need[i][j] << " ";
		}
		cout << setw(5);
		if (i == 0) {
			for (int j = 0; j < m; j++) {
				cout << Available[j] << " ";
			}
		}
		cout << setw(5) << endl;
	}
	cout << endl;
}
void Banker() {
	Security();
	if (security) {
		cout << "系统安全,可以进行资源请求!" << endl;
		cout << "安全序列为:{";

		for (int i = 0; i < n; i++) {
			cout << "P" << SafeOrder[i] << ",";
		}
		cout << "}" << endl;
		Bankerinput();
		int needTemp = 0, sourceTemp = 0;
		for (int j = 0; j < m; j++) {
			if (Request[j] <= Need[RequestPi][j]) {
				needTemp++;
			}
			else {
				cout << "进程" << RequestPi << "要求资源超出最大值!" << endl;
			}
		}
		if (needTemp == m) {
			for (int j = 0; j < m; j++) {
				if (Request[j] <= Available[j]) {
					sourceTemp++;
				}
			}
			if (sourceTemp == m) {       //空闲资源满足要求的资源,假设资源分配
				for (int j = 0; j < m; j++) {
					Available[j] -= Request[j];
					Allocation[RequestPi][j] += Request[j];
					Need[RequestPi][j] -= Request[j];

				}
				Show();
				Security();
				if (security) {
					cout << "系统安全,本次资源分配成功!" << endl;
					cout << "安全序列为:{";

					for (int i = 0; i < n; i++) {
						cout << "P" << SafeOrder[i] << ",";
					}
					cout << "}" << endl;
				}
				else {
					cout << "系统不安全,本次资源分配失败!" << endl;
					for (int j = 0; j < m; j++) {
						Available[j] += Request[j];
						Allocation[RequestPi][j] -= Request[j];
						Need[RequestPi][j] += Request[j];
					}

				}

			}
			else {
				cout << "无足够资源,需要等待。" << endl;
				bool finishprocess;
				cout << "还有要求的进程吗?true or false:";
				cin >> finishprocess;
				if (finishprocess) {
					Banker();
				}
			}
		}
	}
	else {
		cout << "系统不安全,不进行下列操作" << endl;
	}



}

int main() {
	input();
	Show();
	Banker();
	system("pause");
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1008502.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号