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