fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class BankersAlgorithm {
  6. private:
  7. int P, R;
  8. vector<vector<int>> max, allot, need;
  9. vector<int> available, work, safeSeq;
  10. vector<bool> finish;
  11.  
  12. public:
  13. // Constructor to initialize the Banker's algorithm variables
  14. BankersAlgorithm(int processes, int resources) {
  15. P = processes; // Number of processes
  16. R = resources; // Number of resources
  17. max.resize(P, vector<int>(R));
  18. allot.resize(P, vector<int>(R));
  19. need.resize(P, vector<int>(R));
  20. finish.resize(P, false);
  21. available.resize(R);
  22. work.resize(R);
  23. }
  24.  
  25. // Function to set maximum resource requirements for each process
  26. void setMax(int i, vector<int> maxResources) {
  27. max[i] = maxResources;
  28. }
  29.  
  30. // Function to set allocated resources for each process
  31. void setAllot(int i, vector<int> allottedResources) {
  32. allot[i] = allottedResources;
  33. }
  34.  
  35. // Function to set available resources
  36. void setAvailable(vector<int> availableResources) {
  37. available = availableResources;
  38. }
  39.  
  40. // Function to calculate the Need matrix
  41. void calculateNeed() {
  42. for (int i = 0; i < P; i++) {
  43. for (int j = 0; j < R; j++) {
  44. need[i][j] = max[i][j] - allot[i][j];
  45. }
  46. }
  47. }
  48.  
  49. // Function to check if the system is in a safe state
  50. bool isSafe() {
  51. work = available;
  52. safeSeq.clear();
  53. fill(finish.begin(), finish.end(), false);
  54.  
  55. int count = 0;
  56. while (count < P) {
  57. bool found = false;
  58. for (int i = 0; i < P; i++) {
  59. if (!finish[i]) {
  60. bool canFinish = true;
  61. // Check if the process can finish with current available resources
  62. for (int j = 0; j < R; j++) {
  63. if (need[i][j] > work[j]) {
  64. canFinish = false;
  65. break;
  66. }
  67. }
  68. // If the process can finish, update the work and finish status
  69. if (canFinish) {
  70. for (int k = 0; k < R; k++) {
  71. work[k] += allot[i][k]; // Add allocated resources to work
  72. }
  73. finish[i] = true;
  74. safeSeq.push_back(i);
  75. count++;
  76. found = true;
  77. }
  78. }
  79. }
  80. if (!found) {
  81. return false; // If no process can finish, the system is unsafe
  82. }
  83. }
  84. return true; // If all processes can finish, the system is safe
  85. }
  86.  
  87. // Function to print the safe sequence
  88. void printSafeSequence() {
  89. if (isSafe()) {
  90. cout << "System is in a safe state.\nSafe sequence is: ";
  91. for (int i = 0; i < P; i++) {
  92. cout << "P" << safeSeq[i] << " ";
  93. }
  94. cout << endl;
  95. } else {
  96. cout << "System is in an unsafe state.\n";
  97. }
  98. }
  99. };
  100.  
  101. int main() {
  102. int P = 5; // Number of processes
  103. int R = 3; // Number of resources
  104.  
  105. BankersAlgorithm bank(P, R);
  106.  
  107. // Set maximum resource requirements for each process
  108. bank.setMax(0, {7, 5, 3});
  109. bank.setMax(1, {3, 2, 2});
  110. bank.setMax(2, {9, 0, 2});
  111. bank.setMax(3, {2, 2, 2});
  112. bank.setMax(4, {4, 3, 3});
  113.  
  114. // Set allocated resources for each process
  115. bank.setAllot(0, {0, 1, 0});
  116. bank.setAllot(1, {2, 0, 0});
  117. bank.setAllot(2, {3, 0, 2});
  118. bank.setAllot(3, {2, 1, 1});
  119. bank.setAllot(4, {0, 0, 2});
  120.  
  121. // Set available resources
  122. bank.setAvailable({3, 3, 2});
  123.  
  124. // Calculate Need matrix and print safe sequence
  125. bank.calculateNeed();
  126. bank.printSafeSequence();
  127.  
  128. return 0;
  129. }
  130.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
System is in a safe state.
Safe sequence is: P1 P3 P4 P0 P2