操作系统(第四版)银行家算法C++实现
仅做学习使用,只是自己的一个小练习,有些地方的代码是针对例题设置
书籍封面:
题目:(书籍的P111-P114页内容)
以下是代码(配有注释):
#include <iostream>
#include<string.h>
using namespace std;
#define SourseNum 3
#define N 5
#define M 3
#define A 0
#define B 1
#define C 2
int Available[SourseNum]= {3,3,2};
int Max[N][M]={ {7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3} };
int Allocation[N][M]={ {0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2} };
int Need[N][M]={ {7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1} };
int Request[N][SourseNum];
int Work[SourseNum];
bool Finish[N];
void initial();
bool banker_suanfa(int name);
bool is_safe();
void banker_back_suanfa(int name);
void output();
bool banker_suanfa(int name){
if(Request[name][A]>Need[name][A]) return false;
if(Request[name][B]>Need[name][B]) return false;
if(Request[name][C]>Need[name][C]) return false;
if(Request[name][A]>Available[A]) return false;
if(Request[name][B]>Available[B]) return false;
if(Request[name][C]>Available[C]) return false;
for(int i =0 ;i<SourseNum;i++){
Available[i]-=Request[name][i];
Allocation[name][i] += Request[name][i];
Need[name][i]-=Request[name][i];
}
return true;
}
bool is_safe(){
for(int k =0;k<N;k++){
Finish[k]=false;
}
memcpy(Work,Available,sizeof(Available));
bool flag =true;
int i=0;
for(;i<N;){
if(Finish[i]==false && Need[i][A]<=Work[A] &&Need[i][B]<=Work[B] &&Need[i][C]<=Work[C]){
cout<<"此时的work ABC = \t"<<Work[A]<<"\t"<<Work[B]<<"\t"<<Work[C]<<endl;
Work[A]+=Allocation[i][A];
Work[B]+=Allocation[i][B];
Work[C]+=Allocation[i][C];
Finish[i]=true;
cout<<"安全性算法中:目前找到的进程:进程"<<i<<endl;
cout<<"此时的work+Allocation ABC = \t"<<Work[A]<<"\t"<<Work[B]<<"\t"<<Work[C]<<endl;
cout<<"------------------------------"<<endl;
i=0;
}
else{
i++;
}
}
for(int j=0;j<N;j++){
if(Finish[j]==false){
flag=false;
break;
}
}
if(flag==false){
return false;
}else{
return true;
}
}
void banker_back_suanfa(int name){
for(int i =0 ;i<SourseNum;i++){
Available[i]+=Request[name][i];
Allocation[name][i] -= Request[name][i];
Need[name][i]+=Request[name][i];
}
}
void output(){
for(int i =0;i<N;i++){
cout<<"进程P"<<i<<"的Allocation ABC:"<<Allocation[i][A]<<"\t"<<Allocation[i][B]<<"\t"<<Allocation[i][C]<<endl;
}
cout<<"-------------------------------------------------------"<<endl;
for(int i =0;i<N;i++){
cout<<"进程P"<<i<<"的Need ABC:"<<Need[i][A]<<"\t"<<Need[i][B]<<"\t"<<Need[i][C]<<endl;
}
cout<<"-------------------------------------------------------"<<endl;
cout<<"Available ABC分别为:"<<Available[A]<<"\t"<<Available[B]<<"\t"<<Available[C]<<endl;
cout<<"-------------------------------------------------------"<<endl;
cout<<"-------------------------------------------------------"<<endl;
cout<<"-------------------------------------------------------"<<endl;
}
bool is_End(){
for(int i =0;i<SourseNum;i++)
{
if(Available[i]>0){
return false;
}
}
return true;
}
int main()
{
int name;
int Sourse_A;
int Sourse_B;
int Sourse_C;
cout<<"程序还未开始时各资源情况如下:"<<endl;
output();
cout<<"提示:若进程申请资源成功,且是安全状态程序继续运行,若申请失败则需要重新申请"<<endl;
while(1){
if(is_End()) break;
cout<<"哪个进程要申请空间?(进程0——4)"<<endl;
cin>>name;
cout<<"\n 分别申请ABC多少资源数(数字之间搁一个空格)?\n";
cin>>Sourse_A>>Sourse_B>>Sourse_C;
Request[name][A]=Sourse_A;
Request[name][B]=Sourse_B;
Request[name][C]=Sourse_C;
if(banker_suanfa(name)){
if(is_safe()){
cout<<"进程"<<name<<"申请资源成功,现在情况如下:"<<endl;
output();
cout<<"可以继续申请资源"<<endl;
continue;
}
else {
banker_back_suanfa(name);
cout<<"资源申请不合理!申请资源之前的状态如下:"<<endl;
output();
cout<<"请重新申请资源!"<<endl;
continue;
}
}
else{
cout<<"输入的申请资源数不合理,请重新输入!现在情况如下:"<<endl;
output();
continue;
}
}
cout<<"\n程序结束~!"<<endl;
return 0;
}
以上代码能够运行,初步判断结果正确 下面是书上的例子,有兴趣的话读者可以把代码拷贝到本地运行,输入测试样例试一下。
over~
|