fork download
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3.  
  4. #define MAX_PROCESSES 10
  5. #define MAX_RESOURCES 10
  6.  
  7. int n, m;
  8. int Available[MAX_RESOURCES];
  9. int Max[MAX_PROCESSES][MAX_RESOURCES];
  10. int Allocation[MAX_PROCESSES][MAX_RESOURCES];
  11. int Need[MAX_PROCESSES][MAX_RESOURCES];
  12.  
  13. void calculateNeed() {
  14. for (int i = 0; i < n; i++)
  15. for (int j = 0; j < m; j++)
  16. Need[i][j] = Max[i][j] - Allocation[i][j];
  17. }
  18.  
  19. bool isSafe() {
  20. int Work[MAX_RESOURCES];
  21. bool Finish[MAX_PROCESSES];
  22. int safeSequence[MAX_PROCESSES];
  23. int count = 0;
  24.  
  25. for (int j = 0; j < m; j++)
  26. Work[j] = Available[j];
  27. for (int i = 0; i < n; i++)
  28. Finish[i] = false;
  29.  
  30. while (count < n) {
  31. bool found = false;
  32. for (int i = 0; i < n; i++) {
  33. if (!Finish[i]) {
  34. bool canAllocate = true;
  35. for (int j = 0; j < m; j++) {
  36. if (Need[i][j] > Work[j]) {
  37. canAllocate = false;
  38. break;
  39. }
  40. }
  41. if (canAllocate) {
  42. for (int j = 0; j < m; j++)
  43. Work[j] += Allocation[i][j];
  44. safeSequence[count] = i;
  45. Finish[i] = true;
  46. count++;
  47. found = true;
  48. }
  49. }
  50. }
  51. if (!found) {
  52. printf("System is not in a safe state.\n");
  53. return false;
  54. }
  55. }
  56.  
  57. printf("System is in a safe state.\nSafe sequence: ");
  58. for (int i = 0; i < n; i++)
  59. printf("P%d ", safeSequence[i]);
  60. printf("\n");
  61. return true;
  62. }
  63.  
  64. void requestResources(int process, int Request[]) {
  65. for (int j = 0; j < m; j++) {
  66. if (Request[j] > Need[process][j]) {
  67. printf("Error: Request exceeds maximum need for process P%d.\n", process);
  68. return;
  69. }
  70. }
  71.  
  72. for (int j = 0; j < m; j++) {
  73. if (Request[j] > Available[j]) {
  74. printf("Resources not available. Process P%d must wait.\n", process);
  75. return;
  76. }
  77. }
  78.  
  79. for (int j = 0; j < m; j++) {
  80. Available[j] -= Request[j];
  81. Allocation[process][j] += Request[j];
  82. Need[process][j] -= Request[j];
  83. }
  84.  
  85. if (isSafe()) {
  86. printf("Request granted for process P%d.\n", process);
  87. } else {
  88. for (int j = 0; j < m; j++) {
  89. Available[j] += Request[j];
  90. Allocation[process][j] -= Request[j];
  91. Need[process][j] += Request[j];
  92. }
  93. printf("Request denied for process P%d. System would be unsafe.\n", process);
  94. }
  95. }
  96.  
  97. int main() {
  98. printf("Enter number of processes: ");
  99. scanf("%d", &n);
  100. printf("Enter number of resource types: ");
  101. scanf("%d", &m);
  102.  
  103. printf("Enter Available resources (for %d resource types):\n", m);
  104. for (int j = 0; j < m; j++) {
  105. printf("Resource %d: ", j);
  106. scanf("%d", &Available[j]);
  107. }
  108.  
  109. printf("Enter Max matrix (%d processes x %d resources):\n", n, m);
  110. for (int i = 0; i < n; i++)
  111. for (int j = 0; j < m; j++) {
  112. printf("Max[P%d][R%d]: ", i, j);
  113. scanf("%d", &Max[i][j]);
  114. }
  115.  
  116. printf("Enter Allocation matrix (%d processes x %d resources):\n", n, m);
  117. for (int i = 0; i < n; i++)
  118. for (int j = 0; j < m; j++) {
  119. printf("Allocation[P%d][R%d]: ", i, j);
  120. scanf("%d", &Allocation[i][j]);
  121. }
  122.  
  123. calculateNeed();
  124.  
  125. printf("\nMax Matrix:\n");
  126. for (int i = 0; i < n; i++) {
  127. for (int j = 0; j < m; j++)
  128. printf("%d ", Max[i][j]);
  129. printf("\n");
  130. }
  131.  
  132. printf("\nAllocation Matrix:\n");
  133. for (int i = 0; i < n; i++) {
  134. for (int j = 0; j < m; j++)
  135. printf("%d ", Allocation[i][j]);
  136. printf("\n");
  137. }
  138.  
  139. printf("\nNeed Matrix:\n");
  140. for (int i = 0; i < n; i++) {
  141. for (int j = 0; j < m; j++)
  142. printf("%d ", Need[i][j]);
  143. printf("\n");
  144. }
  145.  
  146. printf("\nAvailable Resources: ");
  147. for (int j = 0; j < m; j++)
  148. printf("%d ", Available[j]);
  149. printf("\n");
  150.  
  151. isSafe();
  152.  
  153. int process;
  154. printf("\nEnter process number for resource request (0 to %d): ", n - 1);
  155. scanf("%d", &process);
  156. int Request[MAX_RESOURCES];
  157. printf("Enter Request vector for P%d (for %d resources):\n", process, m);
  158. for (int j = 0; j < m; j++) {
  159. printf("Request[R%d]: ", j);
  160. scanf("%d", &Request[j]);
  161. }
  162.  
  163. requestResources(process, Request);
  164.  
  165. return 0;
  166. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Enter number of processes: Enter number of resource types: Enter Available resources (for 0 resource types):
Enter Max matrix (0 processes x 0 resources):
Enter Allocation matrix (0 processes x 0 resources):

Max Matrix:

Allocation Matrix:

Need Matrix:

Available Resources: 
System is in a safe state.
Safe sequence: 

Enter process number for resource request (0 to -1): Enter Request vector for P0 (for 0 resources):
System is in a safe state.
Safe sequence: 
Request granted for process P0.