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

📄 exp4.c

📁 linux编程 操作系统试验报告
💻 C
字号:
#include<stdio.h>
//模拟实现
int main()
{ 
  int NResourse=3;
  int Available[NResourse],MAX[5][NResourse],Allocation[5][NResourse];
  int Need[5][NResourse],Work[NResourse],Finish[5];
  int i,j,k=0,s,t,P[5];

  printf("Please input the following numbers:\n");

  for(i=0;i<=4;i++)//初始化
  {
        for(j=0;j<NResourse;j++)
        {
           printf("MAX[%d,%d]=",i,j);
           scanf("%d",&MAX[i][j]);
           printf("Allocation[%d,%d]=",i,j);
           scanf("%d",&Allocation[i][j]);
           Need[i][j]=MAX[i][j]-Allocation[i][j];
        }
  }

  for(i=0;i<NResourse;i++)//初始化
  {
        printf("Available[%d]=",i);
        scanf("%d",&Available[i]);
        Work[i]=Available[i];
  }

  for(i=0;i<=4;i++)//初始化
        Finish[i]=0;

  for(t=0;t<=4;t++)//书上P96算法
  {
        for(i=0;i<=4;i++)
        {
           s=0;
           if(Finish[i]==0)
           {
                for(j=0;j<NResourse;j++)
                  if(Need[i][j]<=Work[j])
                  {
                     Work[j]=Work[j]+Allocation[i][j];
                     s++;
                  }
                if(s==NResourse)
                   Finish[i]=1;
                if(Finish[i]==1)
                {
                   P[k]=i;
                   k++;
                }
           }
        }
  }

  if(k==5)//判断是否安全
  {
        printf("It is safe under the current condition!\n");
        printf("The safe process queue is:\n");
        printf("P(%d)->P(%d)->P(%d)->P(%d)->P(%d)\n",
                P[0],P[1],P[2],P[3],P[4]);
  }
  else
        printf("It is not safe under the current condition!\n");

exit(0);
}

⌨️ 快捷键说明

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