操作系统(银行家算法)

银行家算法

  • 安全性检测
  • 资源申请
/***********************************************************
* 版权所有 (C)2020, Liuzhihan
*
* 文件名称: os.cpp 
* 文件标识:无
* 内容摘要:银行家算法安全性检查,进程申请资源 
* 其它说明:无
* 当前版本: V3.0
* 作   者:刘志涵 
* 完成日期: 20200701
*
**********************************************************/

#include <bits/stdc++.h>

using namespace std;
const int N = 100;

const int total_resources = 3; //资源总数

struct process
{
	/* data */
	int resources_max[total_resources];		   //每种资源的总大需求量
	int resources_allocation[total_resources]; //已分配的资源
	int resources_need[total_resources];	   //还需要的各类资源数
	/* need = max - allocation */
	bool finished;
};

int resources_available[total_resources]; //可分配的资源

int cnt_process;	   //总进程个数
int remaining_process; //未完成的进程个数
vector<process> p;	   //各进程数据

int State;
int request[total_resources]; //请求向量

int work[total_resources];
bool finished[N];
int sequence[N];
bool result;

/*********************************************************
* 功能描述:检查当前进程是否可以获取需要的资源need 
* 输入参数:cur - 进程号 
* 输出参数:true - 可以
			false - 不可以 
* 返回值:无 
* 其它说明:无 
************************************************************/
bool check(int cur)
{
	for (int i = 0; i < total_resources; i++)
	{
		if (p[cur].resources_need[i] > work[i])
			return false;
	}
	return true;
}

/*********************************************************
* 功能描述:dfs时选取下一个进程后 可分配资源减变化 
* 输入参数:i - 进程号 
* 输出参数:无 
* 返回值:无 
* 其它说明:无 
************************************************************/
void nextWork(int i)
{
	for (int j = 0; j < total_resources; ++j)
	{
		work[j] += p[i].resources_allocation[j];
	}
	finished[i] = true;
}

/*********************************************************
* 功能描述:dfs时返回后 可分配资源的前一状态 
* 输入参数:i - 进程号 
* 输出参数:无 
* 返回值:无 
* 其它说明:无 
************************************************************/
void prevWork(int i)
{
	for (int j = 0; j < total_resources; ++j)
	{
		work[j] -= p[i].resources_allocation[j];
	}
	finished[i] = false;
}

/*********************************************************
* 功能描述:显示可分配资源 Available
* 输入参数:无
* 输出参数:无 
* 返回值:无 
* 其它说明:无 
************************************************************/
void show_available()
{
	cout << endl;
	cout << "Available" << endl;
	for (int i = 0; i < total_resources; ++i)
	{
		if (i)
			cout << "   ";
		cout << "R" << i + 1;
	}
	cout << endl;
	for (int i = 0; i < total_resources; ++i)
	{
		if (i)
			cout << "    ";
		cout << resources_available[i];
	}
	cout << endl
		 << endl;
}

/*********************************************************
* 功能描述:显示安全序列 
* 输入参数:无
* 输出参数:无 
* 返回值:无 
* 其它说明:无 
************************************************************/
void show_sequence()
{
	vector<int> v(total_resources);
	for (int i = 0; i < total_resources; ++i)
	{
		v[i] = resources_available[i];
	}

	cout << "       MAX  "
		 << "  Allocation"
		 << "  Need  "
		 << "  Available" << endl;
	for (int i = 0; i < cnt_process; ++i)
	{
		cout << "P" << sequence[i] + 1 << "   ";
		for (int j = 0; j < total_resources; ++j)
		{
			cout << " " << p[sequence[i]].resources_max[j];
		}
		cout << "    ";
		for (int j = 0; j < total_resources; ++j)
		{
			cout << " " << p[sequence[i]].resources_allocation[j];
		}
		cout << "    ";
		for (int j = 0; j < total_resources; ++j)
		{
			cout << " " << p[sequence[i]].resources_need[j];
		}
		cout << "    ";
		for (int j = 0; j < total_resources; ++j)
		{
			v[j] += p[sequence[i]].resources_allocation[j];
			cout << " " << v[j];
		}
		cout << endl;
	}
	cout << endl;
}

/*********************************************************
* 功能描述:dfs求安全序列 
* 输入参数:cnt - 可以分配资源的进程个数 
* 输出参数:无 
* 返回值:无 
* 其它说明:无 
************************************************************/
void dfs(int cnt)
{
	if (result) return; //注释这一行就可输出所有的安全序列
	if (cnt == cnt_process)
	{
		for (int i = 0; i < cnt; ++i)
		{
			if (i)
				cout << " -> ";
			cout << "P" << sequence[i] + 1;
		}
		cout << endl;
		show_available();
		show_sequence();
		result = true;
		return;
	}
	for (int i = 0; i < cnt_process; ++i)
	{
		if (!finished[i] && check(i))
		{
			sequence[cnt] = i;
			nextWork(i);
			dfs(cnt + 1);
			prevWork(i);
		}
	}
}

//bool check_finished(process &tmp)
//{
//	bool flag = true;
//	for (int j = 0; j < total_resources; j++)
//	{
//		if (tmp.resources_need[j])
//		{
//			return false;
//		}
//	}
//	return true;
//}

/*********************************************************
* 功能描述:安全性检查 
* 输入参数:无
* 输出参数:无 
* 返回值:无 
* 其它说明:无 
************************************************************/
void getResult()
{
	for (int i = 0; i < total_resources; ++i)
	{
		work[i] = resources_available[i];
	}
	for (int i = 0; i < cnt_process; ++i)
	{
		//		finished[i] = p[i].finished;
		finished[i] = false;
		sequence[i] = -1;
	}
	result = false;
	dfs(0);
}

/*********************************************************
* 功能描述:主函数 
* 输入参数:无
* 输出参数:无 
* 返回值:无 
* 其它说明:无 
************************************************************/
int main()
{
	cout << "资源种类默认设置为3" << endl;
	cout << "请输入进程个数:";
	cin >> cnt_process;
	cout << "请依次输入各进程的最大需求 MAX 和已分配资源 Allocation" << endl;
	for (int i = 0; i < cnt_process; i++)
	{
		process temp{};
		cout << "MAX of process" << i << ":" << endl;
		for (int &j : temp.resources_max)
		{
			cin >> j;
		}
		cout << "Allocation of process" << i << ":" << endl;
		for (int j = 0; j < total_resources; j++)
		{
			cin >> temp.resources_allocation[j];
			temp.resources_need[j] = temp.resources_max[j] - temp.resources_allocation[j];
		}
		//		temp.finished = check_finished(temp);
		temp.finished = false;
		p.push_back(temp);
	}
	cout << "Available:" << endl;
	for (int &i : resources_available)
	{
		cin >> i;
	}

	cout << endl
		 << "-----------------------------------------------" << endl
		 << endl;

	getResult();
	if (!result)
	{
		cout << "Deadlock!" << endl;
		return 0;
	}
	cout << "T" << State << " is safe." << endl
		 << endl;

	while (true)
	{
		int mark;
		cout << endl
			 << "1.继续申请资源" << endl
			 << "0.退出" << endl;
		cin >> mark;
		if (mark == 0)
			break;

		int process_num;
		cout << "选择进程 process(1-" << cnt_process << "): ";
		cin >> process_num;
		process_num--;

		cout << "输入请求向量 Request[1..." << total_resources << "]: ";
		for (int i = 0; i < total_resources; i++)
		{
			cin >> request[i];
		}

		bool flag = true;
		for (int i = 0; i < total_resources; i++)
		{
			if (request[i] > p[process_num].resources_need[i] || request[i] > resources_available[i])
			{
				flag = false;
				break;
			}
		}
		if (!flag)
		{
			cout << "请求向量不合法,请重新选择" << endl;
			continue;
		}

		for (int i = 0; i < total_resources; i++)
		{
			p[process_num].resources_allocation[i] += request[i];
			p[process_num].resources_need[i] -= request[i];
			resources_available[i] -= request[i];
		}

		getResult();
		if (!result)
		{
			cout << "T" << State + 1 << " is Deadlock!" << endl;
			return 0;
		}
		State++;
		cout << "T" << State << " is safe." << endl
			 << endl;
	}

	return 0;
}

/*
4
3 2 2
1 0 0
6 1 3
5 1 1
3 1 4
2 1 1
4 2 2
0 0 2
1 1 2

2
1 0 1
3
0 0 1
*/

猜你喜欢

转载自blog.csdn.net/weixin_44169557/article/details/107049280