#pragma warning (disable:4996)
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <fstream>
#include <vector>
/*
初始情况:
进程\资源情况 Max Allocation Need Available
P0 7 5 3 0 1 0 7 4 3 3 3 2
P1 3 2 2 2 0 0 1 2 2
P2 9 0 2 3 0 2 6 0 0
P3 2 2 2 2 1 1 0 1 1
P4 4 3 3 0 0 2 4 3 1
*/
/*
进程请求:
(P)1 1 0 2
(P)4 3 3 0
*/
const int N = 5;//进程个数
const int M = 3;//资源个数
class System {
private:
int Available[M];//可利用资源向量:系统中可利用的资源
int Max[N][M];//最大需求矩阵:所有进程对资源的最大需求
int Allocation[N][M];//分配矩阵:系统中的资源分配
int Need[N][M];//需求矩阵:所有进程还需要多少资源
public:
void initial();
bool safe_algorithm();
bool banker_algorithm(int, int[]);
void exced();
};
void System::initial() {
std::ifstream file("data.txt", std::ios::in);
if (!file) {//文件流输入错误信息
std::cerr << "err!" << std::endl;
exit(1);
}
for (int i = 0; i < M; i++) {
file >> Available[i];
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
file >> Max[i][j];
}
for (int j = 0; j < M; j++) {
file >> Allocation[i][j];
}
for (int j = 0; j < M; j++) {
file >> Need[i][j];
}
}
file.close();
std::cout << "资源情况 Max\t\tAllocation\tNeed\t\tAvailable" << std::endl;
std::cout << " A B C\t\tA B C\t\tA B C\t\tA B C" << std::endl;
for (int i = 0; i < N; i++) {
std::cout << " P" << i << " ";
for (int j = 0; j < M; j++) {
std::cout << Max[i][j] << " ";
}
std::cout << "\t";
for (int j = 0; j < M; j++) {
std::cout << Allocation[i][j] << " ";
}
std::cout << "\t\t";
for (int j = 0; j < M; j++) {
std::cout << Need[i][j] << " ";
}
std::cout << "\t\t";
for (int j = 0; j < M; j++) {
std::cout << Available[j] << " ";
}
std::cout << std::endl;
}
}
bool System::safe_algorithm() {
int Work[M];//工作向量:可提供给进程继续运行所需的资源的数目
bool Finish[N];//是否有足够的资源可分配给进程,使之运行完成
for (int i = 0; i < M; i++) {
Work[i] = Available[i];
}
for (int j = 0; j < N; j++) {
Finish[j] = false;
}
std::vector<int> order;
int cnt = 0;
while (cnt < N) {
bool has = false;
for (int i = 0; i < N; i++) {
if (Finish[i] == false) {
bool flag = true;
for (int j = 0; j < M; j++) {
if (Need[i][j] > Work[j]) {
flag = false;
break;
}
}
if (flag) {
for (int j = 0; j < M; j++) {
Work[j] = Work[j] + Allocation[i][j];
}
order.push_back(i);
Finish[i] = true;
cnt++;
has = true;
break;
}
}
}
if (!has) {
return false;
}
}
std::cout << "the safe order: { ";
for (int i = 0; i < order.size(); i++) {
if (i < order.size() - 1)
std::cout << "P[" << order[i] << "], ";
else
std::cout << "P[" << order[i] << "]";
}
std::cout << " }" << std::endl;
for (int i = 0; i < M; i++) {
Work[i] = Available[i];
}
reverse(order.begin(), order.end());
std::cout << "资源情况 Work\t\tNeed\t\tAllocation\tAllocation+Work\tFinish" << std::endl;
std::cout << " A B C\t\tA B C\t\tA B C\t\tA B C" << std::endl;
while (order.size()) {
int i = order.back();
order.pop_back();
std::cout << " P" << i << " ";
for (int j = 0; j < M; j++) {
std::cout << Work[j] << " ";
}
std::cout << "\t";
//std::cout << " ";
for (int j = 0; j < M; j++) {
std::cout << Need[i][j] << " ";
}
std::cout << "\t\t";
//std::cout << " ";
for (int j = 0; j < M; j++) {
std::cout << Allocation[i][j] << " ";
}
std::cout << "\t\t";
//std::cout << " ";
for (int j = 0; j < M; j++) {
std::cout << Allocation[i][j] + Work[j] << " ";
Work[j] = Allocation[i][j] + Work[j];
}
std::cout << "\t\ttrue" << std::endl;
//std::cout << " true" << std::endl;
}
return true;
}
bool System::banker_algorithm(int i, int Request[]) {
for (int j = 0; j < M; j++) {
if (Request[j] > Need[i][j]) {
std::cerr << "more than NEED!" << std::endl;
return false;
}
}
//尝试分配资源
for (int j = 0; j < M; j++) {
Available[j] = Available[j] - Request[j];
Allocation[i][j] = Allocation[i][j] + Request[j];
Need[i][j] = Need[i][j] - Request[j];
}
if (safe_algorithm()) {
return true;
}
else {
for (int j = 0; j < M; j++) {
Available[j] = Available[j] + Request[j];
Allocation[i][j] = Allocation[i][j] - Request[j];
Need[i][j] = Need[i][j] + Request[j];
}
return false;
}
}
void System::exced() {
initial();
std::cout << std::endl;
std::cout << "input REQUEST:" << std::endl;
std::cout << "thread | A | B | C | " << std::endl;
int i;
int Request[M];
while (std::cin >> i) {
for (int j = 0; j < M; j++) {
std::cin >> Request[j];
}
if (banker_algorithm(i, Request)) {
std::cout << "===============successful exced!===============" << std::endl;
}
else {
std::cout << "======failed to exced! please input again!=====" << std::endl;
}
std::cout << "thread | A | B | C | " << std::endl;
}
}
int main() {
System s;
s.exced();
return 0;
}