fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int numProcesses, numResources;
  6.  
  7. cout << "Enter the number of processes: ";
  8. cin >> numProcesses;
  9.  
  10. cout << "Enter the number of resources: ";
  11. cin >> numResources;
  12.  
  13. // Input matrices and arrays
  14. int allocation[10][10], max[10][10], need[10][10], available[10];
  15. bool finish[10] = {false}; // Keep track of finished processes
  16. int safeSequence[10]; // To store the safe sequence
  17.  
  18. // Input Allocation matrix
  19. cout << "Enter the Allocation matrix:" << endl;
  20. for (int i = 0; i < numProcesses; i++) {
  21. for (int j = 0; j < numResources; j++) {
  22. cin >> allocation[i][j];
  23. }
  24. }
  25.  
  26. // Input Max matrix
  27. cout << "Enter the Max matrix:" << endl;
  28. for (int i = 0; i < numProcesses; i++) {
  29. for (int j = 0; j < numResources; j++) {
  30. cin >> max[i][j];
  31. // Calculate the Need matrix on the fly
  32. need[i][j] = max[i][j] - allocation[i][j];
  33. }
  34. }
  35.  
  36. // Input Available resources
  37. cout << "Enter the Available resources:" << endl;
  38. for (int i = 0; i < numResources; i++) {
  39. cin >> available[i];
  40. }
  41.  
  42. int count = 0; // Number of processes completed
  43. bool foundDeadlock = false; // Flag for deadlock detection
  44.  
  45. while (count < numProcesses) {
  46. bool found = false;
  47. for (int i = 0; i < numProcesses; i++) {
  48. if (!finish[i]) { // Process not finished
  49. bool canProceed = true;
  50.  
  51. // Check if the process can proceed (if its need is <= available)
  52. for (int j = 0; j < numResources; j++) {
  53. if (need[i][j] > available[j]) {
  54. canProceed = false;
  55. break;
  56. }
  57. }
  58.  
  59. if (canProceed) {
  60. // Add allocated resources back to available
  61. for (int j = 0; j < numResources; j++) {
  62. available[j] += allocation[i][j];
  63. }
  64. safeSequence[count++] = i;
  65. finish[i] = true;
  66. found = true;
  67. break;
  68. }
  69. }
  70. }
  71.  
  72. // If no process can proceed, then there's a deadlock
  73. if (!found) {
  74. cout << "The system is in a deadlock state." << endl;
  75. foundDeadlock = true;
  76. break;
  77. }
  78. }
  79.  
  80. // If no deadlock is detected, print the safe sequence
  81. if (!foundDeadlock) {
  82. cout << "The system is in a safe state. Safe sequence is: ";
  83. for (int i = 0; i < numProcesses; i++) {
  84. cout << "P" << safeSequence[i];
  85. if (i < numProcesses - 1) {
  86. cout << " -> ";
  87. }
  88. }
  89. cout <<"\n No Deadlock Occurs"<< endl;
  90. } else {
  91. // Display which processes are involved in the deadlock
  92. cout << "Deadlocked processes: ";
  93. for (int i = 0; i < numProcesses; i++) {
  94. if (!finish[i]) {
  95. cout << "P" << i << " ";
  96. }
  97. }
  98. cout << endl;
  99. }
  100.  
  101. return 0;
  102. }
  103.  
  104.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Enter the number of processes: Enter the number of resources: Enter the Allocation matrix:
Enter the Max matrix:
Enter the Available resources:
The system is in a safe state. Safe sequence is: P0
 No Deadlock Occurs