【操作系统实验 / C++】模拟银行家算法(附代码)

1、题目要求

请参照课本上的银行家算法的实例,设计、实现银行家算法。
输入:某个进程请求的资源数量;
输出:已分配或者拒绝本次请求。
要求循环显示提示信息“请输入请求资源的数量和进程号:”。

2、数据结构设计

根据题目要求,以及银行家算法的特性,使用了多个全局数组用于存储进程和资源的情况。共5个进程,3个资源,如下图所示。

int Available[3];           //可用资源数组   
int Need[5][3] = {0};       //需要资源数组   
int Max[5][3];              //最大需求量数组   
int Allocation[5][3];       //当前分配资源数组   
int Request[5][3] = {0};    //请求的资源数  
int IsFinish[5] = {0};      //判断进程是否完成的数组   
int Array[5] = {0};         //安全队列数组   
int FinishNum = 0;          //完成算法的进程个数   
int n=0;                    //表示当前正在处理的进程序号   

同时,除了资源相关的数组外,还设置了几个信号量数组或值,用于存储每一个进程的完成情况、安全队列、完成的进程个数等。

3、程序主要思路和原理

主要思路:

  • 对于该题目,我完成了两种算法,一种是题目要求的循环请求资源,输入进程的序号以及请求资源数,达到所需资源数后完成进程,以及释放资源。另一种是产生一组随机数,并尝试能否通过这组随机数产生一个安全队列,若能够产生则输出该队列。
  • 提到的两种方法中,在请求资源的部分两者共用相同的函数,即判断输入的进程所需的资源数能否被当前可用的资源数满足,若能满足则分配该资源,完成进程后释放资源。同时,对银行家算法特性设置了一个安全检测函数,检测当前的资源分配是否会导致冲突,即其他的内存均没有足够的资源,若资源分配导致不安全则阻止此次分配。
  • 不同的地方在于,产生安全队列的算法中,我采用了一个随机数算法用来生成一个0-4的随机数,随后尝试当前可用资源能否满足该序号的进程运行,若能满足则先将资源分配给该进程,进程结束后再释放资源,同时修改进程相关的信号量,避免再次分配资源给该进程。若在规定次数(50次)内5个进程均完成,则输出记录的安全队列,否则停止循环并说明未找到安全序列。

原理:全局数组、变量,安全性检测,随机数算法

4、程序运行结果

首先运行程序,进行初始化。依次输入最大需求数目Max,已分配数目Allocation,以及当前资源的可用数目Available。随后打印出当前进程和资源的列表,可见需要的资源数目Need已经算出。

在这里插入图片描述

  • ①随机数算法产生安全队列: 随后出现操作列表,选择操作2,则开始通过一组随机数产生安全队列,并且依次输出结果,如下图所示。
    在这里插入图片描述
    在这里插入图片描述
    可见成功的产生了一组安全序列。

  • ②循环请求资源: 再次运行程序,初始化后选择操作1,首先对进程0请求一个A资源,输出结果如下图所示。
    在这里插入图片描述
    此时若再对进程1请求一个C资源,会造成系统死锁,资源不足以分配,此时安全性算法检测到了该错误,拒绝了此次资源分配。

在这里插入图片描述
此时再对进程0请求一个B资源,安全性算法没有产生问题。完成分配。
在这里插入图片描述
最后再对进程0请求一个C资源,进程0达到了最大需求,进程进行并完成,随后释放资源。可以看到进程0的已分配矩阵和需要资源都进行了清零,同时原先分配的资源全部进入剩余可用资源。

在这里插入图片描述

由上述结果可知,程序运行正常,且能够完成预先设定的两种运算,即循环请求资源和随机数算法产生安全队列,该实验完成。

5、详细代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int Available[3];			//可用资源数组 
int Need[5][3] = {0};		//需要资源数组 
int Max[5][3];				//最大需求量数组 
int Allocation[5][3];		//当前分配资源数组 
int Request[5][3] = {0};	//请求的资源数
int IsFinish[5] = {0};		//判断进程是否完成的数组 
int Array[5] = {0};			//安全队列数组 
int FinishNum = 0;			//完成算法的进程个数 
int n=0;					//表示当前正在处理的进程序号 

void Show()			//展示函数 
{
	cout<<endl;
	cout<<"----------------------------------------------------------------------------"<<endl;
	cout<<'\t'<<'\t'<<"最大需求矩阵"<<'\t'<<"已分配矩阵"<<'\t'<<"需要资源"<<'\t'<<"剩余可用资源"<<endl;
	cout<<'\t'<<'\t'<<"     Max    "<<'\t'<<"Allocation"<<'\t'<<"  Need  "<<'\t'<<"  Available "<<endl;
	cout<<"----------------------------------------------------------------------------"<<endl;
	cout<<"进程名称"<<"\tA   B   C "<<"\tA   B   C"<<"\tA   B   C"<<"\tA   B   C"<<endl;
	
	int i,j;
	for(i=0; i<5; i++)
	{
		cout<<"   P"<<i<<'\t'<<'\t';
		
		for(j=0; j<3; j++)
			cout<<Max[i][j]<<"   ";
		cout<<'\t';
		
		for(j=0; j<3; j++)
			cout<<Allocation[i][j]<<"   ";
		cout<<'\t';
		
		for(j=0; j<3; j++)
			cout<<Need[i][j]<<"   ";
		cout<<'\t';
		
		if(i == 0)
		{
			for(j=0; j<3; j++)
				cout<<Available[j]<<"   ";
		}
		cout<<endl;
	}
	cout<<"----------------------------------------------------------------------------"<<endl;
}


bool Check()		//对银行家算法的安全性检测 
{
	int i,j;
	int work[3] = {0};
	bool finish[3];
	
	for(i=0; i<3; i++)			//初始化 
		work[i] = Available[i];
	for(j=0; j<3; j++)
		finish[j] = false;
	
	int check = 0;
	bool flag;
	
	for(i=0; i<3; i++)
	{
		if(Need[n][i] <= work[i])
			check ++;
	}
	
	if(check == 3)
		flag = true;
	else
		flag = false;
	
	check = 0;
	
	for(i=0; i<3; i++)		//循环检测 
	{
		for(i=0; i<3; i++)
		{
			if(finish[i] == false && flag == true)
			{
				for(j=0; j<3; j++)
					work[j] = work[j] + Allocation[n][j];
				finish[i] = true;
				check ++;
			}
		}
	}
	
	if(check == 3)
		return true;
	else
		return false;
}


int Finish(int i)		//检测某一进程是否完成 
{
	int j;
	int check = 0;
	
	for(j=0; j<3; j++)
	{
		if(Need[i][j] == 0)
			check ++;
	}
	
	if(check == 3)
		IsFinish[i] = 1;
	return IsFinish[i];
}


void Process()			//处理请求的函数 
{
	int i,j,check1 = 0,check2 = 0;
	
	for(i=0; i<3; i++)
	{
		if(Request[n][i] <= Need[n][i])
		{
			if(Request[n][i] <= Available[i])
				check1 ++;
			else
			{
				cout<<"没有足够的资源分配给进程"<<n<<"请求的 ";
				for(j=0; j<3; j++)
				{
					cout<<Request[n][j]<<" ";
				}
				cout<<",请等待!"<<endl<<endl;
			}
		}
		
		else
		{
			cout<<"进程"<<n<<"请求的资源 ";
			for(j=0; j<3; j++)
				cout<<Request[n][j]<<" ";
			cout<<"超过了进程的需求数!"<<endl;
		}
	}
	
	if(check1 == 3)		//若请求的资源数目均小于可用 
	{
		cout<<endl<<"开始处理进程"<<n<<"的请求 ";
		for(i=0; i<3; i++)
			cout<<Request[n][i]<<" ";
		cout<<endl;
		
		for(j=0; j<3; j++)
		{
			Available[j] = Available[j] - Request[n][j];
			Allocation[n][j] = Allocation[n][j] + Request[n][j];
			Need[n][j] = Need[n][j] - Request[n][j];
		}
		
		if(Check() == true)
		{
			if(Finish(n) == 1)		//如果进程结束则释放资源 
			{
				cout<<endl<<"进程"<<n<<"已经结束,释放资源。"<<endl;
				
				for(i=0; i<3; i++)
				{
					Available[i] = Available[i] + Allocation[n][i];
					Allocation[n][i] = 0;
					Need[n][i] = 0;
				}
				
				Array[FinishNum] = n;
				FinishNum ++;
			}
			Show();
		}
		
		else
		{
			cout<<endl<<"进程"<<n<<"请求的资源 ";
			for(j=0; j<3; j++)
				cout<<Request[n][j]<<" ";
			cout<<"导致系统不安全!资源申请失败!返回。"<<endl;
			
			for(j=0; j<3; j++)		//回收资源 
			{
				Available[j] = Available[j] + Request[n][j];
				Allocation[n][j] = Allocation[n][j] - Request[n][j];
				Need[n][j] = Need[n][j] + Request[n][j];
			}
		}
	}
}


void Requester()		//请求队列,产生一个 0-4 的随机数,并尝试是否能生成安全队列 
{
	int i,j,check = 0;
	time_t t;
	srand((unsigned)time(&t)+rand());
	n = rand()%5;
	
	for(i=0; i<3; i++)
	{
		if(Need[n][i] <= Available[i])
			check ++;
	}
	
	for(j=0; j<3; j++)
	{
		if(check == 3)
		{
			Request[n][j] = Need[n][j];
		}
		else
		{
			srand((unsigned)time(&t)+rand());
			Request[n][j] = rand()%5;
		}
	}
	
	if(IsFinish[n] == 0)
		Process();
	else
		return; 
}


int main()
{
	int i,j;
	cout<<"银行家算法,共有5个进程,3种资源,首先分配资源。"<<endl;
	cout<<"请输入每个进程对于每个资源的最大需求数目(3*5):"<<endl;
	for(i=0; i<5; i++)
	{
		for(j=0; j<3; j++)
			cin>>Max[i][j];
	}
	
	cout<<"请输入每个进程的每个资源的已分配数目(3*5):"<<endl;
	for(i=0; i<5; i++)
	{
		for(j=0; j<3; j++)
		{
			cin>>Allocation[i][j];
			Need[i][j] = Max[i][j] - Allocation[i][j];
			
			if(Need[i][j] < 0)
				Need[i][j] = 0;
		}
	}
	
	cout<<"请输入每个资源的现有可用数目(3):";
	for(i=0; i<3; i++)
		cin>>Available[i];
	
	cout<<endl<<"初始化..."<<endl;
	Show();
	
	int check = 0;
	char choice;
	
	cout<<endl<<"-------------操作列表-------------"; 
	cout<<endl<<"请选择银行家算法的操作模式:";
	cout<<endl<<"1.手动请求资源,循环输入进程序号;";
	cout<<endl<<"2.根据所给的资源自主算出安全队列。";
	cout<<endl<<"----------------------------------";
	cout<<endl<<"**请输入选择的操作:";
	cin>>choice;
	
	switch(choice)
	{
		case '1':
			do
			{
				cout<<endl<<"请输入进程号(0-4):";
				cin>>n;
				
				cout<<endl<<"请输入请求的资源(3):";
				for(i=0; i<3; i++)
					cin>>Request[n][i];
					
				Process(); 
			}while(FinishNum < 5);
			break;
		
		case '2':
			do
			{
				Requester();
				check ++;
			}while(check != 50);
			
			if(FinishNum == 5)
			{
				cout<<endl<<"发现一组安全序列:";
				for(i=0; i<4; i++)
					cout<<Array[i]<<"-->";
				cout<<Array[4]<<endl<<endl;
			}
			else
				cout<<endl<<"在规定次数内未发现安全序列。"<<endl<<endl;
				break;
				
		default:
			cout<<endl<<"输入错误!程序将关闭,请重新启动。"<<endl<<endl;
			break;
	}
	
	system("pause");
	return 0;
}
  • 35
    点赞
  • 294
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
好的,下面是一个简单的C++代码实现银行家算法模拟: ```c++ #include <iostream> #include <vector> #include <algorithm> using namespace std; // 定义进程结构体 struct Process { int id; // 进程id vector<int> allocation; // 已分配资源数量 vector<int> max; // 最大资源需求量 vector<int> need; // 还需要的资源数量 bool finished; // 进程是否已完成 }; // 定义系统资源结构体 struct System { vector<int> available; // 可用资源数量 }; // 初始化进程信息 void initProcesses(vector<Process>& processes) { for (int i = 0; i < processes.size(); i++) { cout << "请输入进程" << processes[i].id << "的已分配资源数量:" << endl; for (int j = 0; j < processes[i].allocation.size(); j++) { cin >> processes[i].allocation[j]; } cout << "请输入进程" << processes[i].id << "的最大资源需求量:" << endl; for (int j = 0; j < processes[i].max.size(); j++) { cin >> processes[i].max[j]; processes[i].need[j] = processes[i].max[j] - processes[i].allocation[j]; // 计算还需要的资源数量 } } } // 初始化系统资源信息 void initSystem(System& system) { cout << "请输入系统可用资源数量:" << endl; for (int i = 0; i < system.available.size(); i++) { cin >> system.available[i]; } } // 安全性检查 bool isSafe(vector<Process>& processes, System& system) { vector<int> work = system.available; // 复制可用资源数量 vector<bool> finish(processes.size(), false); // 初始化进程完成状态 int count = 0; // 已完成进程数量 while (count < processes.size()) { bool found = false; // 是否找到满足条件的进程 for (int i = 0; i < processes.size(); i++) { if (!finish[i]) { // 进程未完成 bool enoughResources = true; // 是否有足够的资源分配给该进程 for (int j = 0; j < processes[i].need.size(); j++) { if (processes[i].need[j] > work[j]) { enoughResources = false; break; } } if (enoughResources) { // 有足够的资源分配给该进程 finish[i] = true; // 标记进程为已完成 count++; // 已完成进程数量+1 found = true; // 找到满足条件的进程 for (int j = 0; j < processes[i].allocation.size(); j++) { work[j] += processes[i].allocation[j]; // 更新可用资源数量 } } } } if (!found) { // 没有找到满足条件的进程,系统不安全 return false; } } return true; // 所有进程都已完成,系统安全 } // 银行家算法模拟 void bankerAlgorithm(vector<Process>& processes, System& system) { while (true) { bool found = false; // 是否找到满足条件的进程 for (int i = 0; i < processes.size(); i++) { if (!processes[i].finished) { // 进程未完成 bool enoughResources = true; // 是否有足够的资源分配给该进程 for (int j = 0; j < processes[i].need.size(); j++) { if (processes[i].need[j] > system.available[j]) { enoughResources = false; break; } } if (enoughResources) { // 有足够的资源分配给该进程 processes[i].finished = true; // 标记进程为已完成 found = true; // 找到满足条件的进程 for (int j = 0; j < processes[i].allocation.size(); j++) { system.available[j] += processes[i].allocation[j]; // 更新可用资源数量 } cout << "分配资源给进程" << processes[i].id << ",当前可用资源数量为:"; for (int j = 0; j < system.available.size(); j++) { cout << system.available[j] << " "; } cout << endl; } } } if (!found) { // 没有找到满足条件的进程,退出循环 break; } } bool allFinished = true; // 是否所有进程都已完成 for (int i = 0; i < processes.size(); i++) { if (!processes[i].finished) { allFinished = false; break; } } if (allFinished) { cout << "所有进程都已完成,安全!" << endl; } else { cout << "有进程未能完成,不安全!" << endl; } } int main() { int n; // 进程数量 int m; // 资源数量 cout << "请输入进程数量和资源数量:" << endl; cin >> n >> m; vector<Process> processes(n); // 进程数组 vector<int> available(m); // 可用资源数组 System system = { available }; // 系统资源 // 初始化进程信息 for (int i = 0; i < n; i++) { processes[i].id = i; processes[i].allocation.resize(m); processes[i].max.resize(m); processes[i].need.resize(m); } initProcesses(processes); // 初始化系统资源信息 system.available.resize(m); initSystem(system); // 进行安全性检查 if (isSafe(processes, system)) { cout << "系统安全!" << endl; // 进行银行家算法模拟 bankerAlgorithm(processes, system); } else { cout << "系统不安全!" << endl; } return 0; } ``` 在上面的代码中,我们通过定义进程结构体和系统资源结构体来存储进程和系统资源的信息。在main函数中,我们先初始化进程信息和系统资源信息,然后进行安全性检查,如果系统安全,就执行银行家算法模拟。 在模拟过程中,我们先检查每个进程是否已完成,如果未完成并且有足够的资源分配给该进程,就分配资源给该进程,并更新可用资源数量。重复执行该过程,直到所有进程都已完成为止。 希望这个代码能够帮到你。如果你有具体的问题或需求,欢迎随时向我提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值