银行家算法的实现c++
时间: 2024-05-25 12:08:40 浏览: 21
银行家算法是用来防止死锁的一种资源分配算法,它可以判断系统在分配资源后是否会进入死锁状态。在C++中,可以通过以下步骤实现银行家算法:
1. 定义资源数量和进程数量,并且定义一个二维数组max来表示每个进程对各类资源的最大需求量。
2. 定义一个一维数组available来表示每个资源现在可用的数量。
3. 定义一个二维数组allocation来表示每个进程已经分配到的各类资源数量。
4. 定义一个二维数组need来表示每个进程还需要的各类资源数量(即max-allocation)。
5. 实现安全性检查函数,该函数需要定义一个一维数组work来表示每个资源现在可用的数量,以及一个一维数组finish来表示每个进程是否可以完成。
6. 实现资源请求函数,该函数需要判断请求的资源数量是否小于等于need和available,如果是,则分配资源,并更新need、allocation和available数组,如果不是,则提示无法分配资源。
以下是一个简单的C++实现示例:
```c++
#include <iostream>
#include <vector>
using namespace std;
const int N = 10010;
int n, m;
int available[N], allocation[N][N], maxn[N][N], need[N][N];
bool check_safe()
{
int work[N];
bool finish[N];
for(int i = 1; i <= m; i++)
work[i] = available[i];
for(int i = 1; i <= n; i++)
finish[i] = false;
int cnt = 0;
while(cnt < n)
{
bool flag = false;
for(int i = 1; i <= n; i++)
{
if(!finish[i])
{
bool ok = true;
for(int j = 1; j <= m; j++)
if(need[i][j] > work[j])
{
ok = false;
break;
}
if(ok)
{
flag = true;
finish[i] = true;
cnt++;
for(int j = 1; j <= m; j++)
work[j] += allocation[i][j];
}
}
}
if(!flag)
return false;
}
return true;
}
bool resource_request(int pid, vector<int>& request)
{
for(int i = 1; i <= m; i++)
if(request[i] > need[pid][i] || request[i] > available[i])
return false;
for(int i = 1; i <= m; i++)
{
available[i] -= request[i];
allocation[pid][i] += request[i];
need[pid][i] -= request[i];
}
if(check_safe())
return true;
else
{
for(int i = 1; i <= m; i++)
{
available[i] += request[i];
allocation[pid][i] -= request[i];
need[pid][i] += request[i];
}
return false;
}
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= m; i++)
cin >> available[i];
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >> maxn[i][j];
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
cin >> allocation[i][j];
need[i][j] = maxn[i][j] - allocation[i][j];
}
if(check_safe())
cout << "Safe" << endl;
else
cout << "Unsafe" << endl;
vector<int> request(m + 1);
int pid;
cin >> pid;
for(int i = 1; i <= m; i++)
cin >> request[i];
if(resource_request(pid, request))
cout << "Request Accepted" << endl;
else
cout << "Request Denied" << endl;
return 0;
}
```
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)