银行家算法
安全性检测资源申请
#include <bits/stdc++.h>
using namespace std;
const int N = 100;
const int total_resources = 3;
struct process
{
int resources_max[total_resources];
int resources_allocation[total_resources];
int resources_need[total_resources];
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;
bool check(int cur)
{
for (int i = 0; i < total_resources; i++)
{
if (p[cur].resources_need[i] > work[i])
return false;
}
return true;
}
void nextWork(int i)
{
for (int j = 0; j < total_resources; ++j)
{
work[j] += p[i].resources_allocation[j];
}
finished[i] = true;
}
void prevWork(int i)
{
for (int j = 0; j < total_resources; ++j)
{
work[j] -= p[i].resources_allocation[j];
}
finished[i] = false;
}
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;
}
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);
}
}
}
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] = 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 = 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;
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-541.html