www.pudn.com > BA.rar > ba.cpp
// //程序名称:银行家算法的实现 //程序作者:张焕人 //作者邮箱: renwairen369@yahoo.com.cn // renwairen369@hotmail.com //作者QQ:27949278 // // // #include#include #define M 5 //设置M个process,N个resource #define N 3 int available[N]={10,5,7}, //可用资源向量:代表一类可利用的资源数目 work[N]={3,2,2}, //工作向量: 表示系统可提供给进程继续运行所需要的各类资源数目 max[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}}, //最大需求矩阵:m个进程中的每一个对n类资源的最大需求 allocation[M][N]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}}, //分配矩阵:每一类资源当前已分配给每一进程的资源数 need[M][N], //需求矩阵:每一进程尚需的各类资源数 request[N], //请求向量:表示当前进程需要的各类资源数 which; int sp[M]; //用来记录安全队列的数组 void input() { int i,j; for(j=0;j >available[j]; } for(i=0;i >max[i][j]; } } for(i=0;i >allocation[i][j]; while(allocation[i][j]>max[i][j]) { cout<<"输入有误(allocation[i][j]>max[i][j])!"< >allocation[i][j]; } } } } bool compare(int *x,int *y) //比较一维数组函数, 要求x<=y为真 { int j; for(j=0;j y[j]) return false; } return true; } /*bool compare2(int *x,int *y) //比较一维数组和二维数组指定行函数 要求X<=Y为真 { int j; for(j=0;j y[j]) return false; } return true; } */ /* bool compareg(int *x,int *y) //比较一维数组和二维数组指定行函数 要求X>Y为真 { int j; for(j=0;j >ch; while(ch=='n') { input(); /*初始化*/ if(safecheck()==0) cout<<"初始数据会发生死锁! 请重新输入!"< >which; cout<<"请输入该进程的请求向量: "; for(i=0;i >request[i]; //输入某个进程的请求向量 if(compare(request,need[which])) //判断是否大于需要的最大值 { //printf("need pass\n"); if(compare(request,available)) //判断是否有足够的数目 request<=available { // printf("Available pass\n"); for(j=0;j =N) { for(j=0;j