The banker algorithm (Banker's algorithm) is a well-known algorithm for avoiding deadlocks (Deadlock), an algorithm designed by Edsger Dijkstra to avoid deadlocks in 1965 for T.H.E systems. It is based on the allocation strategy of the bank loan system, and determines and guarantees the safe operation of the system.
In the avoidance method, the process is allowed to request resources dynamically, but before the system allocates resources, the security of the allocated resources should be calculated first, if the allocation will not cause the system to enter an unsafe state, then allocate, otherwise wait. To implement the banker algorithm, the system must set up several data structures.
1) Total number of resources vector resource
is an array of M elements, each of which represents the total number of resources that a class of systems can provide. If Resource[j]=k, it represents the existing RJ class resource K in the system.
2) Maximum demand matrix claim
This is a nxm matrix that defines the maximum demand for M-class resources for each process in n processes in a system. If claim[i,j]=k, indicates that process I requires the maximum number of RJ class resources to be K.
3) Distribution matrix allocation
This is also a nxm matrix that defines the number of resources that each class of resources in the system currently has allocated to each process. If allocation[i,j]=k, indicates that process I currently has the number of RJ class resources as K.
4) Number of resources that can also be used available vector
This is an array of M-elements, each representing the number of resources that a class can use.
The algorithm's execution process is simple:
(1) Use Resource[i]-sum[i] (each process takes up the total number of resources) to find the Available[i] Array
(2) then traverse the n*m matrix, as long as there is a process for all types of resources are less than the available array, it is considered to be safe, and after the end of its occupation of the resources released, that is Available[j]+=alloc[i][j];
(3) On the contrary, in the previous step, if you cannot find such a sequence, we will assume that it is a full sequence of unrest, do not allow allocation, the end of the program.
#include <iostream> #include <string> #include <cstring> #include <vector>using namespace std;# Define M 4#define N 3int resource[m];int available[m];int claim[n][m];int alloc[n][m];int vis[n];vector<int> ans; int main () {cout<< "Enter the maximum value of each process to the resource matrix" <<endl;for (int i=0;i<n;i++) for (int j=0;j<m;j++) cin>> claim[i][j];cout<< "Please enter each resource already assigned by each process" <<endl;for (int i=0;i<n;i++) for (int j=0;j<m;j++) cin>> alloc[i][j];cout<< "Please enter the total number of individual resources" <<endl;for (int i=0;i<m;i++) Cin>>resource[i];int Sum[M];memset (Sum,0,sizeof (sum)); for (int i=0;i<n;i++) {for (int j=0;j<m;j++) sum[j]+=alloc[i][j];} for (int i=0;i<m;i++) available[i]=resource[i]-sum[i];memset (vis,0,sizeof (Vis)); ans.clear (); int Is_ok=0;while (1 {if (ans.size () ==n) {is_ok=1;break;} int index;int flag=0;for (int i=0;i<n;i++) {int judge=1;if (!vis[i]) {for (int j=0;j<m;j++) {if (available[j]< Claim[i][j]-alloc[i][j]) {judge=0; break;}}} Elsecontinue;if (judge) {Index=i; flag=1;break;}} if (flag) {vis[index]=1; Ans.push_back (index); for (int k=0;k<m;k++) available[k]+=alloc[index][k]; } else {cout<< "There is a deadlock, cannot be assigned!" <<endl;return 0; }}if (IS_OK) {cout<< "No deadlock occurs, the security sequence is:" <<endl;for (int i=0;i<ans.size (); i++) cout<<ans[i]<< " "; Cout<<endl;} return 0;}
Operating system: Banker algorithm Analysis and code implementation (c + + language)