⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qinling.c

📁 操作系统试验银行家算法
💻 C
字号:

#include "stdio.h"
#include "conio.h"
 int request[3],work[3],need[5][3],workalloc[5][3];
 int max[5][3]/*={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}}*/;
 int allocation[5][3]/*={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}}*/;
 int available[3]/* = {3,3,2}*/;
 int result[5] = {-1,-1,-1,-1,-1};
 int num[5],p[5];
/*------------------------输入数据--------------------*/
int input(){
 int p;
 printf("\nInput Available:\n");
  scanf("%d,%d,%d",&available[0],&available[1],&available[2]);

  printf("\nInput Max:\n");
 for(p=0;p<5;p++)
  {
   printf("[P%d]:",p);
   scanf("%d,%d,%d",&max[p][0],&max[p][1],&max[p][2]);
   }

 printf("\nInput Allocation:\n");
 for(p=0;p<5;p++)
  {
   printf("[P%d]:",p);
   scanf("%d,%d,%d",&allocation[p][0],&allocation[p][1],&allocation[p][2]);
  }
}
/*-----------------------输出表格3-15 -----------------*/
int output(){
 int p,s;
 printf("Max source{A,B,C}={10,5,7};\n");
 printf("Available{A,B,C}={%d,%d,%d};\n",available[0],available[1],available[2]);
 printf("----------------------------------------------\n");
 printf("       Max     |  Allocation |    Need\n");
 printf("     A  B  C   |   A  B  C   |   A  B  C\n");
 for(p=0;p<5;p++){
  printf("P%d: ",p);
  for(s=0;s<3;s++){
   printf(" %d ",max[p][s]);
   need[p][s]=max[p][s]-allocation[p][s];
  }
  printf("  |  ");
  for(s=0;s<3;s++)
  printf(" %d ",allocation[p][s]);
  printf("  |  ");
  for(s=0;s<3;s++)
  printf(" %d ",need[p][s]);
  printf("\n");
 }
 printf("----------------------------------------------\n");
}
/*-----------------------输出表格3-16 -----------------*/
int output2(){
 int p,s;
 printf("==============================================================================\n");
 printf("        Work      |       Need     |    Allocation  | Work+Allocatio | Finish\n");
 printf("     A    B   C   |    A   B   C   |    A   B   C   |   A    B    C  |  \n");
 printf("------------------------------------------------------------------------------\n");
 for(p=0;p<5;p++){
  printf("P%d: ",num[p]);
  for(s=0;s<3;s++){
   printf(" %2d ",workalloc[num[p]][s]-allocation[num[p]][s]);
  }
   printf("  |  ");
  for(s=0;s<3;s++)
   printf(" %2d ",need[num[p]][s]);
   printf("  |  ");
  for(s=0;s<3;s++)
   printf(" %2d ",allocation[num[p]][s]);
   printf("  |  ");
  for(s=0;s<3;s++){
   printf(" %2d ",workalloc[num[p]][s]);
  }
   printf("  |  ");
   printf("true");
   printf("\n");
 }
  printf("-------------------------------------------------------------------------------\n");
}

/*---------------初始化work[],need[]-------------------*/
int init(){
 int i,j;
  for(i=0;i<3;i++){
    work[i]=available[i];
   }

  for(i=0;i<5;i++){
    for(j=0;j<3;j++){
        need[i][j]=max[i][j]-allocation[i][j];
        }
   }
}

/*------------------主函数-----------------------*/
main()
{
 int i,j=0,k,p[5],count=0,s,flag=1;
 int request[3],finish[3];
 input();
 init();
 output();

 while(!0){
 printf("Please input which process call a Request(N<5):\n");
 scanf("%d",&k);


 if(k>=5){
   printf("This process does not exist!\n");
   printf("Please input again:\n");
   continue;
   }

 if(k<0) break;

 printf("\n-----------------------------------------------------\n");
 printf("Input a Request\n");
 printf("              A,B,C\n ");
 printf("Request[P%d]: ",k);
 scanf("%d,%d,%d",&request[0],&request[1],&request[2]);

 printf("\n");

  if(request[0]>need[k][0] || request[1]>need[k][1] || request[2]>need[k][2]){
     printf("The request[P%d] is out of Need!\n\n",k);
    }

  else if(request[0]>available[0]||request[1]>available[1]||request[2]>available[2]){
    printf("The request[P%d] is out of available!\n",k);
    printf("P[%d] should wait!\n\n",k);
    }
/*----------------------------银行家算法---------------------------*/
  else{
   for(i=0;i<3;i++){
    available[i]-=request[i];
    allocation[k][i]+=request[i];
    need[k][i]-=request[i]; }

   for(i=0;i<3;i++){
    work[i]=available[i];
   }
   output();
/*------------------------------安全性算法-------------------------*/
   for(i=0;i<5;i++){
     result[i]=-1;}

   count=0;
   for(s=0;s<5;s++)
   for(i=0;i<5;i++){
    if(result[i]==-1&&need[i][0]<=work[0]&&need[i][1]<=work[1]&&need[i][2]<=work[2]){
     work[0]=work[0]+allocation[i][0];
     work[1]=work[1]+allocation[i][1];
     work[2]=work[2]+allocation[i][2];
     count++;
     workalloc[i][0]=work[0];
     workalloc[i][1]=work[1];
     workalloc[i][2]=work[2];
     result[i]=s;
     num[j]=i;
     j++;
     printf("P[%d]-->",i);
    }
   }

   if(count==5){
       printf("It is safe!\n");
       output2();
       count=0;
       }
   else{
       printf("It is not safe!\n");
       printf("P[%d] shoud wait!\n\n",k);
/*-------------------------------数据恢复-----------------------*/
       for(i=0;i<3;i++){
           available[i]+=request[i];
           allocation[k][i]-=request[i];
           need[k][i]+=request[i]; }
      }
  }

 }

  getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -