Advertisement
Sri27119

banker

Nov 25th, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #define MAX 5
  3.  
  4. int available[MAX], max[MAX][MAX], allocation[MAX][MAX], need[MAX][MAX], n, m;
  5.  
  6. void calculateNeed()
  7. {
  8.     for (int i = 0; i < n; i++)
  9.         for (int j = 0; j < m; j++)
  10.             need[i][j] = max[i][j] - allocation[i][j];
  11. }
  12.  
  13. int checkSafeState()
  14. {
  15.     int work[MAX], finish[MAX] = {0}, safeSequence[MAX], count = 0;
  16.     for (int i = 0; i < m; i++)
  17.         work[i] = available[i];
  18.  
  19.     while (count < n)
  20.     {
  21.         int found = 0;
  22.         for (int i = 0; i < n; i++)
  23.         {
  24.             if (finish[i] == 0)
  25.             {
  26.                 int j;
  27.                 for (j = 0; j < m; j++)
  28.                     if (need[i][j] > work[j])
  29.                         break;
  30.  
  31.                 if (j == m)
  32.                 {
  33.                     for (int k = 0; k < m; k++)
  34.                         work[k] += allocation[i][k];
  35.                     safeSequence[count++] = i;
  36.                     finish[i] = 1;
  37.                     found = 1;
  38.                 }
  39.             }
  40.         }
  41.  
  42.         if (found == 0)
  43.         {
  44.             printf("\nSystem is not in a safe state.\n");
  45.             return 0;
  46.         }
  47.     }
  48.  
  49.     printf("\nSystem is in a safe state.\nSafe sequence is: ");
  50.     for (int i = 0; i < n; i++)
  51.     {
  52.         printf("%d ", safeSequence[i]);
  53.     }
  54.     printf("\n");
  55.     return 1;
  56. }
  57.  
  58. int main()
  59. {
  60.     printf("Enter the number of processes: ");
  61.     scanf("%d", &n);
  62.     printf("Enter the number of resources: ");
  63.     scanf("%d", &m);
  64.  
  65.     printf("\nEnter available resources: \n");
  66.     for (int i = 0; i < m; i++)
  67.     {
  68.         printf("Resource %d: ", i + 1);
  69.         scanf("%d", &available[i]);
  70.     }
  71.  
  72.     printf("\nEnter maximum resources for each process:\n");
  73.     for (int i = 0; i < n; i++)
  74.     {
  75.         printf("Enter maximum resources for process %d: ", i + 1);
  76.         for (int j = 0; j < m; j++)
  77.         {
  78.  
  79.             scanf("%d", &max[i][j]);
  80.         }
  81.     }
  82.  
  83.     printf("\nEnter allocated resources for each process:\n");
  84.     for (int i = 0; i < n; i++)
  85.     {
  86.         printf("Enter allocated resources for process %d: ", i + 1);
  87.         for (int j = 0; j < m; j++)
  88.         {
  89.             scanf("%d", &allocation[i][j]);
  90.         }
  91.     }
  92.     calculateNeed();
  93.     checkSafeState();
  94.  
  95.     return 0;
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement