Deadlock Avoidance
Deadlock Avoidance
Deadlock Avoidance
Deadlock refers to a situation in which one process is waiting for a resource that is currently
under the control of some other process. Hence, it results in the permanent blocking of the
processes. This situation can be avoided by avoiding the entry of a deadlock by using various
deadlock avoidance techniques.
Deadlock avoidance does not impose any rules but, here each resource request is carefully
analyzed to see whether it could be safely fulfilled without causing deadlock. The drawback of
this scheme is its requirement of information in advance about how resources are to be requested.
Different algorithms require different types and amounts of information, some require a
maximum number of resources that each process requires. The following are the various
deadlock avoidance algorithms.
Safe State :
Consider a system consisting of several processes like <P 1, P2, P3,…, Pn>. Each of them requires
certain resources immediately and also specifies the maximum number of resources that they
may need in their lifetime.
Using this information, we need to build a “safe sequence” which is a sequence of processes
where their resource request can be satisfied without having a deadlock occur. If there exists any
such safe sequence, then the system is said to be in a “safe state” during which deadlock cannot
occur. An unsafe state may lead to deadlock but not always. There are some sequences in the
unsafe state which can lead to deadlock.
P1 9 14
P2 6 8
P3 6 13
If the processes are executed in the sequence <P 2, P1, P3> then the safety condition can be
satisfied. The table below shows the sequence of resource allocation and release.
Total Printers
P2 P1 P3
Remaining
6+2=
P2 is allocated its maximum requirements. 9 6 1
8
9 + 5 =
P1 is allocated its maximum requirements. – 6 4
14
6 + 7 =
P3 is allocated its maximum requirements. – – 11
13
Any edge from a resource Rj to a process Pi (Rj → Pi) indicates that Rj is allocated to process Pi.
It is called an “assignment edge” when a request is fulfilled it is transformed into an assignment
edge. Processes are represented as circles and resources as rectangles. The below figure is an
example of the graph where process P1 has R1 and requests for R2.
For avoiding deadlocks using resource allocation graph, it is modified slightly by introducing a
new edge called “claim edge”. It is an edge from process P i to resource Rj (Pi → Rj) indicates that
in the future, Pi may request for Rj. The direction of the arrow is same as the request edge but
with a dashed line as shown below.
A process must specify in the beginning the maximum number of instances of each resource type
it may require. It is obvious that this number should not be more than the available. When the
process requests resources, the system decides whether allocation will result in deadlock or not.
If not, resources are allocated otherwise process has to wait.
The following are the various data structures that have to be created to implement Banker’s
algorithm. If ‘n’ is the number of processes and ‘m’ is the number of resources.
Step 1 – Assume work and finish as vectors of length ‘m’ and ‘n’ respectively.
Work = Avaiable
Finish[i] = ‘false’
Needi ≤ Work
Step 3 –
Work = Work + Allocation
Finish[i] = ‘true’
Step 4 – If finish[i] = True for all then the system is in a safe state.