fork download
  1.  
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main() {
  6. int process, resource;
  7. cout << "Enter number of processes: ";
  8. cin >> process;
  9. cout << "Enter number of resources: ";
  10. cin >> resource;
  11. int allocation[10][10];
  12. int maximum[10][10];
  13. int remaining[10][10];
  14. int available[10];
  15. cout << "\nEnter Allocation Matrix:\n";
  16. for (int i = 0; i < process; i++) {
  17. for (int j = 0; j < resource; j++) {
  18. cin >> allocation[i][j];
  19. }
  20. }
  21. cout << "\nEnter Maximum Matrix:\n";
  22. for (int i = 0; i < process; i++) {
  23. for (int j = 0; j < resource; j++) {
  24. cin >> maximum[i][j];
  25. }
  26. }
  27. cout << "\nEnter Available Resources:\n";
  28. for (int i = 0; i < resource; i++) {
  29. cin >> available[i];
  30. }
  31.  
  32. for (int i = 0; i < process; i++) {
  33. for (int j = 0; j < resource; j++) {
  34. remaining[i][j] = maximum[i][j] - allocation[i][j];
  35. }
  36. }
  37. cout << "\nRemaining Matrix:\n";
  38. for (int i = 0; i < process; i++) {
  39. for (int j = 0; j < resource; j++) {
  40. cout << remaining[i][j] << " ";
  41. }
  42. cout << endl;
  43. }
  44. bool finished[10] = {false};
  45. int safeSequence[10];
  46. int count = 0;
  47. while (count < process) {
  48. bool found = false;
  49. for (int i = 0; i < process; i++) {
  50. if (!finished[i]) {
  51.  
  52. bool possible = true;
  53.  
  54. for (int j = 0; j < resource; j++) {
  55.  
  56. if (remaining[i][j] > available[j]) {
  57. possible = false;
  58. break;
  59. }
  60. }
  61.  
  62. if (possible) {
  63.  
  64. for (int j = 0; j < resource; j++) {
  65. available[j] += allocation[i][j];
  66. }
  67.  
  68. safeSequence[count] = i;
  69. count++;
  70. finished[i] = true;
  71. found = true;
  72. }
  73. }
  74. }
  75.  
  76. if (!found) {
  77. cout << "\nSystem is NOT in safe state.\n";
  78. return 0;
  79. }
  80. }
  81.  
  82. cout << "\nSystem is in SAFE state.\n";
  83.  
  84. cout << "Safe Sequence: ";
  85.  
  86. for (int i = 0; i < process; i++) {
  87.  
  88. cout << "P" << safeSequence[i];
  89.  
  90. if (i != process - 1)
  91. cout << " -> ";
  92. }
  93.  
  94. cout << endl;
  95.  
  96. return 0;
  97. }
  98.  
  99.  
  100.  
  101.  
  102. /*
  103. Enter number of processes: 5
  104. Enter number of resources: 3
  105.  
  106. Enter Allocation Matrix:
  107. 0 1 0
  108. 2 0 0
  109. 3 0 2
  110. 2 1 1
  111. 0 0 2
  112.  
  113. Enter Maximum Matrix:
  114. 7 5 3
  115. 3 2 2
  116. 9 0 2
  117. 2 2 2
  118. 4 3 3
  119.  
  120. Enter Available Resources:
  121. 3 3 2
  122.  
  123. Need Matrix:
  124. 7 4 3
  125. 1 2 2
  126. 6 0 0
  127. 0 1 1
  128. 4 3 1
  129.  
  130.  
  131.  
  132.  
  133.  
  134. */
  135.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Enter number of processes: Enter number of resources: 
Enter Allocation Matrix:

Enter Maximum Matrix:

Enter Available Resources:

Remaining Matrix:

System is in SAFE state.
Safe Sequence: