Guest User

Untitled

a guest
Nov 21st, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int current[5][5], maximum_claim[5][5], available[5],need[50][50];
  4. int allocation[5] = {0, 0, 0, 0, 0};
  5. int maxres[5], running[5], safe = 0;
  6. int counter = 0, i, j, exec, resources, processes, k = 1;
  7.  
  8. int main()
  9. {
  10. printf("\nEnter number of processes: ");
  11. scanf("%d", &processes);
  12.  
  13. for (i = 0; i < processes; i++)
  14. {
  15. running[i] = 1;
  16. counter++;
  17. }
  18.  
  19. printf("\nEnter number of resources: ");
  20. scanf("%d", &resources);
  21.  
  22. printf("\nEnter resource instances:");
  23. for (i = 0; i < resources; i++)
  24. {
  25. scanf("%d", &maxres[i]);
  26. }
  27.  
  28. printf("\nEnter Allocated Resource Table:\n");
  29. for (i = 0; i < processes; i++)
  30. {
  31. for(j = 0; j < resources; j++)
  32. {
  33. scanf("%d", &current[i][j]);
  34. }
  35. }
  36.  
  37. printf("\nEnter Maximum Claim Table:\n");
  38. for (i = 0; i < processes; i++)
  39. {
  40. for(j = 0; j < resources; j++)
  41. {
  42. scanf("%d", &maximum_claim[i][j]);
  43. }
  44. }
  45.  
  46. for (i = 0; i < processes; i++)
  47. {
  48. for (j = 0; j < resources; j++)
  49. {
  50. allocation[j] += current[i][j];
  51. }
  52. }
  53.  
  54. printf("\nAllocated resources:");
  55. for (i = 0; i < resources; i++)
  56. {
  57. printf("\t%d", allocation[i]);
  58. }
  59.  
  60. for (i = 0; i < resources; i++)
  61. {
  62. available[i] = maxres[i] - allocation[i];
  63. }
  64.  
  65. printf("\nAvailable resources:");
  66. for (i = 0; i < resources; i++)
  67. {
  68. printf("\t%d", available[i]);
  69. }
  70. printf("\n");
  71.  
  72. printf("\nNeeded resources:\n");
  73. for(i=0;i<processes;i++)
  74. {
  75. for(j=0;j<resources;j++)
  76. {
  77. need[i][j]=maximum_claim[i][j]-current[i][j];
  78. printf("\t%d",need[i][j]);
  79. }
  80. printf("\n");
  81. }
  82.  
  83.  
  84. while (counter != 0)
  85. {
  86. safe = 0;
  87. for (i = 0; i < processes; i++)
  88. {
  89. if (running[i])
  90. {
  91. exec = 1;
  92. for (j = 0; j < resources; j++)
  93. {
  94. if (need[i][j]< available[j])
  95. {
  96. exec=0;
  97. break;
  98. }
  99. }
  100.  
  101. if (exec)
  102. {
  103. printf("\nprocess%d is eexecuting\n", i+1);
  104. running[i]=0;
  105. counter--;
  106. safe=1;
  107. for(j=0;j<resources;j++)
  108. {
  109. available[j]+=current[i][j];
  110. }
  111. break;
  112.  
  113. }
  114. }
  115. }
  116. if (!safe)
  117. {
  118. printf("\nThe processes are in unsafe state.\n");
  119. break;
  120. }
  121. else
  122. {
  123. printf("\nThe process is in safe state");
  124. printf("\nAvailable vector:");
  125.  
  126. for (i = 0; i < resources; i++)
  127. {
  128. printf("\t%d", available[i]);
  129. }
  130.  
  131. printf("\n");
  132. }
  133. }
  134. return 0;
  135. }
Add Comment
Please, Sign In to add comment