Deadlocks avoidance: Habermann`s algorithm

Deadlocks avoidance: Habermann’s algorithm
Assume that we have processes P1,…,PN requiring resources, number of which is
a (for general case it may be vector, now we consider a to be a scalar value). There is
vector b=(b1, …, bN) representing maximal requirements of each i-th process to
resources, and vector с=(с1, …, сN) shows current resources granted to respective
processes.
State of resources is said to realizable, if the following holds:

(1)
k bk  a 

( 2)
 Nc  b

(3)
ci  a
 
i 1
N
Number of free resources r t   a   c i must satisfy to
i 1
(4)
rt   0
If realizable state guarantees absence of deadlocks, it is called safe, otherwise –
not safe. Realizable state (a,b,c) is safe if there exists such sequence S of processes that:


Pij  S  bij  r t    cik (t )  (5)
k j



Such sequence S is called a safe sequence. Condition (5) says that request of the process
Pij on resources should not exceed the sum of free resources and the sum of resources
which will be freed by preceding it in the sequence processes.
Importance of the concept of safe state is that starting from such state we may
proceed without deadlocks even in the situation when processes request additional
resources during their life and don’t release them until their termination, if grant
additional resources to processes according to sequence S.
Also important: if resources state is safe and partial sequence S of processes satisfies (5),
it can be extended.
Safe state detection algorithm
1. S=0 (empty sequence satisfies (5));
2. Try to extend S so, that it would satisfy (5) after appending to S some process Рk.
This trial may fail due to: а) all processes are already in S, it means that state is safe;
b) no one extension satisfies (5), it means that state is not safe.
Let’s consider this algorithm in more details: S – is a partially formed sequence;
S* – is complement of S; Т – is a set of candidates on inclusion in S (Т and S* are
related), R is number of free resource units.
{
S=0;
T=S*;
while (T<>0) {
Candidate=Т[1];
Т=Т-{Т[1]};
if (B[Candidate]-С[Candidate]R+  c[i] ){
iS
S=S{ Candidate };
T=S*;
}
}
SAFE=(S*=0)
}
Order of emerging of requests may differ from the sequence S. In such a case detection
of safe state may be made for each new request. Given above algorithm may be
simplified due to the following statement: if safe state is modified by assigning additional
resources to process Рk and if some sequence S (it may be partial) contains Рk and
satisfies to (5), then new state will be also safe.
Example 1:
Let number of processes N=5, а=10 is a number of resource units, b – is a vector
of maximal requests on resources, с – is a vector of currently granted resources:
5
2
 
 
8
 3
b  7 , c  1 .
 
 
6
1
9
 3
 
 
Is such a state (a, b, c) safe? Is it realizable?
2+3+1+1+3=10  (1)-(3) are satisfied  realizable.
1) Let’s check this state for safety.
S=0
T=S*: Т={1,2,3,4,5}
Candidate=1
T={2,3,4,5}
B1-C1=5-2=30; No
T={2,3,4,5}
Candidate =2
T={3,4,5}
B2-C2=8-3=50; No
T={3,4,5}
Candidate =3
T={4,5}
B3-C3=7-1=60; No
T={4,5}
Candidate =4
T={5}
B4-C4=6-1=50; No
T={5}
Candidate =5
T=0 -- Empty
B5-C5=9-3=60; No
T=0
S*0 – Not empty
Hence, this state is not safe.
Example 2: Let’s consider the previous system, but now
5
1
 
 
8
1
b  7 , c  1
 
 
6
1
9
2
 
 
B1-C1=5-1=44+0 – Yes
S=0{1}={1}
T={2,3,4,5}
Candidate=2
T={3,4,5}
B2-C2=8-1=74+1; No
T={3,4,5}
Candidate =3
T={4,5}
B3-C3=7-1=64+1; No
T={4,5}
Candidate =4
T={5}
B4-C4=6-1=54+1; Yes
S={1,4}
T={2,3,5} – Complement of S
Candidate =2
T={3,5}
B2-C2=8-1=74+1+1; No
T={3,5}
Candidate =3
T={5}
B3-C3=7-1=64+1+1; Yes
S={1,4,3}
T={2,5} – Complement of S
Candidate =2
T={5}
B2-C2=8-1=74+1+1+1; Yes
S={1,4,3,2}
Candidate =5
T=0 – Empty
B5-C5=9-2=74+1+1+1+1; Yes
S={1,4,3,2,5}
T=0
Hence, state is safe. Let’s consider development of the processes in the dynamics:
P1
0,3
P2
0,5
P3
0,7 
3,7 
1
3
5,11
1
4
7,10 
7,11
5
11,14
5
10,13
11,  
0
14,17 
17,  
8
0
13,  
0
7
6
P4
12,  
9,12 
2,9
0
6
3
P5
12,15
15,20 
20,  
6,12 
7
9
0
4
0,3 means that P1 requires 1 unit of resource from
In the table above, for example,
1
moment 0 till moment 3 (in the absence of other processes). As far as really processes
will work in parallel, there may be some delays due to the absence of resources, but
lengths of time intervals necessary for processes to work with some resources will be
invariant. Safe sequence according to initial resources requests is (1,4,3,2,5) as it was
obtained in Example 2.
1
0,2 
1
0,6
2
resources
10
9
8
7
6
5
4
3
2
1
P1
P1
P5
P4
Wait
P2
P2
4
P4
P4
P4
P4
Wait
P3
P3
Wait
P1
P1
3
P3
Wait
P1
1 2
P4
P4
5 6
7
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Resources are granted according to safe sequence (1, 4, 3, 2, 5). At moment 3 P1 will ask
for additional resources and will get them because he is first in the sequence.
Р
– Process run
Wait – waiting
– free
Similar idea has banker’s algorithm – to grant resources only if resulting state will be
deadlock safe.