0% found this document useful (0 votes)
14 views36 pages

Dead Lock

1) Deadlocks occur when processes are waiting for resources held by other waiting processes, resulting in a circular wait where no process can continue. 2) There are four conditions required for a deadlock to occur: mutual exclusion, hold and wait, no preemption, and circular wait. 3) Methods for handling deadlocks include prevention, avoidance, detection, and recovery. Prevention ensures that at least one of the four conditions is never satisfied to avoid deadlocks altogether.

Uploaded by

anuradha Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views36 pages

Dead Lock

1) Deadlocks occur when processes are waiting for resources held by other waiting processes, resulting in a circular wait where no process can continue. 2) There are four conditions required for a deadlock to occur: mutual exclusion, hold and wait, no preemption, and circular wait. 3) Methods for handling deadlocks include prevention, avoidance, detection, and recovery. Prevention ensures that at least one of the four conditions is never satisfied to avoid deadlocks altogether.

Uploaded by

anuradha Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

DEAD LOCK

•Introduction

•Request

•Release

•Wait

•Resource allocation graph


•Deadlock Prevention

•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

Instance Of Resources( Number of Resources)

Vertices are the process to resource or resource to process


Edge are from resource to process or process to resource
Ri Pi (Allocated Edge) or assignment edge
Pi Ri (Requested Edge)

R1P1
P1R2
P1 R1

P3

R2
P2

R3

Dead Lock represented by Directed Graph known as Resource


allocation graph.
 This Graph consist a set of number of vertices and edge.
The Set of vertices are partitioned into two types:-
 Nodes of process consisting of all active process in the system
Node of resource consisting of all resource type in the system
R1 R2

P1 P2 P3

R3

R1, R2, R3(ASS) R1, R2,R3 (Req)


p1 0,0,1 1,0,0
P2 111 0,0,0
p3 0,1,0 0,0,1
A directed edge from Ri  Pj signifies that an instance of resource
(Ri) is allocated to the process (Pj). It is known as assignment edge.
 A directed graph from Pi  Rj means Process(pi) is requesting a
resource (Rj).
 If the graph contain no cycle , then no process in the system is in
dead lock.
If the graph contains a cycle, then a dead lock may or may not be
exist.
If each resource type has a single instance, then cycle implies that
dead lock has occurred.
If each resource type has multiple instance , then the cycle in the
graph is necessary , not the sufficient condition for the existence of a
dead lock
Dead Lock Characterization
a dead lock situation can arise if four condition hold simultaneously in
system:
1. Mutual Exclusion:
• In the case of ME , at least one resource must be held in non-
sharable mode.
• Only one process can use the resource if another process request
that resource
• The requesting process must be delayed until the resource has been
released .

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

P1 hold the resource R1 and wait for R2 at that time

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

P1 hold the resource of P2 and wait for the resource


held by P4 at that time
4. No Pre-Emption
• resource cant be preempted, that is resource can be released only
voluntarily by the process holding it, after that process has completed its
task or the condition says once a process has a resource , it will not be
able to forced to give it up .

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.

Hold and Wait:


To ensure that hold and wait condition doesn’t hold in the system, we
must ensure that whenever a process request for a resource, it doesn’t
hold any other resource. The protocol used for this is each process to
request and be allocated all the resources before it begins its execution.
Another protocol used here is the process requests resource only when it
has no resource. The demerit of the system is:-
1. Resource utilization is very low
2. Starvation may occur.
No-Pre-emption:
To ensure that no-premption condition doesn’t hold we can use the following
protocol:-
•If the process is holding some resource and request another resource that
can not be immediately allocated to it, then all resources the process is
currently holding are pre-empted that is resource are implicitly released.
The process will restart only when it can regain its old resources as well as
the new resource that is requesting.
•Another protocol is if a process request some resource, we first check
whether they are available or not, if they are available allocate them . If
not, we check that they are allocated to some other process , that is waiting
for additional resource, if so , we pre-empt the desired resource from
waiting process and allocate them to requesting process.
•Circular wait:
Dead Lock Avoidance
A deadlock-avoidance algorithm dynamically examines the resource-
allocation state to ensure that a circular wait condition can never exist. It
defines the behaviour of two state:-
Circular Wait
unsafe- Deadlock (Cycle)
Safe-may or may not be in dead lock
• This is the forth and final condition of dead lock situation.
•One way to ensure that this condition never ease to impose a total ordering
of all resource types and to require that each process requests a resource in
the increasing order of enumeration .
•Example
•If there are 3 resources in the system, such as tape drive , disk drive and
printer. We assign an unique integer to each resource type which allows us
to compare 2 resource and determine whether one proceed another in our
ordering . Each process can request a resource only in an increasing order of
enumeration.
•Safe:
A state is safe if the system can allocate resources to each process (up to its
maximum) in some order and still avoid a deadlock
•Unsafe:
But If no such determined sequence are exist, then the system state is said
to be unsafe.
NOTE:
A safe state is not a deadlocked state. Conversely, a deadlocked state is
an unsafe state. Not all unsafe states are deadlocks, however an unsafe state
may lead to a deadlock. As long as the state is safe, the operating system
can avoid unsafe (and deadlocked) states. In an unsafe state,
the operating system cannot prevent processes from requesting resources
such that a deadlock occurs: The behaviour of the processes controls unsafe
states.
Unsafe
Deadlock

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

Process Allocation Max Need Total Remaining


(A,B,C ) Available Need (A,B,C )
(A,B,C ) (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.

Process Allocation Max Need Available Remaining


(A,B,C ) (A,B,C ) Need (A,B,C )
(Max need –
Allocation)

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

Process Allocation Max Need Available Remaining


(A,B,C ) (A,B,C ) Need (A,B,C )
(Max need –
Allocation)

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:-

Process Allocation Max Need Available Remaining


(A,B,C ) (A,B,C ) Need (A,B,C )
(Max need –
Allocation)

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:-

Process Allocation Max Need Available Remaining


(A,B,C ) (A,B,C ) Need (A,B,C )
(Max need –
Allocation)

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 PiPj 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 PiRq , Rq Pj for resource (Rq).
P5 P5

R1 R3 R4

P1 P2 P3

P1 P2 P3

P4
R2 P4 R5

Resource Allocation Graph Wait For Graph

R1,r2,r3,r4r5 All Req


P1 0,1,0,0,0 1, 0,0,0,0
P2
p3
p4
p5
b. Multiple Instance of resource Type:
Wait for graph is not applicable to the graph with multiple instance of each
resource. For that it will use Banker’s algorithm to detect where the
deadlock exist.
Recovery
When Deadlock detection algorithm examines the deadlock several
possibilities exists
•To inform the operator that a deadlock has occurred
•Lead operators to deal with deadlock manually.
•Let the system recover from deadlock automatically
There are two option for breaking a deadlock cycle:
•To abort one or more processes.(process termination)
•To break the circular wait (Process Termination).
•To pre-empt some resources from one or more deadlock processes
(resource termination).
CPU= 8

Process Used res Max res


P1 2 6

P2 1 5,4
P3 2 4
p4 3 7
A= 10,5,8 4 , 3 3

Process Allocation Max Need Total Remaining


(A,B,C ) Available Need (A,B,C )
(A,B,C ) (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 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

Process Used res REQ


P1 1,1,0, 1 2,0,0,1

P2 0,1,1,0 1,0,0,0
P3
p4

You might also like