#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
int Resource[3] = { 10,5,7 }; //资源总量
//各进程所需资源总量
int Max[5][3] = { {7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3} };
//各进程已占有资源
int Allocation[5][3] = { {0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2} };
//各进程还需要的资源
int Need[5][3];
//可用资源
int Available[3] = { 3,3,2 };
//申请的资源
int Request[5][3];
//安全函数里的参数
int temp[5];//记录顺序
int Work[3]; //工作函数
void Print();
void NeedInit();
void Banker();
bool Safe();
int main()
{
cout << "*************************初始状态************************" << endl;
NeedInit();
Print();
cout << "************************银行家算法***********************" << endl;
Banker();
system("pause");
return 0;
}
void NeedInit()
{
//Need初始化
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
Need[i][j] = Max[i][j] - Allocation[i][j];
}
}
}
void Print()
{
//输出各进程资源分配表
cout << "Max" << endl;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
cout << Max[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
cout << "Allocation" << endl;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
cout << Allocation[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
cout << "Need" << endl;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
cout << Need[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
cout << "Available" << endl;
for (int i = 0; i < 3; i++)
cout << Available[i] << " ";
cout << endl << endl;
}
void Banker()
{
pro:
cout << "输入进程:";
int p, i, j;
cin >> p;
if (p > 4)
{
cout << "没有该进程,请重新输入" << endl;
goto pro;
}
source:
cout << "请输入进程所请求的各资源的数量" << endl;
for (i = 0; i < 3; i++)
{
cin >> Request[p][i];
}
for (i = 0; i < 3; i++)
{
if (Request[p][i] > Need[p][i])//如果用户选择的线程的第i个资源请求数>该线程该资源所需的数量
{
cout << "您输入的请求数超过进程的需求量!请重新输入!" << endl;
goto source;
}
if (Request[p][i] > Available[i])//如果用户选择的线程的第i个资源请求数>系统现有的第i个资源的数量
{
cout << "您输入的请求数超过系统有的资源数!请重新输入!" << endl;
goto source;
}
}
for (i = 0; i < 3; i++)//如果请求合理,那么下面
{
Available[i] -= Request[p][i];//系统可用资源减去申请了的
Allocation [p][i] += Request[p][i];//线程被分配的资源加上已申请了的
Need[p][i] -= Request[p][i];//线程还需要的资源减去已申请得到的
}
if (Safe()==true) //测试安全算法
{
cout << "经安全性检查,系统安全,本次分配成功。" << endl << endl;
cout << "本次安全序列:" << endl;
for (int i = 0; i < 5; i++)//打印安全系统的进程调用顺序
{
cout<<"进程";
cout << temp[i];
if (i < 4)
cout<<"->";
}
cout << endl;
cout << "*************************余下状态************************" << endl;
Print();
goto pro;
}
else //restore original state
{
cout << "您的请求被拒绝!" << endl;
for (i = 0; i < 3; i++)
{
Available[i] += Request[p][i]; //恢复现场
Allocation[p][i] -= Request[p][i];
Need[p][i] += Request[p][i];
}
cout << "*************************余下状态************************" << endl;
Print();
goto pro;
}
}
bool Safe() //安全测试函数
{
vector<int> v;
vector<int>::iterator iter;
v.clear();
bool Finish[5] = { false,false ,false ,false ,false };
int count = 0, k = 0;
for (int i = 0; i < 3; i++) //赋值,保护现场
{
Work[i] = Available[i];
}
for (int i = 0; i < 5; i++)
{
count = 0;
for (int j = 0; j < 3; j++)
{
if (Finish[i] == false && Need[i][j] <= Work[j])
count++;
}
if (count == 3)//当进程i 各类资源都满足NEED<=WORK时
{
for (int m = 0; m < 3; m++)
Work[m] += Allocation[i][m]; //新的available
Finish[i] = true;
temp[k] = i;//记录进程号
k++;
if (k == 5 && v.empty()) //所有进程都能运行
{
return true;
}
}
else
{
v.push_back(i); //阻塞当前进程
continue;
}
}
iter = v.end() - 1;
//遍历阻塞的进程
while (!v.empty())
{
int i = *iter;
count = 0;
for (int j = 0; j < 3; j++)
{
if (Finish[i] == false && Need[i][j] <= Work[j])
count++;
}
if (count == 3)
{
v.pop_back(); //从阻塞队列中移除
for (int m = 0; m < 3; m++)
Work[m] += Allocation[i][m]; //新的available
Finish[i] = true;
temp[k] = i;//记录进程号
k++;
if (k == 5 && v.empty()) //所有进程都能运行
{
return true;
}
iter = v.end() - 1; //更新迭代器,保证每个阻塞的进程都能得到遍历
}
else
{
if (iter == v.begin()) //余下阻塞进程都不满足
{
return false;
}
else
iter--;
}
}
cout << endl << endl;
return true;
}
操作系统实验 银行家算法
最新推荐文章于 2024-02-13 14:51:47 发布