Advertisement
JavierGumin

Bankers Algorithm

Jun 4th, 2025
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.31 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.   int process, resource, i, j, instance, k=0, count=0, temp=0;
  6.   printf("\n Enter No. of Process : ");
  7.   scanf("%d", &process);
  8.   printf(" Enter No. of Resources : ");
  9.   scanf("%d",&resource);
  10.   int available[resource], max[process][resource], allocated[process][resource], need[process][resource], completed[process];
  11.  
  12.   for(i=0; i<process; i++)
  13.     completed[i] = 0;
  14.  
  15.   printf("\n Enter No. of AVAILABLE Instances for each resource\n\n");
  16.   for(i=0; i<resource;i++)
  17.   {  
  18.     printf(" Resources[%d] : ", i);
  19.     scanf("%d", &instance);
  20.     available[i] = instance;
  21.   }
  22.  
  23.   printf("\n------------------------------------------------------------------------------\n");
  24.   printf("\n Enter MAXIMUM instance for a Process & its corresponding resource :\n");
  25.   for(i=0; i<process; i++)
  26.   {
  27.     printf("\n For Process[%d] \n", i);
  28.     for(j=0; j<resource; j++)
  29.     {
  30.         printf(" Resource[%d] : ", j);
  31.         scanf("%d", &instance);
  32.         max[i][j] = instance;
  33.     }
  34.   }
  35.  
  36.   printf("\n------------------------------------------------------------------------------\n");
  37.   printf("\n Enter instance ALLOCATED for a Process & its corresponding resource :\n");
  38.   for(i=0; i<process; i++)
  39.   {
  40.     printf("\n For Process[%d] \n", i);
  41.     for(j=0; j<resource; j++)
  42.     {
  43.       printf(" Resource[%d] : ", i);
  44.       scanf("%d", &instance);
  45.       allocated[i][j] = instance;
  46.       need[i][j] = max[i][j] - allocated[i][j];
  47.     }
  48.   }
  49.  
  50.   printf("\n------------------------------------------------------------------------------\n");
  51.   printf("\nSafe Sequence is : ");
  52.   while(count != process)
  53.   {
  54.     count = temp;
  55.     for(i=0; i<process; i++)
  56.     {
  57.       for(j=0; j<resource; j++)
  58.       {
  59.         if(need[i][j] <= available[j])
  60.         {
  61.           k++;
  62.         }
  63.       }
  64.       if(k == resource && completed[i] == 0 )
  65.       {
  66.         printf(" > P[%d] ", i);
  67.         completed[i] = 1;
  68.         for(j=0; j<resource; j++)
  69.         {
  70.           available[j] = available[j] + allocated[i][j];
  71.         }      
  72.         count++;
  73.       }
  74.       k=0;
  75.     }
  76.  
  77.     if(count == temp)
  78.     {
  79.       break;
  80.     }
  81.   }
  82.  
  83.   for(i=0; i<process; i++)
  84.   {
  85.     if(completed[i] != 1)
  86.     {
  87.       printf("\n\n P[%d] not able to allocate", i);
  88.     }
  89.   }
  90.   printf("\n");
  91.  
  92.   return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement