Advertisement
Guest User

Untitled

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