<pre name="code" class="cpp">#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
typedef struct{
//进程号
int numberOfThread;
//该进程对每个资源的需求量
int countOf_A, countOf_B;
int countOf_C;
//该进程是否完成
bool isFinish;
}infor;
//在银行家算法中,总共需要4个向量
//每个资源拥有的剩余资源数量
vector<int>avliable;
//每个资源已经分配的,最大需求,和需要的
vector<infor>allocation;
vector<infor>max;
vector<infor>need;
void getInforOfThread();
void getNeed();
void getReslut();
int main(int argc, char argv[])
{
getInforOfThread();
getNeed();
cout << "获取分配资源后剩余资源数:" << endl;
for (int i = 0; i < 3; i++)
{
int num;
cin >> num;
avliable.push_back(num);
}
getReslut();
return 0;
}
void getReslut()
{
int countNum[3];
auto iterOfAvliable = avliable.begin();
int i = 0;
for (; iterOfAvliable != avliable.end(); iterOfAvliable++)
{
countNum[i++] = *iterOfAvliable;
}
int j = 0;
int count = 0;
bool flag = false;
int length = need.size();
while (j < length)
{
if (count < length)
{
auto iterOfNeed = need.begin();
auto iterOfMax = max.begin();
for (; iterOfNeed != need.end(); iterOfNeed++)
{
if (countNum[0] >= iterOfNeed->countOf_A&&countNum[1] >= iterOfNeed->countOf_B&&countNum[2] >= iterOfNeed->countOf_C&&iterOfNeed->isFinish == false)
{
//当前拥有资源数量满足该进程所需要的数量
//打印出当前每个资源的剩余个数
for (int i = 0; i < 3; i++)
{
cout << i << "类资源: " << countNum[i] << " ";
}
cout << endl;
//打印出当前可以被执行的进程所需要的资源数
cout << "当前可执行进程号:" << iterOfNeed->numberOfThread << " A:" << iterOfNeed->countOf_A << " B:" << iterOfNeed->countOf_B << " C:" << iterOfNeed->countOf_C << endl;
iterOfNeed->isFinish = true;
cout << "进程p" << iterOfNeed->numberOfThread << endl;
for (; iterOfMax != max.end(); iterOfMax++)
{
if (iterOfMax->numberOfThread == iterOfNeed->numberOfThread&&iterOfMax->isFinish == false)
break;
}
countNum[0] += iterOfMax->countOf_A;
countNum[1] += iterOfMax->countOf_B;
countNum[2] += iterOfMax->countOf_C;
iterOfMax->isFinish = true;
count++;
break;
}
}
}
else
{
flag = true;
break;
}
j++;
}
if (flag)
{
cout << "系统不安全" << endl;
exit(true);
}
}
void getInforOfThread()
{
//获得每个进程所需的资源最大量,和每个进程已经分配的数量
ifstream cin("test.txt");
int numberOfThread, count_A, count_B, count_C;
int alloc_A, alloc_B, alloc_C;
infor str, ptr;
while (!cin.eof())
{
cin >> numberOfThread >> count_A >> count_B >> count_C >> alloc_A >> alloc_B >> alloc_C;
str.numberOfThread = numberOfThread;
str.countOf_A = count_A;
str.countOf_B = count_B;
str.countOf_C = count_C;
str.isFinish = false;
max.push_back(str);
ptr.numberOfThread = numberOfThread;
ptr.countOf_A = alloc_A;
ptr.countOf_B = alloc_B;
ptr.countOf_C = alloc_C;
ptr.isFinish = false;
allocation.push_back(ptr);
}
cin.close();
}
void getNeed()
{
//计算每个进程剩余所需资源的数量
auto iterOfAllocation = allocation.begin();
auto iterOfMax = max.begin();
infor temp;
for (; iterOfAllocation != allocation.end(), iterOfMax != max.end(); iterOfAllocation++, iterOfMax++)
{
temp.numberOfThread = iterOfAllocation->numberOfThread;
temp.countOf_A = iterOfMax->countOf_A - iterOfAllocation->countOf_A;
temp.countOf_B = iterOfMax->countOf_B - iterOfAllocation->countOf_B;
temp.countOf_C = iterOfMax->countOf_C - iterOfAllocation->countOf_C;
temp.isFinish = false;
//temp.countOf_D = iterOfMax->countOf_D - iterOfAllocation->countOf_D;
need.push_back(temp);
}
}
<img src="https://img-blog.csdn.net/20160627164659075?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
<img src="https://img-blog.csdn.net/20160627164743716?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />