Dead Lock
Dead Lock
•Introduction
•Request
•Release
•Wait
•Deadlock avoidance
•Banker’s algorithm
•Deadlock Detection
•Recovery
Introduction
•In Multiprogramming environment Several process may compete for a finite
number of resources
•A process request resources, if resources are not available at that time the
process enter to the waiting state
•A waiting process never change its state, because the resource it has
requested are held by other waiting process, this situation is known as
Dead Lock
•A system may consist finite number of resources, distributed among a
number of competing processes.
•The resources are normally either physical like memory, keyboard or
logical like CPU cycle, CLOCK Speed, Time slice etc.
Under the normal mode of operation, a process may utilize a resource only
in the following sequence:-
Request
Use
Release
Request:-
The process request the resource, if the resource cannot be granted
immediately , then the requesting process must wait till the resources
are not available.
Use:-
The process can operate on the resource.
Release:-
The process release the resource after its completion.
Note:-
A set of process are in Dead Lock state when every process set are in
the waiting for an event that can be caused only by the another
process in the set.
Resource Allocation Graph:-
Resource
Process
R1P1
P1R2
P1 R1
P3
R2
P2
R3
P1 P2 P3
R3
P1 allocate
R1
a b
Wait
P2 R1
P
In Dead Lock
2. Hold And Wait
• A process must be holding at least one resource and waiting to acquire
additional resource that are currently being held by other process.
P1 All
oc
a it ate
W
R2 R1
All
oca it
e t P2 Wa
Demerit:
•Resource utilization is very low
•Starvation may occur
Methods for Handling Deadlocks:
1. Dead lock prevention
2. Dead lock avoidance
3. Dead lock Detection
4. Recovery
When Dead lock occur we can avoid it one of three ways
5. We can use a protocol to prevent and avoid dead lock by ensuring that
system will never enter in dead lock state.
6. If process enter in dead lock state, detect it and recover from it.
7. Ignore the problem altogether and pretend that deadlocks never
occur in the system.(Used by most OS including Window and UNIX)
Dead Lock Prevention:
Dead lock ensure that at least one of the 4 necessary condition should not be
occur.
•Mutual Exclusion
•Hold And Wait
•No preemption
•Circular Wait
3. Circular Wait
• A set of process (P0,P1,…,Pn) of waiting process must exist such that p0
is waiting for a resource held by P2,….Pn-1 is waiting for a resource held
by Pn and Pn I waiting for a resource held by P0 is known as circular wait.
P1 Wa
it
a it
W
P4 P2
Wa
it it
P3 Wa
R1 P2
P3
P1
R2
P4
•Mutual Exclusion:
It holds for non – sharable resources. If we use the sharable resources,
mutual exclusion condition doesn’t hold and the process can not be
involved in dead lock situation because a process never wait for a sharable
resource. Read only files are the example of sharable resource, whereas
printer is a non- sharable resource.
Safe
•Deadlock avoidance algorithm for avoiding deadlocks is to require
additional information about how the resources are requested, with this
additional information of complete sequence of request and release for such
process, we can decide for each request whether or not the process should
wait.
•It dynamically examines the resource allocation graph to ensure that a
circular wait can never exist.
•A state is a safe state if the system can allocate resource to each process in
some order and avoid a dead lock or that is a system is in safe state only if
there exist a safe sequence.
•A sequence of process P 0,P1….,Pn is known as safe sequence if each (P0) ,
the resource that Pi can still request can be satisfied by the currently
available resource plus the resource held by all other processes.
•If needed resource of Pi are not immediately available, then Pi can wait
until Pj have finished. When they have finished, Pi can obtained its needed
resources, complete its task and return it to OS for termination.
•When Pi terminates, Pi+1 can obtained its resource and complete its
execution, if no such sequence exist, the system state is said to be unsafe.
•A safe state is not a dead lock state.
• a Deadlock is an Unsafe state, but not all the unsafe state are dead lock.
•An unsafe state may lead to a deadlock .
Banker’s Algorithm:
•Also known as deadlock avoidance algorithm.
•Sometime referred as detection algorithm .
•It tests for safety by simulating the:-
allocation for predetermined maximum possible amounts of all
resources.
Allocation for predetermined maximum time taken by processes for
resources.
Note:- With these above situation system decide that which process
must be execute first and which one is last.
Example: (Resource: A,B,C)
Total A=10,B=5,C=7 (No. of Resources in System)
Process:- P
Let, A= CPU, B=Memory, C= Printer,
Allocation : means which resource allocates with which process.
Max need:- means how much resources are needed by process
Available: availability of resource (remaining resources after allocation)
(allocation- no. of res.)
Remaining Need: Formula :- Max Need – Allocation
(A,B,C )
P1 0, 1, 0 7 , 5, 3 3, 3 ,2 7, 4, 3
P2 2 , 0, 0 3,2,2 3, 3 ,2 +532 1, 2, 2
Now we need to check that system fulfill the requirement of any process by
reaming need column where all processes check the availability in a
particular sequence (by default started from P1).
In following Table Remaining need of process P1 is: 7, 4 ,3 but the available
resource are 3, 3 , 2 So P1 is not in Sequence of execution due to low
amount of resources .
Process Allocation Max Need Available Remaining
(A,B,C ) (A,B,C ) Need (A,B,C )
(Max need –
Allocation)
(A,B,C ) Unsafe
P1 0,1,0 7 , 5, 3 3, 3 ,2 7, 4, 3
Now next processes is P2
In following Table Remaining need of process P2 is: 1, 2, 2 and the
available resource are 3, 3 , 2 So P2 is in Sequence of execution (also
known as safe sequence) and release their all allocated resources after
the execution.
Process Allocation Max Need Available Remaining
(A,B,C ) (A,B,C ) Need (A,B,C )
(Max need –
Allocation)
P2 P2 Terminates and
available resources are: Safe
P1 0, 1, 0 7 , 5, 3 3, 3 ,2 + 2,0,0 7, 4, 3
(A,B,C )
=5,3,2
P2 2, 0, 0 3, 2, 2 3, 3 ,2 1,2,2
P3 3 , 0, 2 9 , 0, 2 6, 0,0
P4 2, 1, 1 4 , 2, 2 2, 1, 1
P5 0 , 0, 2 5, 3, 3 5, 3, 1
Total 7, 2, 5
Now next processes is P3,
In following Table Remaining need of process P3 is: 6, 0, 0 but the available
resource are 3, 2, 2 So P3 is not in Sequence of execution due to low
amount of resources and defined as unsafe state.
(A,B,C ) Unsafe
P3 3,0,2 9,0 ,2 5, 3 ,2 6, 0, 0
Now next processes is P4
In following Table Remaining need of process P4 is: 1, 2, 2 and the
available resource are 3, 3 , 2 So P2 is in Sequence of execution (also
known as safe sequence) and release their all allocated resources after
the execution.
Safe
(A,B,C )
Sequence of Safe execution :- P2
P4 2, 1, 1 4, 2, 2 5, 3 ,2 2,1,1
P4
P4 P4 Terminates and
available resources are:
P1 0, 1, 0 7 , 5, 3 3, 3 ,2 + 7, 4, 3
2,0,0
=5,3,2
P2 2,0,0 3,2,2 3,2,2 1,2,2
P3 3 , 0, 2 9 , 0, 2 5,3,2 6, 0,0
P4 2, 1, 1 4, 2, 2 5,3,2+ 2,1,1
2,1,1= 7,4,3
P5 Terminates and
P5 0,0,2 5,3,3 7,4,3+0,0,2= 5,3,1
available resources
7,4,5
are: 7,4,5
Now next processes is P5
In following Table Remaining need of process P5 is: 5, 3, 1 and the P2
available resource are 7, 4, 3 So P5 is successfully execute (also
known as safe sequence) and release their all allocated resources
after the execution. So the sequence of execution is:- P4
P5
Now next processes is P1
In following Table Remaining need of process P1 is: 7, 4, 3 and the
available resource are 7, 5, 5 So P5 is successfully execute (also
known as safe sequence) and release their all allocated resources
after the execution. So the sequence of execution is:-
Safe
Now Release All resources (i.e 7, 4, 5 + 0,1,0=7,5,5)
(A,B,C )
P1 0,1,0 7,5,3 7,4,5 7,4,3
Now next processes is P3
In following Table Remaining need of process P3 is: 6, 0, 0 and the
available resource are 7, 5, 5 So P3 is successfully execute (also
known as safe sequence) and release their all allocated resources
after the execution. So the sequence of execution is:-
Safe
Now Release All resources (i.e 7, 5, 5 +3,0,2=10,5,7)
(A,B,C )
P3 3, 0, 2 9, 0, 2 7,5,5 6,0,0
Sequence of execution is:
P2
P4
P5
P1
P3
Dead Lock Detection Algorithm:
If A System doesn’t employ either a dead lock avoidance or a deadlock
prevention then a dead lock situation may occur. In this situation a system
may provide:
•An algorithm that may examines the state of the system to determine
whether a deadlock has occurred.
•An algorithm to recover from deadlock.
•Deadlock detection algorithm employs a wait for graph (also known as
resource allocation graph)
•Deadlock only exist if and only if wait for graph contains a cycle
•Deadlock detection algorithm (DDA) checks the graph for a cycle.
•Deadlock detection algorithm exist for
a. Single Instance of resource Type (ex CPU)
b. Multiple Instance of resource Type (ex registers)
R1 P2
P3
R2
P1
R3
a. Single Instance of resource Type:
When all resource have only a single instance , deadlock detection algorithm
assign a wait for graph which is drawn from resource allocation graph.
Wait for graph:
• it can be obtained by removing the node of resource and collapsing the
appropriate edge. An edge PiPj in a wait for graph means process (Pi)
is waiting for process, (Pj) to release a resource that Pi needs .
• Deadlock exist if and only if wait for graph contains a cycle.
• Deadlock detection algorithm periodically check the graph for a cycle.
• An edge Pi to Pj exist if and only if resource allocation graph(RAG) (in
slide 6) contains two edge PiRq , Rq Pj for resource (Rq).
P5 P5
R1 R3 R4
P1 P2 P3
P1 P2 P3
P4
R2 P4 R5
P2 1 5,4
P3 2 4
p4 3 7
A= 10,5,8 4 , 3 3
(A,B,C )
P1 0, 1, 0 7 , 5, 3 3, 3 ,2 7,4,3
P2 2 , 0, 0 3,2,2 1,2,2
P3 3 , 0, 3 9 , 0, 4 6,0,1
P4P2 5,3,2,2,p41,– 1743, p1, =47, 52,3 2p3 , p5 2,1,1
P5 0 , 0, 2 5, 3, 3 5,3,1
Total 7, 2, 6
R1=3, R2 =2, R3 =2 , R4 =2
a r3
p1 p2
r4
b
R1=3, R2 =2, R3 =2 , R4 =2
P2 0,1,1,0 1,0,0,0
P3
p4