manthanthakker40

OS -bankers algorithm

Sep 24th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. int main()
  2. {
  3. int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];
  4. int p, r, i, j, process, count;
  5. count = 0;
  6.  clrscr();
  7. printf("Enter the no of processes : ");
  8. scanf("%d", &p);
  9.  
  10. for(i = 0; i< p; i++)
  11.     completed[i] = 0;
  12.  
  13. printf("\n\nEnter the no of resources : ");
  14. scanf("%d", &r);
  15.  
  16. printf("\n\nEnter the Max Matrix for each process : ");
  17. for(i = 0; i < p; i++)
  18. {
  19.     printf("\nFor process %d : ", i + 1);
  20.     for(j = 0; j < r; j++)
  21.         scanf("%d", &Max[i][j]);
  22. }
  23.  
  24. printf("\n\nEnter the allocation for each process : ");
  25. for(i = 0; i < p; i++)
  26. {
  27.     printf("\nFor process %d : ",i + 1);
  28.     for(j = 0; j < r; j++)
  29.         scanf("%d", &alloc[i][j]);  
  30. }
  31.  
  32. printf("\n\nEnter the Available Resources : ");
  33. for(i = 0; i < r; i++)
  34.         scanf("%d", &avail[i]);
  35.  
  36.  
  37.     for(i = 0; i < p; i++)
  38.         for(j = 0; j < r; j++)
  39.             need[i][j] = Max[i][j] - alloc[i][j];
  40.          
  41. do
  42. {
  43.     printf("\n Max matrix:\tAllocation matrix:\n");
  44.     for(i = 0; i < p; i++)
  45.     {
  46.         for( j = 0; j < r; j++)
  47.             printf("%d  ", Max[i][j]);
  48.         printf("\t\t");
  49.         for( j = 0; j < r; j++)
  50.             printf("%d  ", alloc[i][j]);
  51.         printf("\n");
  52.     }
  53.  
  54.     process = -1;
  55.  
  56.     for(i = 0; i < p; i++)
  57.     {
  58.         if(completed[i] == 0)//if not completed
  59.         {
  60.             process = i ;
  61.             for(j = 0; j < r; j++)
  62.             {
  63.                 if(avail[j] < need[i][j])
  64.                 {
  65.                     process = -1;
  66.                     break;
  67.                 }
  68.             }
  69.         }
  70.         if(process != -1)
  71.             break;
  72.     }
  73.  
  74.     if(process != -1)
  75.     {
  76.         printf("\nProcess %d runs to completion!", process + 1);
  77.         safeSequence[count] = process + 1;
  78.         count++;
  79.         for(j = 0; j < r; j++)
  80.         {
  81.             avail[j] += alloc[process][j];
  82.             alloc[process][j] = 0;
  83.             Max[process][j] = 0;
  84.             completed[process] = 1;
  85.         }
  86.     }
  87. }while(count != p && process != -1);
  88.  
  89. if(count == p)
  90. {
  91.     printf("\nThe system is in a safe state!!\n");
  92.     printf("Safe Sequence : < ");
  93.     for( i = 0; i < p; i++)
  94.             printf("%d  ", safeSequence[i]);
  95.     printf(">\n");
  96. }
  97. else
  98.     printf("\nThe system is in an unsafe state!!");
  99. getch();
  100. return 0;
  101. }
Add Comment
Please, Sign In to add comment