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

📄 1.c

📁 c.rar___z8786052200751110333362469.rar
💻 C
字号:
OS中的银行家算法(C语言)    最近写了一个银行家算法的实现,不过还有待完善,因为我这个算法只是一个进程申请资源后的安全性检测,还不能循环检测。如果有错,请浏览者指证或提出更好的方法。代码如下:
#include<stdio.h>
typedef struct
{
 int x;
 int requ[4];
}REQUEST;
void detect(int allocation[][4],int available[4],int need[][4]);
main()
{
 int i,j;
 int allocation[5][4]={{3,0,1,1},{0,1,0,0},{1,1,1,0},{1,1,0,1},{0,0,0,0}};
 int need[5][4]={{1,1,0,0},{0,1,1,2},{3,1,0,0},{0,0,1,0},{2,1,1,0}};
 int available[4]={1,0,2,0};
 REQUEST req;
 printf("please input the NO. of process: ");
 scanf("%d",&req.x);
 i=req.x;
 printf("please input the resouces of request:");
 scanf("%d,%d,%d,%d",&req.requ[0],&req.requ[1],&req.requ[2],&req.requ[3]);
 if(req.requ[0]<=need[i][0]
    &&req.requ[1]<=need[i][1]
    &&req.requ[2]<=need[i][2]
    &&req.requ[3]<=need[i][3])
 { if(req.requ[0]<=available[0]
  &&req.requ[1]<=available[1]
  &&req.requ[2]<=available[2]
  &&req.requ[3]<=available[3])
    {for(j=0;j<4;j++)
   { available[j]=available[j]-req.requ[j];
     allocation[i][j]=allocation[i][j]+req.requ[j];
     need[i][j]=need[i][j]-req.requ[j];
   }
 detect(allocation,available,need);
    }
    printf("there are not enough available resouces!");
    exit(1);
 }
 printf("The resouces of request have been beyond the max number needed!");
}
void detect(int allocation[][4],int available[4],int need[][4])
{
  int t,n,j,e;
  int work[4];
  int finish[5]={0,0,0,0,0};
  for(t=0;t<4;t++)
  work[t]=available[t];
  for(n=0;n<5;n++)
  { if(finish[n]==0
   &&need[n][0]<=work[0]
   &&need[n][1]<=work[1]
   &&need[n][2]<=work[2]
   &&need[n][3]<=work[3])
 {
   for(j=0;j<4;j++)
   work[j]=work[j]+allocation[n][j];
   finish[n]=1;
   n=-1;/*从0再次循环*/
 }
  }

  for(e=0;e<5;e++)
 {  if(finish[e]==1)
     ;
   else
    {for(e=0;e<5;e++)
     printf("%d",finish[e]);
     printf("the system is not safe!");
     exit(1);
  }
  }
   printf("the system is safe!");
   exit(1);
 }
 
 
 

⌨️ 快捷键说明

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