操作系统课程设计 银行家算法

应付一下课设 写的一般 

一、目的        
       通过编写一个模拟动态资源分配的银行家算法程序,进一步深入理解死锁、产生死锁的必要条件、安全状态等重要概念,并掌握避免死锁的具体实施方法。

二、内容
(1)模拟一个银行家算法: 设置数据结构 设计安全性算法
(2) 初始化时让系统拥有一定的资源
(3) 用键盘输入的方式申请资源
(4)如果预分配后,系统处于安全状态,则修改系统的资源分配情况
(5)如果预分配后,系统处于不安全状态,则提示不能满足请求
三、要点说明
数据结构

 可利用资源向量   int  Available[m]   m为资源种类
最大需求矩阵        int  Max[n][m]     n为进程的数量
分配矩阵               int  Allocation[n][m]
申请资源数量        int  Request [m]  
工作向量               int  Work[m]    int  Finish[n]
银行家算法bank()函数

Requesti:进程Pi的请求向量。   0<=j<=m-1

(1) 若 Requesti[j] ≤ Need[i,j],转向(2),否则出错。
(2) 若 Requesti[j] ≤ Available[j],转向(3),否则等待。
(3) 系统试探着把资源分配给进程Pi,修改下面内容:
      Available[j] = Available[j] – Requesti[j];
      Allocation[i,j] = Allocation[i,j] + Requesti[j];
      Need[i,j] = Need[i,j] –Requesti[j];
(4) 试分配后,执行安全性算法,检查此次分配后系统是否处于安全状态。若安全,才正式分配;否则,此次试探性分配作废,进程Pi等待。
安全性算法Issafe()函数

(1) 初始化:设置两个向量Work(1×m)和Finish(1×n)
      Work – 系统可提供给进程继续运行所需各类资源数,初态赋值Available
      Finish – 系统是否有足够资源分配给进程,初值false.
(2) 从进程集合中满足下面条件进程:
      Finish[i] = false;  Need[i,j] ≤ Work[j];
      若找到,执行(3),否则,执行(4)。
(3) 进程Pi获得资源,可顺利执行,完成释放所分配的资源。
      Work[j] = Work[j]+Allocation[i,j];  Finish[i] = true;  go to (2).
(4) 若所有进程Finish[i] = true,表示系统处于安全状态,否则处于不安全状态。
先对用户提出的请求进行合法性检查,即检查请求的是否不大于需要的,是否不大于可利用的。 若请求合法,则进行试分配。最后对试分配后的状态调用安全性检查算法进行安全性检查。 若安全,则分配,否则,不分配,恢复原来状态,拒绝申请。
 

#include<iostream>
#include<stdlib.h>
using namespace std;
class Bank
{
public:
	Bank(){};
	void dispaly()
	{
		PrintAvailable();
		PrintMax();
		PrintAllocation();
		PrintNeed();
	}
	void Init()
	{
		cout << "请输入系统总资源数目:";
		cin >> NumOfResoure;
		cout << endl;
		cout << "请输入参与进程的数目:";
		cin >> NumOfProcess;
		InitAvailable();
		InitMax();
		InitAllocation();
		InitNeed();
	}
	void InitAvailable();
	void PrintAvailable();
	void InitMax();
	void PrintMax();
	void InitAllocation();
	void PrintAllocation();
	void InitNeed();
	void PrintNeed();
	int IsAllTrue(bool* arr, int size);
	void PrintSafeSequence(int* arr, int size);
	int findSafeProc(bool* Finish, int* Work);
	bool IsSafe();
	void Request();
	int NumOfResoure = 0;
	int NumOfProcess = 0;
private:
	int Available[100];
	int Max[100][100];
	int Allocation[100][100];
	int Need[100][100];
};

 

#pragma once
#include<iostream>
#include<stdlib.h>
#include"Bank.h"
using namespace std;

class Meau {
public:
	Bank b1;
	void meau()
	{
		int choice;
		while (1)
		{
			cout << "---> 银行家算法 <---" << endl;
			cout << "      [1]:Init		 " << endl;
			cout << "      [2]:Request	 " << endl;
			cout << "      [3]:IsSafe	 " << endl;
			cout << "      [4]:exit		 " << endl;
			cout << "请输入您的选择:>";
			fflush(stdin);
			cin >> choice;
			fflush(stdin);
			switch (choice)
			{
			case 1:
				b1.Init();
				b1.dispaly();
				break;
			case 2:
				b1.Request();
				b1.dispaly();
				break;
			case 3:
				b1.IsSafe();
				b1.dispaly();
				break;
			case 4:
				return;
			}
		}
	}

};
#include"Meau.h"
bool Bank::IsSafe()
{
	bool Finish[100] = { false };
	int Work[100]{};
	
	for (int i = 0; i < NumOfResoure; ++i)
	{
		Work[i] = Available[i];
	}

	int safeSequence[100]{};
	int SafeNum = 0;

	for (int j = 0; j < NumOfProcess; ++j)
	{
		int ret = findSafeProc(Finish, Work);

		if (ret == -1)
		{
			cout << "此状态不安全!!!,无法分配" << endl;
			return false;
		}

		else
		{
			Finish[ret] = true;
			safeSequence[SafeNum++] = ret;
			for (int k = 0; k < NumOfResoure; ++k)
			{
				Work[k] += Allocation[ret][k];
			}
		}
	}

	int ret = IsAllTrue(Finish, NumOfProcess);
	if (ret == 1)
	{
		cout << "此状态安全,安全序列为:";
		PrintSafeSequence(safeSequence, NumOfProcess);
		return true;
	}

	else
	{
		cout << "此状态不安全!!!" << endl;
		return false;
	}
}

int Bank::findSafeProc(bool* Finish, int* Work)
{
	for (int i = 0; i < NumOfProcess; ++i)
	{
		if (Finish[i] == false)
		{
			int flag = 1;
			for (int j = 0; j < NumOfResoure; ++j)
			{
				if (Need[i][j] > Work[j])
				{
					flag = 0;
				}
			}

			if (flag == 1)
			{
				return i;
			}
		}
	}
	return -1;
}
void Bank:: Request()
{
	int Request[100];
	int procNum;
	int falg = 0;
	cout << "请输入请求资源的进程编号 -->";
	cin >> procNum;

	cout << "请输入该进程所需要的" << NumOfResoure << "类资源(Request)数目 -->";

	for (int i = 0; i < NumOfResoure; ++i)
	{
		cin >> Request[i];
		if (Request[i] > Need[procNum][i])
		{
			falg = 1;
			cout << "错误!! 请求的资源大于所需要的资源数目,无法分配" << endl;
		}
		else if (Request[i] <= Need[procNum][i] && Request[i] > Available[i])
		{
			falg = 1;
			cout << "错误!! 请求的资源大于可利用的资源数目,无法分配" << endl;
		}
	}
	if (falg == 1)
	{
		return;
	}

	cout << "暂时分配开始" << endl;

	for (int i = 0; i < NumOfResoure; ++i)
	{
		Available[i] -= Request[i];
		Allocation[procNum][i] += Request[i];
		Need[procNum][i] -= Request[i];
	}
	cout << "正在检查当前状态是否安全" << endl;
	//再执行安全性算法,检查此次资源分配后系统是否处于安全状态
	if (!IsSafe())
	{
		for (int i = 0; i < NumOfResoure; ++i)
		{
			Available[i] += Request[i];
			Allocation[procNum][i] -= Request[i];
			Need[procNum][i] += Request[i];
		}
	}
	else
	{
		int flag = 1;
		for (int i = 0; i < NumOfResoure; ++i)
		{
			if (Need[procNum][i] != 0)
			{
				flag = 0;
			}
		}
		if (flag == 1)
		{
			for (int i = 0; i < NumOfResoure; ++i)
			{
				Available[i] += Allocation[procNum][i];
			}
			for (int i = 0; i < NumOfResoure; ++i)
			{
				Allocation[procNum][i] = 0;
			}
		}
	}
}


void Bank::InitAvailable()
{
	cout << "初始化Available数组" << endl;
	for (int i = 0; i < NumOfResoure; ++i)
	{
		cout << "请输入Resoure" << i + 1 << "的可用资源(Avaliable)数目: --->";
		cin >> Available[i];
	}
}

void Bank::PrintAvailable()
{
	cout << "打印Available数组" << endl;
	for (int i = 0; i < NumOfResoure; ++i)
	{
		cout << Available[i] << " ";
	}
	cout << endl;
}

void Bank::InitMax()
{
	cout << "初始化Max数组" << endl;
	for (int i = 0; i < NumOfProcess; ++i)
	{
		cout << "请输入Proc" << i << "的" << NumOfResoure << "个最大需求资源(Max)数目 -->";
		for (int j = 0; j < NumOfResoure; ++j)
		{
			cin >> Max[i][j];
		}
	}
}

void Bank::PrintMax()
{
	cout << "打印Max数组" << endl;
	for (int i = 0; i < NumOfProcess; ++i)
	{
		for (int j = 0; j < NumOfResoure; ++j)
		{
			cout << Max[i][j] << " ";
		}
		cout << endl;
	}
}

void Bank::InitAllocation()
{
	cout << "初始化Allocation数组" << endl;
	for (int i = 0; i < NumOfProcess; ++i)
	{
		cout << "请输入Proc" << i << "的" << NumOfResoure << "个已分配资源(Allocation)数目 -->";
		for (int j = 0; j < NumOfResoure; ++j)
		{
			cin >> Allocation[i][j];
		}
	}
}
void Bank::PrintAllocation()
{
	cout << "打印Allocation数组" << endl;
	for (int i = 0; i < NumOfProcess; ++i)
	{
		for (int j = 0; j < NumOfResoure; ++j)
		{
			cout << Allocation[i][j] << " ";
		}
		cout << endl;
	}
}
void Bank::InitNeed()
{
	for (int i = 0; i < NumOfProcess; ++i)
	{
		for (int j = 0; j < NumOfResoure; ++j)
		{
			Need[i][j] = Max[i][j] - Allocation[i][j];
		}
	}

}
void Bank::PrintNeed()
{
	cout << "打印Need数组" << endl;
	for (int i = 0; i < NumOfProcess; ++i)
	{
		for (int j = 0; j < NumOfResoure; ++j)
		{
			cout << Need[i][j] << " ";
		}
		cout << endl;
	}

}

int Bank::IsAllTrue(bool* arr, int size)
{
	for (int i = 0; i < size; ++i)
	{
		if (arr[i] == false)
			return 0;
	}
	return 1;
}

void Bank::PrintSafeSequence(int* arr, int size)
{
	for (int i = 0; i < size; ++i)
	{
		cout << arr[i] << "-->";
	}
	cout << "NULL" << endl;
}

int main()
{
	Meau m1;
	m1.meau();
	return 0;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
银行家算法和时间轮转法都是操作系统中常用的调度算法,下面分别进行概述: 1. 银行家算法 银行家算法是一种用于避免死锁的算法,它是由荷兰计算机科学家 Edsger Dijkstra 发明的。银行家算法的基本思想是,系统在分配资源之前,先进行银行家算法安全性检查,以确保分配资源后系统不会进入死锁状态。 银行家算法的实现需要记录系统中每个进程的最大资源需求、已分配资源和尚需资源。系统在分配资源时,需要先检查是否有足够的资源满足进程的最大需求,然后再检查分配资源后系统是否仍然处于安全状态。如果分配资源后系统仍然处于安全状态,则进行资源分配,否则等待。 银行家算法的优点是能够避免死锁,缺点是需要预先知道每个进程的资源需求,且资源分配需要满足某些约束条件,实现起来比较麻烦。 2. 时间轮转法 时间轮转法是一种常用的调度算法,它是由 IBM 公司发明的。时间轮转法的基本思想是,将进程按照先来先服务的原则排队,然后按照一定时间片轮流执行每个进程,直到所有进程都执行完毕。 时间轮转法的优点是简单易实现,并且能够保证公平性,所有进程都有机会得到执行。缺点是可能会出现进程执行时间过长的情况,导致系统响应时间变慢。 在实际应用中,可以根据不同的需求和场景选择合适的调度算法,以达到更好的系统性能和用户体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五毛变向.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值