Banker-Algorithm


The Implementation of Banker’s Algorithma

Introduction

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++.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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:
result.png
As we can see,the program runs very well!


Author: 5helter
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 !
  TOC