银行家算法

#include <string.h> #include <stdio.h> #include <iostream> #define FALSE 0 #define TRUE 1 #define W 10 #define R 10 using namespace std; int M ; // 总进程数 int N ; // 资源种类 int ALL_RESOURCE[W];// 各种资源的数目总和 int MAX[W][R]; // M个进程对N类资源最大资源需求量 int AVAILABLE[R]; // 系统可用资源数 int ALLOCATION[W][R]; // M个进程已经得到N类资源的资源量 int NEED[W][R]; // M个进程还需要N类资源的资源量 int Request[R]; // 请求资源个数 void output() { int i,j; cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl; cout<<"各种资源的总数量:"<<endl; for (j=0;j<N;j++) cout<<" 资源"<<j<<": "<<ALL_RESOURCE[j]; cout<<endl; cout<<"━━━━━━━━━━━━━━━━━━"<<endl; cout<<"目前各种资源可利用的数量为:"<<endl; for (j=0;j<N;j++) cout<<" 资源"<<j<<": "<<AVAILABLE[j]; cout<<endl; cout<<"━━━━━━━━━━━━━━━━━━"<<endl; cout<<"各进程还需要的资源数量:"<<endl<<endl; for(i=0;i<N;i++) cout<<" 资源"<<i; cout<<endl; for (i=0;i<M;i++) { cout<<"进程"<<i<<": "; for (j=0;j<N;j++) cout<<NEED[i][j]<<" "; cout<<endl; } cout<<endl; cout<<"━━━━━━━━━━━━━━━━━━"<<endl; cout<<"各进程已经得到的资源量: "<<endl<<endl; for(i=0;i<N;i++) cout<<" 资源"<<i; cout<<endl; for (i=0;i<M;i++) { cout<<"进程"<<i<<": "; for (j=0;j<N;j++) cout<<ALLOCATION[i][j]<<" "; cout<<endl; } cout<<endl; } void distribute(int k) { int j; for (j=0;j<N;j++) { AVAILABLE[j]=AVAILABLE[j]-Request[j]; ALLOCATION[k][j]=ALLOCATION[k][j]+Request[j]; NEED[k][j]=NEED[k][j]-Request[j]; } } void restore(int k) { int j; for (j=0;j<N;j++) { AVAILABLE[j]=AVAILABLE[j]+Request[j]; ALLOCATION[k][j]=ALLOCATION[k][j]-Request[j]; NEED[k][j]=NEED[k][j]+Request[j]; } } int check() { int WORK[R],FINISH[W]; int i,j; for(j=0;j<N;j++) WORK[j]=AVAILABLE[j]; for(i=0;i<M;i++) FINISH[i]=FALSE; for(i=0;i<M;i++) { for(j=0;j<N;j++) { if(FINISH[i]==FALSE&&NEED[i][j]<=WORK[j]) { WORK[j]=WORK[i]+ALLOCATION[i][j]; } } FINISH[i]=TRUE; } for(i=0;i<M;i++) { if(FINISH[i]==FALSE) { cout<<endl; cout<<" 系统不安全!!! 本次资源申请不成功!!!"<<endl; cout<<endl; return 1; } else { cout<<endl; cout<<" 经安全性检查,系统安全,本次分配成功。"<<endl; cout<<endl; return 0; } } } void bank() // 银行家算法 { int i=0,j=0; char flag='Y'; while(flag=='Y'||flag=='y') { i=-1; while(i<0||i>=M) { cout<<"━━━━━━━━━━━━━━━━━━"<<endl; cout<<endl<<" 请输入需申请资源的进程号:"; cin>>i; if(i<0||i>=M) cout<<" 输入的进程号不存在,重新输入!"<<endl; } cout<<" 请输入进程"<<i<<"申请各类资源的数量:"<<endl; for (j=0;j<N;j++) { cout<<" 资源"<<j<<": "; cin>>Request[j]; if(Request[j]>NEED[i][j]) // 若请求的资源数大于进程还需要i类资源的资源量j { cout<<endl<<" 进程"<<i<<"申请的资源数大于进程"<<i<<"还需要"<<j<<"类资源的数量!"; cout<<" 若继续执行系统将处于不安全状态!"<<endl; flag='N'; break; } else { if(Request[j]>AVAILABLE[j]) // 若请求的资源数大于可用资源数 { cout<<endl<<" 进程"<<i<<"申请的资源数大于系统可用"<<j<<"类资源的数量!"; cout<<" 若继续执行系统将处于不安全状态!"<<endl; flag='N'; break; } } } if(flag=='Y'||flag=='y') { distribute(i); // 调用change(i)函数,改变资源数 if(check()) // 若系统安全 { restore(i); // 调用restore(i)函数,恢复资源数 output(); // 输出资源分配情况 } else // 若系统不安全 output(); // 输出资源分配情况 } else // 若flag=N||flag=n cout<<endl; cout<<" 是否继续银行家算法演示,按'Y'或'y'键继续,按'N'或'n'键退出演示: "; cin>>flag; } } void version() { cout<<endl; cout<<"\t         银 行 家 算 法         "<<endl; } int main() // 主函数 { int i=0,j=0,p; version(); getchar(); cout<<endl<<"请输入总进程数:"; cin>>M; cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl; cout<<"请输入总资源种类:"; cin>>N; cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl; cout<<"请输入各类资源总数:(需要输入数为"<<N<<"个)"; for(i=0;i<N;i++) cin>>ALL_RESOURCE[i]; cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl; cout<<"输入各进程所需要的各类资源的最大数量:(需要输入数为"<<M*N<<"个)"; for (i=0;i<M;i++) { for (j=0;j<N;j++) { do { cin>>MAX[i][j]; if (MAX[i][j]>ALL_RESOURCE[j]) cout<<endl<<"占有资源超过了声明的该资源总数,请重新输入"<<endl; } while (MAX[i][j]>ALL_RESOURCE[j]); } } cout<<endl<<"━━━━━━━━━━━━━━━━━━"<<endl; cout<<"输入各进程已经占据的各类资源的数量:(需要输入数为"<<M*N<<"个)"; for (i=0;i<M;i++) { for (j=0;j<N;j++) { do { cin>>ALLOCATION[i][j]; if (ALLOCATION[i][j]>MAX[i][j]) cout<<endl<<"占有资源超过了声明的最大资源,请重新输入"<<endl; } while (ALLOCATION[i][j]>MAX[i][j]); } } for (j=0;j<N;j++) // 初始化资源数量 { p=ALL_RESOURCE[j]; for (i=0;i<M;i++) { p=p-ALLOCATION[i][j];// 减去已经被占据的资源 AVAILABLE[j]=p; if(AVAILABLE[j]<0) AVAILABLE[j]=0; } } for (i=0;i<M;i++) for(j=0;j<N;j++) NEED[i][j]=MAX[i][j]-ALLOCATION[i][j]; output(); bank(); return 0; } 

    原文作者:银行家问题
    原文地址: https://blog.csdn.net/zzj806683450/article/details/17415969
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞