Banker’s Algorithm is one of the most representative deadlock avoidance algorithm.This Algorithm is initially designed for bank system which, makes it what it was nameed. The following is the Implementation of Banker’s Algorithm using C++.
while (true) { int m, n; cout << "input the account of resources categories" << endl; cin >> m; vector<int> Available(m, 0); cout << "input the account of each resource" << endl; for (int i = 0; i < m; ++i) { int temp; cin >> temp; Available[i] = temp; } cout << "input the account of processes" << endl; cin >> n; cout << "input the maximum demand vector of each process" << endl; vector<vector<int>> Max(n, vector<int>(m, 0)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; j++) { int temp; cin >> temp; Max[i][j] = temp; } } vector<vector<int>> Allocation(n, vector<int>(m, 0)); vector<vector<int>> Need(Max); while (true) { printResource(Max, Allocation, Need, Available);
cout << "input process applying" << endl; int aptemp; cin >> aptemp; vector<int> Request(m, 0); for (int i = 0; i < m; ++i) { int temp; cin >> temp; Request[i] = temp; } bool excced = false; for (int i = 0; i < m; ++i) { if (Request[i] > Need[aptemp][i]) { cout << "Number of resources requested by the process exceeds its announced maximum, and request has been rejected." << endl; excced = true; break; } } if (excced) { continue; }
for (int i = 0; i < m; ++i) { if (Available[i] < Request[i]) { cout << " System resources may not be able to satisfy a given process now, Waiting..." << endl; break; } }
//attempt to allocate for (int i = 0; i < m; ++i) { Available[i] = Available[i] - Request[i]; Allocation[aptemp][i] = Allocation[aptemp][i] + Request[i]; Need[aptemp][i] = Need[aptemp][i] - Request[i]; }
//security check auto Work = Available; vector<bool> Finish(n, false); for (int i = 0; i < n; ++i) { bool founded = true; if (Finish[i] == false) { for (int j = 0; j < m; ++j) { if (Need[i][j] > Work[j]) { founded = false; break; } } if (founded) { for (int j = 0; j < m; j++) { Work[j] = Work[j] + Allocation[i][j];//释放j的全部资源 } Finish[i] = true; i=-1; //if founded we must founed front first again; } } } bool security = true; for (int i = 0; i < n; ++i) { if (Finish[i] == false) { cout << "Unable to generate security sequence.Request has been rejected." << endl; security = false; break; } } if (security) { cout << "Resources have been allocated." << endl;
} else { for (int i = 0; i < m; ++i) { Available[i] = Available[i] + Request[i]; Allocation[aptemp][i] = Allocation[aptemp][i] - Request[i]; Need[aptemp][i] = Need[aptemp][i] + Request[i]; } } } }
Please note that the above program are designed to demonstrate the main logic of this algorithm,We didn’t actually create processes to apply for resources.Instead,We simply ask the user to input strings as the resources that a process is requesting.Function printResource should be customized. The running results are as follows:
Reprint policy:
All articles in this blog are used except for special statements
CC BY 4.0
reprint policy. If reproduced, please indicate source
5helter
!