操作系统之--银行家算法1

来源:互联网 发布:淘宝网上买白酒 编辑:程序博客网 时间:2024/05/29 17:42

 

#include<iomanip.h>   
    
  
#define   M   5                       //总进程数   
  
#define   N   3                       //总的资源种数   
  
#define   FALSE   0   
  
#define   TRUE   1   
  
//M个进程对N类资源最大资源需求量   
  int   MAX[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};   
    
  
//系统可用资源数   
  int   AVAILABLE[N]={3,3,2};     
    
  
//M个进程已经得到N类资源的资源量   
  int   ALLOCATION[M][N]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};   
    
  
//M个进程还需要N类资源的资源量   
  int   NEED[M][N]={{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};   
    
  
int   Request[N]={0,0,0};   
    
  
void   main()   
  
{   
  
int   i=0,j=0;   
  
char   flag='Y';   
  
void   showdata();   
  
void   changedata(int);   
  
void   restoredata(int);   
  
int   checkerror(int);   
  showdata();   
    
  
while(flag=='Y'||flag=='y')   
  
{   
  i
=-1;   
  
while(i<0||i>=M)   
  
{   
  cout
<<"     请输入需申请资源的进程号从0到"<<M-1<<":,否则重输入!";   
    
  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])   
  
{   
  cout
<<"     进程"<<i<<"申请的资源数大于进程"<<i<<"还需要"<<j<<"类资源的资源量!";   
    
  cout
<<"申请不合理,出错!请重新选择!"<<endl<<endl;   
  flag
='N';   
  
break;   
  }
   
  
else   
  
{   
  
if(Request[j]>AVAILABLE[j])   
  
{   
  cout
<<"     进程"<<i<<"申请的资源数大于系统可用"<<j<<"类资源的资源量!";   
    
  cout
<<"申请不合理,出错!请重新选择!"<<endl<<endl;   
  flag
='N';   
  
break;   
  }
   
  }
   
  }
   
  
if(flag=='Y'||flag=='y')   
  
{   
  changedata(i);   
  
if(checkerror(i))   
  
{   
  restoredata(i);   
  showdata();   
  }
   
  
else   
  showdata();   
  }
   
  
else   
    
  showdata();   
    
  cout
<<endl;   
  cout
<<"     是否继续银行家算法演示,按'Y'或'y'键继续,按'N'或'n'键退出演示:   ";   
    
  cin
>>flag;   
  }
   
  }
   
    
  
void   showdata()   
  
{   
  
int   i,j;   
          cout
<<"                 系统可用的资源数为:"<<endl<<endl;   
  cout
<<"             ";   
  
for   (j=0;j<N;j++)       cout<<"     资源"<<j<<":     "<<AVAILABLE[j];   
          cout
<<endl;   
    
          cout
<<endl;   
  cout
<<"                 各进程还需要的资源量:"<<endl<<endl;   
  
for   (i=0;i<M;i++)   
  
{   
  cout
<<"进程"<<i<<":";   
  
for   (j=0;j<N;j++)cout<<"     资源"<<j<<":     "<<NEED[i][j];   
                  cout
<<endl;   
  }
   
          cout
<<endl;   
  cout
<<"                 各进程已经得到的资源量:     "<<endl<<endl;   
  
for   (i=0;i<M;i++)   
  
{   
  cout
<<"进程"<<i<<":";   
  
for   (j=0;j<N;j++)cout<<"     资源"<<j<<":     "<<ALLOCATION[i][j];   
                  cout
<<endl;   
  }
   
          cout
<<endl;   
  }
   
    
  
void   changedata(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   restoredata(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   checkerror(int   s)   
  
{   
  
int   WORK[M],FINISH[M],temp[M];   
    
  
int   i,j,k=0;   
    
  
for(i=0;i<M;i++)   
  
{   
  FINISH[i]
=FALSE;   
  }
   
    
    
  
for(j=0;j<N;j++)   
  
{   
  WORK[j]
=AVAILABLE[j];   
  i
=s;   
  
while(i<M)   
  
{     
  
if   (FINISH[i]==FALSE&&NEED[i][j]<=WORK[j])   
  
{   
      WORK[j]
=WORK[j]+ALLOCATION[i][j];   
      FINISH[i]
=TRUE;   
      temp[k]
=i;   
      
++k;   
      i
=0;   
  }
   
          
else   
  
{   
      
++i;   
  }
   
    
  }
   
  
for(i=0;i<M;i++)   
  
{   
  
if(FINISH[i]==FALSE)   
  
{   
  cout
<<endl;   
  cout
<<"     系统不安全!!!   本次资源申请不成功!!!"<<endl;   
  cout
<<endl;   
  
return   1;   
  }
   
  
else   
  cout
<<"进程"<<temp[i]<<"->";   
  }
   
    
    
  }
   
                  cout
<<"     经安全性检查,系统安全,本次分配成功。"<<endl;   
  cout
<<"     安全序列为:   "<<endl;   
  
for(i=0;i<M;++i)   
  
{   
  cout
<<setw(5)<<temp[i];   
  }
   
    
  cout
<<endl;   
    
  
return   0;   
  }
   
原创粉丝点击