死锁避免-银行家算法 模拟

#include "string.h"
#include "iostream.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 changdata(int);
void rstordata(int);
int chkerr(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')
  {
   changdata(i);
   if(chkerr(0))
   {
    rstordata(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<<":  "<<MAX[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<<":  "<<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 changdata(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 rstordata(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 chkerr(int s)

{
         int WORK[N],FINISH[M],temp[M];
         int i,j,k=0,flag;

         for(i=0;i<M;i++)

                 FINISH[i]=FALSE;

         for(j=0;j<N;j++)
                 WORK[j]=AVAILABLE[j];
         i=s;
         while(i<M)
         {
                 flag=0;
                 for (j=0;j<N;j++)
                          if(NEED[i][j]>WORK[j])
                                       flag=1;
  
                 if (FINISH[i]==FALSE&&flag==0)

                 {

                          for(j=0;j<N;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;
                  }
          cout<<endl;
          cout<<"  经安全性检查,系统安全,本次分配成功。"<<endl;
          cout<<endl;
          cout<<"  本次安全序列:";
          for(i=0;i<M;i++)cout<<"进程"<<temp[i]<<"->";
                  cout<<endl<<endl;;
          return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值