#include<iostream.h>
#define MaxP 10
#define MaxR 10
/*---判断指定进程执行某此请求时,是否安全!---*/
int issafe(int Request[],int Avai[],int Max[][MaxR],int Need[][MaxR],int Allo[][MaxR],int r,int n);
/*---判断指定的进程是否完成!---*/
int isfinish(int Allo[][MaxR],int Max[][MaxR],int Finish[],int n,int r);
/*---对指定完成的进程进行内存回收!---*/
void cycle(int Avai[],int Allo[][MaxR],int n,int r);
void main()
{
int Avai[MaxR]; //System total Resource
int Max[MaxP][MaxR]; //all Process of all kinds of Resource
int Allo[MaxP][MaxR]; //all Process has get Resource current
int Need[MaxP][MaxR]; //all Process still need Resource
int Finish[MaxP]; //all Process wherther finish
int Request[MaxR]; //current Request Resource number
int i,j,k=1; //temporary value
int p; //Process number
int r; //Resource number
int n; //Request Process number
cout<<"input How many Process(small "<<MaxP<<"):";
cin>>p;
cout<<"input How many Resource(small "<<MaxR<<"):";
cin>>r;
cout<<"input initat number all kinds of Resource:"<<endl;
for(i=0;i<r;i++){
cin>>Avai[i];
}
cout<<"input number all kinds of MaxResource of Process"<<endl;
for(i=0;i<p;i++){
for(j=0;j<r;j++){
cin>>Max[i][j];
Need[i][j]=Max[i][j]; //对进程资源进行初始化
Allo[i][j]=0;
}
Finish[i]=0;
}
while(k!=0){
cout<<"*****银行家算法测试系统*****"<<endl;
cout<<"*****1.作业请求资源!********"<<endl;
cout<<"*****2.查看作业当前资源情况!"<<endl;
cout<<"*****3.执行当前可以完成的作业!"<<endl;
cout<<"*****0.退出!****************"<<endl;
cout<<"/t choic(0~2):";
cin>>k;
switch(k){
case 0:
break;
case 1:
cout<<"input the Process of number which request the Resource(samll "<<p<<"):"<<endl;
cin>>n;
while(isfinish(Allo,Max,Finish,n,r)){ //直到输入没有完成的进程为止
cout<<"作业已经完成,请重新输入!"<<endl;
cin>>n;
}
cout<<"input this Process of all kinds of Resource number:"<<endl;
for(i=0;i<r;i++){
cin>>Request[i];
}
if(issafe(Request,Avai,Max,Need,Allo,r,n)){ //判断指定的进程的请求是否安全,从而决定是否接受请求
cout<<"accept the Request!"<<endl;
Finish[n]=0;
if(isfinish(Allo,Max,Finish,n,r)){ //接受请求后判断是否完成,如果完成则进行内存回收
cycle(Avai,Allo,n,r);
}
}else{
cout<<"this Request is refuse,please Wait!"<<endl;
Finish[n]=2;
}
break;
case 2:
cout<<"当前资源分布状况!"<<endl;
cout<<"当前剩下的总资源有";
for(i=0;i<r;i++){ //按一定格式进行信息显示
cout<<Avai[i]<<" ";
}
cout<<endl;
cout<<"Max /t/t Need /t/t Allocation /t State"<<endl;
for(i=0;i<p;i++){
cout<<"(";
for(j=0;j<r;j++){
cout<<Max[i][j]<<",";
}
cout<<") /t";
cout<<"(";
for(j=0;j<r;j++){
cout<<Need[i][j]<<",";
}
cout<<") /t";
cout<<"(";
for(j=0;j<r;j++){
cout<<Allo[i][j]<<",";
}
cout<<")";
if(Finish[i]==1){
cout<<"/t Finish"<<endl;
}else if(Finish[i]=0){
cout<<"/t Running"<<endl;
}else if(Finish[i]==2){
cout<<"/t Wating"<<endl;
}
}
break;
case 3:
int flag=0; //找到定第一个可以执行的进程
for(i=0;i<p;i++){
if(Finish[i]==1){
continue;
}
for(j=0;j<r;j++){
if(Avai[j]<Need[i][j]){
break;
}
if(r==j+1){
flag=1;
break;
}
}
if(flag==1){ //如果找到,则执行,并进行一定的内存回收
Finish[i]=1;
cycle(Avai,Allo,i,r);
for(j=0;j<r;j++){
Need[i][j]=0;
Allo[i][j]=Max[i][j];
}
break;
}
}
break;
}
}
}
/*---判断指定进程执行某此请求时,是否安全!---*/
int issafe(int Request[],int Avai[],int Need[][MaxR],int Allo[][MaxR],int r,int n,int p){
int i,j,k,count=0,flag;
int Needn[MaxR];
int Avain[MaxR]; //记录当前可用资源的分布情况
int Avai_n[MaxR]; //保存预先分配的资源状况
int isFinish[MaxP]; //判断进程是否执行完成
for(i=0;i<r;i++){
if(Request[i]>Need[n][i] || Request[i]>Avai[i]){ //对请求进行合法性判断
return 0;
}
}
for(i=0;i<r;i++){ //对请求的资源分配先进行一些预处理
Needn[i]=Need[n][i]-Request[i];
Avain[i]=Avai[i]-Request[i];
Avai_n[i]=Avain[i];
}
for(k=0;k<p;k++){
isFinish[k]=0;
}
for(k=0;k<p;k++){ //进行安全性检测
for(j=0;j<p;j++){
flag=1; //判断该进程是否可以执行的标志
if(isFinish[j]!=0){ //判断进程是否执行完成
continue;
}
for(i=0;i<r;i++){ //对每个资源进行判断,是否可以执行
if(j!=n){ //由于刚申请的资源没有通过安全测试,所以还没有进行分配,必须作特殊处理
if(Need[j][i]>Avain[i]){
flag=0;
break;
}
}else{
if(Needn[i]>Avain[i]){
flag=0;
break;
}
}
}
if(flag==1){
for(i=0;i<r;i++){
if(j!=n){
Avain[i]+=Allo[j][i];
}else{
Avain[i]+=Request[i]+Allo[n][i]; //由于刚申请的资源没有通过安全测试,所以还没有进行分配,必须作特殊处理
}
}
count++;
isFinish[j]=count; //为了记录安全执行的顺序
break;
}
}
}
if(count!=p){ //对以上安全算法结果的最终判断
return 0;
}
for(i=0;i<p;i++){
cout<<isFinish[i]<<",";
}
cout<<endl;
for(i=0;i<r;i++){ //如果安全则进行资源分配
Allo[n][i]=Allo[n][i]+Request[i];
Need[n][i]=Needn[i];
Avai[i]=Avai_n[i];
}
return 1;
}
/*---判断指定的进程是否完成!---*/
int isfinish(int Allo[][MaxR],int Max[][MaxR],int Finish[],int n,int r){
int i;
for(i=0;i<r;i++){
if(Allo[n][i]!=Max[n][i]){
return 0;
}
}
Finish[n]=1;
return 1;
}
/*---对指定完成的进程进行内存回收!---*/
void cycle(int Avai[],int Allo[][MaxR],int n,int r){
int i;
for(i=0;i<r;i++){
Avai[i]=Avai[i]+Allo[n][i];
}
}