应付一下课设 写的一般
一、目的
通过编写一个模拟动态资源分配的银行家算法程序,进一步深入理解死锁、产生死锁的必要条件、安全状态等重要概念,并掌握避免死锁的具体实施方法。
二、内容
(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;
}