#include<iostream>
#include <fstream>
#include <queue>
#include<list>
using namespace std;
#define NUMOFPROGRESS 5
#define KINDOFSOURCE 3
class Banker {
private:
int max[NUMOFPROGRESS][KINDOFSOURCE];
int allocation[NUMOFPROGRESS][KINDOFSOURCE] = { 0 };
int need[NUMOFPROGRESS][KINDOFSOURCE];
int availible[KINDOFSOURCE];
queue<int> requireList;
public:
Banker() {
cout << "initialize the banker!" << endl;
ifstream inFile;
inFile.open("data.txt");
inFile >> availible[0];
inFile >> availible[1];
inFile >> availible[2];
int count = 0;
int index = 0;
int num = 0;
int reqArr[4];
for (num = 0; num < 6; num++) {
inFile >> reqArr[0];
inFile >> reqArr[1];
inFile >> reqArr[2];
inFile >> reqArr[3];
requireList.push(reqArr[0]);
requireList.push(reqArr[1]);
requireList.push(reqArr[2]);
requireList.push(reqArr[3]);
}
while (inFile)
{
if (count == 5) {
index++;
count = 0;
}
inFile >> max[index][count];
count++;
inFile.get();
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
need[i][j] = max[i][j];
}
}
}
bool safeCheck() {
int work[3];
work[0] = availible[0];
work[1] = availible[1];
work[2] = availible[2];
bool finish[NUMOFPROGRESS];
int i = 0;
int j = 0;
for (i = 0; i < NUMOFPROGRESS; i++) {
finish[i] = false;
}
for (i = 0; i < NUMOFPROGRESS; i++) {
for (j = 0; j < KINDOFSOURCE; j++) {
if (need[i][j] > work[j]) {
return false;
}
}
for (j = 0; j < KINDOFSOURCE; j++) {
work[j] += allocation[i][j];
}
finish[i] = true;
}
return true;
}
queue<int> getQueue() {
return requireList;
}
int visitNeedArr(int i, int j) {
return need[i][j];
}
int visitAvaArr(int i) {
return availible[i];
}
int visitAlloArr(int i, int j) {
return allocation[i][j];
}
void setAvaArr(int i, int num) {
availible[i] = num;
}
void setAlloArr(int i, int j, int num) {
allocation[i][j] = num;
}
void setNeedArr(int i, int j, int num) {
need[i][j] = num;
}
};
void run(Banker *banker){
queue<int> que = banker->getQueue();
while (!banker->getQueue().empty()) {
int index = que.front();
que.pop();
int reqA = que.front();
que.pop();
int reqB = que.front();
que.pop();
int reqC = que.front();
que.pop();
if (banker->visitNeedArr(index, 0) >=reqA&&banker->visitNeedArr(index, 1) >=reqB&&banker->visitNeedArr(index, 2) >=reqC) {
if (banker->visitAvaArr(0) >=reqA&&banker->visitAvaArr(1) >=reqB&&banker->visitAvaArr(2) >=reqC) {//预分配
cout << "预分配中" << "进程" << index << "分配A资源:" << reqA << "分配V资源:" << reqB << "分配C资源:" << reqC <<endl;
banker->setAvaArr(0, banker->visitAvaArr(0) - reqA);
banker->setAvaArr(1, banker->visitAvaArr(1) - reqB);
banker->setAvaArr(2, banker->visitAvaArr(2) - reqC);
banker->setAlloArr(index, 0, banker->visitAlloArr(index, 0) + reqA);
banker->setAlloArr(index, 1, banker->visitAlloArr(index, 1) + reqB);
banker->setAlloArr(index, 2, banker->visitAlloArr(index, 2) + reqC);
banker->setNeedArr(index, 0, banker->visitNeedArr(index, 0) - reqA);
banker->setNeedArr(index, 1, banker->visitNeedArr(index, 1) - reqA);
banker->setNeedArr(index, 2, banker->visitNeedArr(index, 2) - reqA);
if (banker->safeCheck()==true) {//正式分配
cout << "正式分配!" << endl;
cout << "进程" << index<<"分配A资源:"<<reqA<<"分配V资源:"<<reqB<<"分配C资源:"<<reqC << endl;
}
else {//恢复状态
cout << "恢复状态" << endl;
banker->setAvaArr(0, banker->visitAvaArr(0) + reqA);
banker->setAvaArr(1, banker->visitAvaArr(1) + reqB);
banker->setAvaArr(2, banker->visitAvaArr(2) + reqC);
banker->setAlloArr(index, 0, banker->visitAlloArr(index, 0) - reqA);
banker->setAlloArr(index, 1, banker->visitAlloArr(index, 1) - reqB);
banker->setAlloArr(index, 2, banker->visitAlloArr(index, 2) - reqC);
banker->setNeedArr(index, 0, banker->visitNeedArr(index, 0) + reqA);
banker->setNeedArr(index, 1, banker->visitNeedArr(index, 1) + reqA);
banker->setNeedArr(index, 2, banker->visitNeedArr(index, 2) + reqA);
que.push(index);//需求插到队尾
que.push(reqA);
que.push(reqB);
que.push(reqC);
}
}
}
else {
cout << "需求超过最大值!" << endl;
}
}
};
int main() {
Banker *banker=new Banker();
run(banker);
return 0;
}
c++ 银行家算法【操作系统】
最新推荐文章于 2023-05-19 10:15:00 发布