银行家算法的C语言实现

近日在学习操作系统,发现国外的教材远比国内的教材更加容易理解,有时感觉操作系统是一个不折不扣的坑,书中的每一句话,其实都需要足够长的代码来描述它。

尤其是在拜读了川合秀实的三十天自制操作系统后,感觉自己对操作系统的理解不再那么抽象化了,而开始变得有血有肉起来了。

银行家算法是在系统资源有限的情况下,避免进程出现死锁的一种解决办法。

示例代码如下:

#include<stdio.h>
#include<stdlib.h>

#define TRUE 1
#define FALSE 0

int i,j,g,k;
int allocation[100][100],max[100][100],available[100];
int need[100][100];
int n_process,request[100][100],work[100],total[100];
int finish[100],temp=0;
int flag,finish_flag,safe_flag,complete_flag;
int response;

void initialize_process_resource_type();
void initialize_max();
void initialize_allocation();
void calculate_need();
void calculate_available();
void resource_request();
void safe_state_check();
void print_all();
void check_complete();
void run_time_allocation();
void release_resources();

void main()
{
 initialize_process_resource_type();
 initialize_max();
 initialize_allocation();
 calculate_need();
 calculate_available();
 print_all();

 flag=FALSE;
 while(flag==FALSE)
 {
  if(complete_flag==TRUE)
  {
   printf(“\n\n All Processes has Completed”);
   printf(“Simulated Execution \n”);
   flag=TRUE;
  }
  release_resources();
 // system(“tput clear”);
 // system(“tput clear”);
  print_all();
  printf(“\n Enter the Run-Time Allocation…..”);
  run_time_allocation();
  safe_state_check();
  if(safe_flag==TRUE)
  {
   printf(“\n Granting Request…..”);
   resource_request();
   print_all();
  }

  else
  {
   printf(“\n Request is not Granted !!”);
   printf(“This request leads to the”);
   printf(“Unsafe State……..”);
  }
  printf(“\nTry another Allocation?[y=1/n=0]:”);
  scanf(“%d”,&response);
  flag=FALSE;
  if(response==0) flag=TRUE;
 }
 printf(“\n\n……Simulation of Banker’s”);
 printf(“Algorithm is Completed Successfully.\n”);
}

void initialize_process_resource_type()
{
 printf(“\nEnter the number of Processer:”);
 scanf(“%d”,&i);
 printf(“\nEnter the number of Types of Resources:”);
 scanf(“%d”,&j);
 for(k=1;k<=j;k++)
 {
  printf(“\nEnter the Total Resources of type %d:”,k);
  scanf(“%d”,&total[k]);
 }
}

void initialize_max()
{
 for(g=1;g<=i;g++)
 {
  for(k=1;k<=j;k++)
  {
   printf(“\nEnter Max-Need of resource of”);
   printf(“type %d for Process %d:”,k,g);
   scanf(“%d”,&max[g][k]);
  }
 }
}

void initialize_allocation()
{
 for(g=1;g<=i;g++)
 {
  for(k=1;k<=j;k++)
  {
   printf(“\nEnter Allocation of resource of”);
   printf(“type %d for Process %d:”,k,g);
   flag=FALSE;
   while(flag==FALSE)
   {
    scanf(“%d”,&allocation[g][k]);
    flag=TRUE;
    if(allocation[g][k]>max[g][k])
    {
     flag=FALSE;
     printf(“\nInvalid Allocation !!”);
     printf(“\nReEnter Allocation of Resource”);
     printf(“of type %d for Process %d:”,k,g);
    }
   }
  }
 }
}

void calculate_need()
{
 for(g=1;g<=i;g++)
 {
  for(k=1;k<=j;k++)
  {
   need[g][k]=max[g][k]-allocation[g][k];
  }
 }
}

void calculate_available()
{
 for(k=1;k<=j;k++)
 {
  temp=0;
  for(g=1;g<=i;g++)
   temp+=allocation[g][k];

  available[k]=total[k]-temp;
 }
}

void resource_request()
{
 for(k=1;k<=j;k++)
 {
  if(request[n_process][k]<=need[n_process][k])
  {
   if(request[n_process][k]<=available[k])
   {
    available[k]-=request[n_process][k];
    allocation[n_process][k]+=request[n_process][k];
    need[n_process][k]-=request[n_process][k];
   }
   else
    printf(“\n ERROR!Availability is greater than request..”);
  }
    printf(“\nError!! Request is greater than need…”);
 }
}

void safe_state_check()
{
 for(k=1;k<=i;k++)
  work[k]=available[k];

 for(g=1;g<=i;g++)
  finish[g]=FALSE;

 for(g=1;g<=i;g++)
  for(k=1;k<=j;k++)
  {
   if(finish[g]==FALSE&&need[g][k]<=work[k])
   {
    work[k]+=allocation[g][k];
    finish[g]=TRUE;
   }
  }

    complete_flag=TRUE;
 for(g=1;g<=i;g++)
  if(finish[g]==FALSE)
   complete_flag=FALSE;

  safe_flag=FALSE;
  if(finish[n_process]==TRUE)
   safe_flag=TRUE;
}

void print_all()
{
 printf(“\nProcess and Resource State……….”);
 printf(“\nProcess   Allocation    Max    Need “);
 for(g=1;g<=i;g++)
 {
  printf(“\n%d  “,g);
  for(k=1;k<=j;k++)
   printf(“%d”,allocation[g][k]);
  printf(”       “);
  for(k=1;k<=j;k++)
      printf(“%d”,max[g][k]);
  printf(”       “);
  for(k=1;k<=j;k++)
   printf(“%d”,need[g][k]);
 }
 printf(“\nTotal Resources:”);
 for(k=1;k<=j;k++)
  printf(“%d”,total[k]);

 printf(“\nAvailable:”);
 for(k=1;k<=j;k++)
  printf(“%d”,available[k]);

 printf(“\n”);
}

void check_complete()
{
 complete_flag=FALSE;
}

void run_time_allocation()
{
 printf(“\nEnter the Process Number(1 to %d):”,i);
 flag=FALSE;
 while(flag==FALSE)
 {
  flag=TRUE;
  scanf(“%d”,&n_process);
  if(n_process<1||n_process>i)
  {
   printf(“\nReEnter the Process Number(1 to %d):”,i);
   flag=FALSE;
  }
 }
 g=n_process;
 for(k=1;k<=j;k++)
 {
  printf(“\nEnter Resource of type”);
  printf(“%d allocation for process %d:”,k,g);
  scanf(“%d”,&request[g][k]);
 }
}

void release_resources()
{
 for(g=1;g<=i;g++)
 {
  for(k=1;k<=j;k++)
  {
   if((allocation[g][k]==max[g][k])&&(allocation[g][k]!=0))
   {
    printf(“\nThe Process %d is ready to deallocate”,g);
    printf(“Resource type %d by %dnumbers”,k,max[g][k]);
    printf(“Release[y=1/n=0]:”);
    scanf(“%d”,&response);
    if(response==1)
    {
     available[k]+=allocation[g][k];
     allocation[g][k]=0;
     max[g][k]=0;
    }
   }
  }
 }
}



    原文作者:银行家问题
    原文地址: https://blog.csdn.net/u011587666/article/details/44310777
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞