学习期间自己写的,希望能帮到大家。
//============================================================================
// Name : BankerAlgorithm.cpp
// Author : Aen
// Copyright : SDJZU
// Description : Banker's Algorithm in C++
//============================================================================
#include <iostream>
using namespace std;
bool SafeAlgorithm();//安全性算法
void Menu();//菜单
void InputData();//输入函数
void UpdateData();//修改数据
void ShowData();//显示信息
void ShowSafeData();//显示安全序列
void Res();//判断是否安全
int Avaliable[100] = { 0 }; //拥有资源
int Max[100][100] = { 0 }; //共需要
int Allocation[100][100] = { 0 }; //已得到
int Need[100][100] = { 0 }; //还需要
int Work[100]={0}; //可提供的各类资源数目
int SafeWork[100][100]={0}; //显示数组
bool Finish[100]={false}; //判断是否有足够的资源分给进程
int ProcessNumber=100; //最大进程数 初始化
int ResourceNumber=100; //最大资源数 初始化
char ResourceName[100]={0}; // 资源名称
int SafeSequence[100]={0}; //排序后的进程顺序
/*
* 这个程序写的有点冗杂,安全性算法是SafeAlgorithm()这个函数,银行家算法直接写在了InputData()这个函数中
*/
int main() {
Menu();
InputData();
ShowData();
Res();
ShowSafeData();
UpdateData();
return 0;
}
void InputData(){
cout << "请输入可提供的资源种类数量:";
cin >> ResourceNumber;
for(int i=0;i<ResourceNumber;i++){
cout << "第" << i+1 << "个资源名字:" ;
cin >> ResourceName[i];
cout << "资源数量:" ;
cin >>Avaliable[i];
}
cout << "请输入进程的数量:";
cin >> ProcessNumber;
cout << "请输入每个进程的Max值:" <<endl;
for(int i=0;i<ProcessNumber;i++){
for(int j=0;j<ResourceNumber;j++){
cin >> Max[i][j];
}
}
cout << "请输入每个进程的Allocation值:" << endl;
StartAllocation:
while(1){
for(int i=0;i<ProcessNumber;i++){
for(int j=0;j<ResourceNumber;j++){
cin >> Allocation[i][j];
if(Allocation[i][j]>Max[i][j]){
cout << "申请的资源大于最大值,请重新输入!\n";//假如矩阵输入输入错误会输出多个
goto StartAllocation;
}
Need[i][j] = Max[i][j] - Allocation[i][j];
Avaliable[j] = Avaliable[j] - Allocation[i][j];
}
}
break;
}
//给 Work[] 赋值
for(int i=0;i<ResourceNumber;i++){
Work[i] = Avaliable[i];
}
}
void UpdateData(){
char YN;
int num,q[100]={0},p=0;
while(1){
cout<<"是否修改以上的信息?(Y/N)";
cin >> YN;
if(YN=='Y'||YN=='y'){
cout<<"请输入要修改的进程(";
for(int i=0;i<ProcessNumber;i++){
cout<<i<<" ";
}
cout<<")";
cin >> num;
cout<<"请输入请求的资源数:";
StartUpdate:
for(int i=0;i<ResourceNumber;i++){
cin >> q[i];
}
for(int i=0;i<ResourceNumber;i++){
if((q[i]<=Need[num][i])&&(q[i]<=Avaliable[i])){
++p;
}else{
cout<<"请重新输入请求的资源数:";
goto StartUpdate;
}
}
if(p==ResourceNumber){
for(int i=0;i<ResourceNumber;i++){
Avaliable[i] = Avaliable[i] - q[i];
Need[num][i] = Need[num][i] - q[i];
Allocation[num][i] = Allocation[num][i] + q[i];
}
//给 Work[] 赋值
for(int i=0;i<ResourceNumber;i++){
Work[i] = Avaliable[i];
}
ShowData();
Res();
ShowSafeData();
}
}else{
cout<<"程序结束!"<<endl;
return;
}
}
}
void ShowData(){
cout << " 资源情况 Max Allocation Need Available" << endl;
cout << " 进程 ";
for (int i = 0; i < 3; i++){
for (int j = 0; j < ResourceNumber; j++){
cout << ResourceName[j] << " ";
}
cout << " ";
}
cout<<endl;
for (int i = 0; i < ProcessNumber; i++){
cout << " P" << i << " ";
for (int j = 0; j < ResourceNumber; j++){
cout << Max[i][j] << " ";
}
cout << " ";
for (int j = 0; j < ResourceNumber; j++){
cout << Allocation[i][j] << " ";
}
cout << " ";
for (int j = 0; j < ResourceNumber; j++){
cout << Need[i][j] << " ";
}
if(i==0){
cout << " ";
for (int j = 0; j < ResourceNumber; j++){
cout << Avaliable[j] << " ";
}
}
cout << endl;
}
}
void ShowSafeData(){
cout << " 资源情况 Work Need Allocation Work+Allocation Finish" << endl;
cout << " 进程 ";
for (int i = 0; i < 4; i++){
for (int j = 0; j < ResourceNumber; j++){
cout << ResourceName[j] << " ";
}
cout << " ";
}
cout<<endl;
for (int i = 0; i < ProcessNumber; i++){
cout << " P" << SafeSequence[i] << " ";
for (int j = 0; j < ResourceNumber; j++){
cout << SafeWork[SafeSequence[i]][j] << " ";
}
cout << " ";
for (int j = 0; j < ResourceNumber; j++){
cout << Need[SafeSequence[i]][j] << " ";
}
cout << " ";
for (int j = 0; j < ResourceNumber; j++){
cout << Allocation[SafeSequence[i]][j] << " ";
}
cout << " ";
for (int j = 0; j < ResourceNumber; j++){
cout << SafeWork[SafeSequence[i]][j]+Allocation[SafeSequence[i]][j] << " ";
}
cout << " ";
if(Finish[i]==true){
cout << "true" << " ";
}
cout << endl;
}
}
bool SafeAlgorithm(){//安全性算法
int Y=1,X=0,count=0, s=0;
for(int i=0;i<100;i++){//初始化 finish数组
Finish[i]=false;
}
while(Y){
for(int i=0;i<ProcessNumber;i++){
count=0;
for(int j=0;j<ResourceNumber;j++){
if((Finish[i]==false)&&(Need[i][j]<=Work[j])){//安全性算法
++count;
}
if(count==ResourceNumber){//假如一个进程对所有资源都满足
for(int k=0;k<ResourceNumber;k++){
SafeWork[i][k]=Work[k];
Work[k] = Work[k] + Allocation[i][k];
}
Finish[i]=true;
SafeSequence[s]=i;//存储进程安全序列
++s;
count=0;
++X;
}
}
}
if(X==ProcessNumber){
Y=0;
}
}
for(int i=0;i<ProcessNumber;i++){
if(Finish[i]==false){
return false;
}
}
return true;
}
void Res(){
if(SafeAlgorithm()){
cout<<"It's safe!"<<endl;
for(int i=0;i<ProcessNumber;i++){
cout<<"P"<<SafeSequence[i];
if(i<ProcessNumber-1){
cout<<"->";
}
}
}else{
cout<<"It's dangerous!"<<endl;
}
cout<<endl;
}
void Menu(){
cout << "\t|| 银行家算法实现 ||" << endl;
cout << "\t|| 作者 : 夏德鑫 ||" << endl;
cout << "\t|| 2017/11/19 ||" << endl;
cout << "\n\n"<<endl;
cout << "\t|| 银行家算法开始 ||" << endl;
cout << "\n"<<endl;
}
程序运行截图:(图片内容显示较小,放大页面可以看的清楚)