fork download
  1. #include <stdio.h>
  2.  
  3. #define MAX_PROCESS 10
  4. #define MAX_RESOURCE 10
  5.  
  6. int main() {
  7. int n, m; // Number of processes and resources
  8. int allocation[MAX_PROCESS][MAX_RESOURCE]; // Allocation Matrix
  9. int max[MAX_PROCESS][MAX_RESOURCE]; // Maximum Need Matrix
  10. int available[MAX_RESOURCE]; // Available Resources
  11. int need[MAX_PROCESS][MAX_RESOURCE]; // Need Matrix
  12. int finish[MAX_PROCESS]; // Finish array (0 = not finished, 1 = finished)
  13. int safeSeq[MAX_PROCESS]; // Safe sequence array
  14. int work[MAX_RESOURCE]; // Work array
  15.  
  16. printf("Enter the number of processes: ");
  17. scanf("%d", &n);
  18. printf("Enter the number of resources: ");
  19. scanf("%d", &m);
  20.  
  21. printf("Enter the allocation matrix (%d x %d):\n", n, m);
  22. for (int i = 0; i < n; ++i) {
  23. for (int j = 0; j < m; ++j) {
  24. scanf("%d", &allocation[i][j]);
  25. }
  26. }
  27.  
  28. printf("Enter the maximum matrix (%d x %d):\n", n, m);
  29. for (int i = 0; i < n; ++i) {
  30. for (int j = 0; j < m; ++j) {
  31. scanf("%d", &max[i][j]);
  32. }
  33. }
  34.  
  35. printf("Enter the available resources (%d elements):\n", m);
  36. for (int i = 0; i < m; ++i) {
  37. scanf("%d", &available[i]);
  38. }
  39.  
  40. // Initialize finish array and calculate need matrix
  41. for (int i = 0; i < n; ++i) {
  42. finish[i] = 0;
  43. for (int j = 0; j < m; ++j) {
  44. need[i][j] = max[i][j] - allocation[i][j];
  45. }
  46. }
  47.  
  48. // Initialize work array with available resources
  49. for (int i = 0; i < m; ++i) {
  50. work[i] = available[i];
  51. }
  52.  
  53. int count = 0; // Number of processes in the safe sequence
  54. int safeIndex = 0;
  55.  
  56. // Find a safe sequence
  57. while (count < n) {
  58. int found = 0; // Flag to indicate if a process was found in this iteration
  59. for (int i = 0; i < n; ++i) {
  60. if (finish[i] == 0) { // If process i is not finished
  61. int canExecute = 1; // Assume process i can execute
  62. for (int j = 0; j < m; ++j) {
  63. if (need[i][j] > work[j]) { // If need is greater than work
  64. canExecute = 0;
  65. break;
  66. }
  67. }
  68.  
  69. if (canExecute) { // If process i can execute
  70. for (int j = 0; j < m; ++j) {
  71. work[j] += allocation[i][j]; // Release resources
  72. }
  73. finish[i] = 1;
  74. safeSeq[safeIndex++] = i;
  75. found = 1;
  76. count++;
  77. }
  78. }
  79. }
  80.  
  81. if (!found) { // If no process was found in this iteration, system is unsafe
  82. printf("The system is in an UNSAFE state.\n");
  83. return 0;
  84. }
  85. }
  86.  
  87. // Print the safe sequence
  88. printf("The system is in a SAFE state.\n");
  89. printf("Following is the SAFE Sequence: ");
  90. for (int i = 0; i < n - 1; ++i) {
  91. printf("P%d -> ", safeSeq[i]);
  92. }
  93. printf("P%d\n", safeSeq[n - 1]);
  94.  
  95. return 0;
  96. }
Success #stdin #stdout 0s 5316KB
stdin
4
3
6
5
7
6
3
1
1
2
3
3
2
2
1
2
3
4
1
3
5
0
1
2
2
1
1
0
3
3
1
2
1
0
1
y
2
n
stdout
Enter the number of processes: Enter the number of resources: Enter the allocation matrix (4 x 3):
Enter the maximum matrix (4 x 3):
Enter the available resources (3 elements):
The system is in a SAFE state.
Following is the SAFE Sequence: P0 -> P1 -> P2 -> P3