Deadlock: Amity School of Engineering & Technology
Deadlock: Amity School of Engineering & Technology
Deadlock: Amity School of Engineering & Technology
Deadlock
Amity School of Engineering & Technology
Objectives
– describe deadlock, and forms of prevention,
avoidance, detection, and recovery
Amity School of Engineering & Technology
1) Requests a resource
2) Use the resource
2) Releases the resource
Amity School of Engineering & Technology
Amity School of Engineering & Technology
Example of Deadlock
A real-world example would be traffic, which is going only in
one direction. Here, a bridge is considered a resource. So, when
Deadlock happens, it can be easily resolved if one car backs up
(Preempt resources and rollback).Several cars may have to be
backed up if a deadlock situation occurs.So starvation is possible.
Amity School of Engineering & Technology
What is a Livelock?
• There is a variant of deadlock called livelock. This is a
situation in which two or more processes continuously
change their state in response to changes in the other
process(es) without doing any useful work. This is similar
to deadlock in that no progress is made but differs in that
neither process is blocked or waiting for anything.
Deadlock Prevention
• If we simulate deadlock with a table which is
standing on its four legs then we can also
simulate four legs with the four conditions which
when occurs simultaneously, cause the deadlock.
1. Mutual Exclusion
Spooling
For a device like printer, spooling can work. There
is a memory associated with the printer which
stores jobs from each of the process into it. Later,
Printer collects all the jobs and print each one of
them according to FCFS. By using this mechanism,
the process doesn't have to wait for the printer and
it can continue whatever it was doing. Later, it
collects the output when it is produced.
•
Amity School of Engineering & Technology
Amity School of Engineering & Technology
3. No Preemption
•Deadlock arises due to the fact that a process can't be stopped
once it starts. However, if we take the resource away from the
process which is causing deadlock then we can prevent deadlock.
•This is not a good approach at all since if we take a resource
away which is being used by the process then all the work which
it has done till now can become inconsistent.
•Consider a printer is being used by any process. If we take the
printer away from that process and assign it to some other
process then all the data which has been printed can become
inconsistent and ineffective and also the fact that the process
can't start printing again from where it has left which causes
performance inefficiency.
Amity School of Engineering & Technology
4. Circular Wait
•To violate circular wait, we can assign a priority
number to each of the resource. A process can't
request for a lesser priority resource. This ensures
that not a single process can request a resource
which is being utilized by some other process and
no cycle will be formed.
Amity School of Engineering & Technology
Deadlock avoidance
Resources Assigned
A 3 0 2 2
B 0 0 1 1
C 1 1 1 0
Resources Assigned
D 2 1 4 0
A 1 1 0 0
B 0 1 1 2
C 1 2 1 0
D 2 1 1 2
Amity School of Engineering & Technology
E = (7 6 8 4)
P = (6 2 8 3)
A = (1 4 0 1)
Amity School of Engineering & Technology
Example
•Let’s consider 3 processes P1, P2 and P3, and two types of
resources R1 and R2. The resources are having 1 instance each.
•According to the graph, R1 is being used by P1, P2 is holding R2
and waiting for R1, P3 is waiting for R1 as well as R2.
•The graph is deadlock free since no cycle is being formed in the
graph.
Amity School of Engineering & Technology
Allocation Matrix
Allocation matrix can be formed by using the Resource
allocation graph of a system. In Allocation matrix, an entry
will be made for each of the resource assigned. For Example,
in the following matrix, en entry is being made in front of P1
and below R3 since R3 is assigned to P1.
Process R1 R2 R3
P1 0 0 1
P2 1 0 0
P3 0 1 0
Amity School of Engineering & Technology
Request Matrix
In request matrix, an entry will be made for each of the
resource requested. As in the following example, P1 needs R1
therefore an entry is being made in front of P1 and below R1.
Process R1 R2 R3
P1 1 0 0
P2 0 1 0
P3 0 0 1
Avial = (0,0,0)
Amity School of Engineering & Technology
For Resource
Preempt the resource
We can snatch one of the resources from the owner of the
resource (process) and give it to the other process with the
expectation that it will complete the execution and will release
this resource sooner. Well, choosing a resource which will be
snatched is going to be a bit difficult.
For Process
Kill a process
Killing a process can solve our problem but the
bigger concern is to decide which process to kill.
Generally, Operating system kills a process which
has done least amount of work until now.