【问题描述】编程实现银行家算法。
设计五个进程{P0,P1,P2,P3,P4}共享三类资源{A,B,C}的系统,{A,B,C}的资源数量分别为10,5,7。进程可动态地申请资源和释放资源,系统按各进程的申请动态地分配资源。在T0时刻的资源分配情况如下图所示:
要求:
(1)判断T0时刻系统是否处于安全状态?若是,请输出所有的安全序列。
(2)若进程P1请求资源,发出请求向量Request1(1,0,2),编写程序用银行家算法判断系统能否将资源分配给它;若可以,请输出所有的安全序列。
【输入形式】
【输出形式】
【样例输入】以文件形式输入
5 (表示进程个数,下面表示进程名)
P0
P1
P2
P3
P4
3 (表示资源个数)
0 1 0 (表示P0已经获得的3个资源的个数)
2 0 0 (表示P1已经获得的3个资源的个数)
3 0 2 (表示P2已经获得的3个资源的个数)
2 1 1 (表示P3已经获得的3个资源的个数)
0 0 2 (表示P4已经获得的3个资源的个数)
7 4 3 (表示P0还需要的资源个数)
1 2 2 (表示P1还需要的资源个数)
6 0 0 (表示P2还需要的资源个数)
0 1 1 (表示P3还需要的资源个数)
4 3 1 (表示P4还需要的资源个数)
3 3 2 (表示系统中还剩余资源个数)
P1 (表示在当前状态下请求资源的进程名)
1 0 2 (表示P0请求的资源个数)
【样例输出】
Process Allocatioin Need Available
P0 0 1 0 7 4 3 3 3 2
P1 2 0 0 1 2 2 3 3 2
P2 3 0 2 6 0 0 3 3 2
P3 2 1 1 0 1 1 3 3 2
P4 0 0 2 4 3 1 3 3 2
Current system state is safe!
Safety sequence are:
P1 P3 P0 P2 P4
P1 P3 P0 P4 P2
P1 P3 P2 P0 P4
P1 P3 P2 P4 P0
P1 P3 P4 P0 P2
P1 P3 P4 P2 P0
P1 P4 P3 P0 P2
P1 P4 P3 P2 P0
P3 P1 P0 P2 P4
P3 P1 P0 P4 P2
P3 P1 P2 P0 P4
P3 P1 P2 P4 P0
P3 P1 P4 P0 P2
P3 P1 P4 P2 P0
P3 P4 P1 P0 P2
P3 P4 P1 P2 P0
Process Allocatioin Need Available
P0 0 1 0 7 4 3 2 3 0
P1 3 0 2 0 2 0 2 3 0
P2 3 0 2 6 0 0 2 3 0
P3 2 1 1 0 1 1 2 3 0
P4 0 0 2 4 3 1 2 3 0
P1 's resources can be requested!
Safety sequence are:
P1 P3 P0 P2 P4
P1 P3 P0 P4 P2
P1 P3 P2 P0 P4
P1 P3 P2 P4 P0
P1 P3 P4 P0 P2
P1 P3 P4 P2 P0
P1 P4 P3 P0 P2
P1 P4 P3 P2 P0
#define MAXPROCESS 255
#include <iostream>
#include <string>
#include <vector>
#include<fstream>
using namespace std;
// 进程类定义
class Process
{
public:
string name;// 进程名字
int allocation_and_need_size;// 资源种类数目
int* allocation;// 已分配资源
int* need;// 需求资源
bool is_Finish = false;// 是否完成
Process(int allocation_and_need_size, string name);
Process();
};
// 构造函数初始化
Process::Process(int allocation_and_need_size, string name) {
Process::allocation_and_need_size = allocation_and_need_size;
Process::name = name;
// 初始化数组
allocation = new int[allocation_and_need_size];
need = new int[allocation_and_need_size];
}
// 默认构造函数,用于动态分配内存
Process::Process() {}
// 系统剩余资源
int* work;
// n:进程个数 m:资源种类
int n, m;
// 进程
Process* processes;
// 安全序列
vector<vector<Process>> safe;
vector<Process> lsafe;
void print_table() {
printf("Process Allocatioin Need Available\n");
for (int i = 0; i < n; i++) {
cout << processes[i].name;
printf(" ");
for (int j = 0; j < processes[i].allocation_and_need_size; j++) {
if (j) cout << " ";
printf("%d", processes[i].allocation[j]);
}
printf(" ");
for (int j = 0; j < processes[i].allocation_and_need_size; j++) {
if (j) cout << " ";
printf("%d", processes[i].need[j]);
}
printf(" ");
for (int j = 0; j < m; j++) {
if (j) cout << " ";
printf("%d", work[j]);
}
printf("\n");
}
}
bool resource_compare(int* compare) {
for (int i = 0; i < m; i++) {
if (compare[i] > work[i]) return false;
}
return true;
}
// 深搜找安全序列
void isSafe(int v) {
if(v==n){
safe.push_back(lsafe);
return;
}
//遍历寻找合适的开始进程
for(int i=0;i<n;i++){
//判断进程是否处理完成
if(!processes[i].is_Finish){
//判断进程是否可以处理
if(resource_compare(processes[i].need)){
//暂时放入安全序列
processes[i].is_Finish=true;
lsafe.push_back(processes[i]);
for(int j=0;j<m;j++)
work[j]+=processes[i].allocation[j];
isSafe(v+1);//搜索下一个运行程序
//回溯
lsafe.pop_back();
for(int j=0;j<m;j++)
work[j]-=processes[i].allocation[j];
processes[i].is_Finish=0;
}
}
}
}
void printSafe() {
for (vector<Process> lsafe : safe) {
for (Process process : lsafe) {
cout << process.name << " ";
}
cout << endl;
}
}
int main() {
// 测试文本
//FILE* stream;
//freopen_s(&stream, "test.txt", "r", stdin);
ifstream txtfile;
txtfile.open("test.txt");
//cout << "请输入进程个数:";
txtfile >> n;
// 对进程数组初始化
processes = new Process[n];
// 读入进程名
for (int i = 0; i < n; i++) {
//cout << "请输入第" << i + 1 << "个进程名:";
string name;
txtfile >> name;
processes[i].name = name;
}
//cout << "请输入资源种类数:";
txtfile >> m;
// 读入已分配资源数
//cout << "空格分隔两个数字" << endl;
for (int i = 0; i < n; i++) {
//cout << "请输入" << processes[i].name << "的已分配资源数:";
processes[i].allocation_and_need_size = m;
processes[i].allocation = new int[m];
for (int j = 0; j < m; j++) {
txtfile >> processes[i].allocation[j];
}
}
// 读入还需资源数
//cout << "空格分隔两个数字" << endl;
for (int i = 0; i < n; i++) {
//cout << "请输入" << processes[i].name << "的还需资源数:";
processes[i].need = new int[m];
for (int j = 0; j < m; j++) {
txtfile >> processes[i].need[j];
}
}
// 读入空闲资源数
//cout << "请输入空闲资源数:";
work = new int[m];
for (int i = 0; i < m; i++) {
txtfile >> work[i];
}
cout << endl;
// 打印当前系统中资源状况
print_table();
isSafe(0);
// 判断当前系统状态是否安全
if (safe.size() == 0) {
cout << "Current system state is not secure!" << endl;
}
else {
cout << "Current system state is safe!" << endl << "Safety sequence are:" << endl;
printSafe();
}
// 判断进程发成请求后的安全序列与状态
string process_name;
int* req = new int[m];
//cout << "请输入发出请求的进程名:";
txtfile >> process_name;
//cout << "请输入请求的资源数:";
for (int i = 0; i < m; i++) {
txtfile >> req[i];
}
// 请求后的系统状态
lsafe.clear();
safe.clear();
for (int i = 0; i < m; i++) {
work[i] -= req[i];
}
for (int i = 0; i < n; i++) {
if (processes[i].name == process_name) {
for (int j = 0; j < m; j++) {
processes[i].allocation[j] += req[j];
processes[i].need[j] -= req[j];
}
}
}
print_table();
// 安全性检测
isSafe(0);
if (safe.size()) {
cout << process_name << " 's resources can be requested!" << endl << "Safety sequence are:" << endl;
printSafe();
}
else {
cout << process_name << "The amount of resources can not be requested!" << endl;
}
// 还原系统状态
lsafe.clear();
safe.clear();
for (int i = 0; i < m; i++) {
work[i] += req[i];
}
for (int i = 0; i < n; i++) {
if (processes[i].name == process_name) {
for (int j = 0; j < m; j++) {
processes[i].allocation[j] -= req[j];
processes[i].need[j] += req[j];
}
}
}
}