Chapter 3 - Old PPT - Deadlock

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 40

Chapter 3:

Distributed Deadlock Detection


By;
Mrs.Rutuja S.Kadam
Assistant Professor
CS/IT Deptt
SIT
Deadlock :
• Definition :
Two or more processes are said to be in a
deadlock if they wait for the happening of an event
which would never happen.
OR
A set of processes is said to be in a deadlock when
every process in the set waits for an event that can
only be caused by another process in the set.

Ex : Single lane bridge.


P1
• Process
P2

• Resources R1

R2

• P1 requires R1 and R2 to complete its


execution.
• P2 requirtes R1 and R2 for execution
Deadlock Situation

Process 1 is holding Resource 1 and waiting for resource 2 which is acquired


by process 2, and process 2 is waiting for resource 1.
Necessary conditions for deadlock
• Deadlock can arise if four conditions hold simultaneously

 Mutual exclusion:
only one process at a time can use a resource. ( Resources : not in shared mode )

 Hold and wait:


a process holding resource(s) is waiting to acquire additional resources held by other
processes.

 No preemption:
a resource can be released only voluntarily by the process holding it upon its task
completion.

 Circular wait:
there exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting for a
resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is
waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held
by P0.
System Model
• Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices

• Each resource type Ri has Wi instances.

• Each process utilizes a resource as follows:


– request
– use
– release
Resource Allocation Graph :
 It represents Process Resource interaction in
distributed system.
 If cycle or knot present in resource alloction graph
 system is in deadlock.

 Wait For Graphs(WFG) :


 It represents system state.
 If cycle or knot present in WFG system is in
deadlock.
Resource Allocation Graph & wait for Graph

• If all resources have only a single instance then we can define a deadlock detection
algorithm that uses a variant of resource-allocation graph, called a wait for graph.

P5 P5
R1 R3 R4

P2 P3 P1 P2 P3
P1

P4 P4

R2 R5

Resource allocation graph Corresponding wait for Graph


Resource Allocation Graph With A Cycle
But No Deadlock
• If graph contains no cycles ⇒ no
deadlock.

• If graph contains a cycle ⇒


– if only one instance per resource
type, then deadlock.

– if several instances per resource


type, possibility of deadlock.
Two types of deadlocks
• Resource deadlock: uses AND condition.
AND condition: a process that requires resources
for execution can proceed when it has acquired all
those resources.

• Communication deadlock: uses OR condition.


OR condition: a process that requires resources for
execution can proceed when it has acquired at
least one of those resources.
Deadlock conditions
• The condition for deadlock in a system using the
AND condition is the existence of a cycle.
 A cycle is sufficient to declare a deadlock with this model.

• The condition for deadlock in a system using the


OR condition is the existence of a knot.
 Knot: Subset of a graph such that starting from any node in the
subset, it is impossible to leave the knot by following the edges
of the graph.
• Deadlock in the AND model; there is a cycle but
no knot.
• No Deadlock in the OR model
P1 P2 P1

P1 P1 P1
• Deadlock in both the AND model and the OR
model;
• there are cycles and a knot

P1 P2 P5

P4 P3 P6
Deadlock Handling Strategies
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection
Deadlock Prevention
• We can prevent Deadlock by eliminating any of the
four necessary conditions :
1. Eliminate Mutual Exclusion :
 Resouces are sharable and non sharable.
 Sharable resources(files) do not require mutually
exclusive access, so not involved in deadlock.
 Prb is with non sharable resources (TD,printer) ,they
require ME access.
Therefore it is not possible eliminate Mutual
Exclusion.
2. Eliminate Hold and wait

 In order to ensure hold and wait condition never


holds in the system : 2 Protocols can ne used

1. Allocate all required resources to the process before


start of its execution, this way hold and wait condition is
eliminated but it will lead to low device utilization.
Ex : Consider a process which copies from a card reader to a disk
file, sorts the disk file and then prints the results to a line printer and
copies them to a magnetic tape.
- If all the resources to be requested at the beginning of the
process, then the process must initially request the card reader,disk
file,line printer and tape drive.
- It will hold the TD for its entire execution even though it needs
at the end.
2. Requesting resources only when it has none.
Process will make new request for resources after
releasing the current set of resources.
Ex : Processes initially request only the card reader and
disk file. It copies from the card reader to the disk, and then
releases both the card reader and the disk file. The process
must then re-request the disk file and the line printer.
After copying the disk file to the line printer, it releases both
and then requests the disk file and tape drive. It copies the
disk file to tape then releases these two resources and
terminates.
Drawback : Starvation as process might have to wait for
the resource indefinitely as the resource it requires is always
allocated to some other process.
3.Eliminate No Preemption
• 3rd necessary condition is no pre-emption of
resources that have already been allocated.
• To ensure this condition does not hold : we use
following protocol :
- forcing a process waiting for a resource that
cannot immediately be allocated to pre-empt
(release) all of its currently held resources, so that
other processes may use them to finish.
4. Eliminate Circular Wait :
- One way to achieve this is to force a process to hold only one resource at a time. If it requires
another resource, it must first give up the one that is held by it and then request for another.

- If a process P1 holds R1 and wants R2, it must give up R1 first because another process P2
should be able to get it(R1).

- We face with a problem of assigning a TD to P2 after P1 has processed only half the records.
- Therefore this in unacceptable solution.
- Better solution : Each resource will be assigned with a numerical number. A process can
request for the resources only in increasing order of numbering.
Ex : 1≡Card reader
2≡Printer
3≡Plotter
4≡Tape drive
5≡Card punch
• Now the rule is this: processes can request resources whenever they want to, but all requests
must be made in numerical order. A process may request first printer and then a tape drive
(order: 2, 4), but it may not request first a plotter and then a printer (order: 3, 2). The problem
with this strategy is that it may be impossible to find an ordering that satisfies everyone.
Deadlock Avoidance :
• In this approach, resource is allocated to the process
if the resulting global system state is in safe state.
• Global state includes all processes and resources of
DS
• Safe state : if the system can allocate all resources
requested by all processes without entering a
deadlock state.
Because of the following problems, deadlock
avoidance is impractical in DS
1. Every site maintains global state of the system huge
storage requirements and extensive comm costs(every
change must be communicated to every site)

2. Due to large no. of processes and resources it becomes


expensive to check for a safe state.

So it becomes impractical
Deadlock Detection :
• Here we need to examine process resource
interaction graph (RAG) to find cycles.
• DD in DS has 2 conditions :
1. Once a cycle is formed in WFG, it persists untill it
is detected and broken
2. Cycle detection can proceed concurrently with
normal system activities.
Therefore it does not have –ve effect on sys
throughput.
Hence we will focus on Deadlock Detection
methods in this chapter.
Issues in Deadlock Detection and Resolution

1. Detection of existing deadlocks


2. Resolution of detected deadlocks.

1. Detection : 2 issues

Search of WFG for the presence


Maintenance of WFG of cycles or knot

 In DS, a cycle may involve several sites, so the


search for cycles depend upon how the WFG of the
system is represented across the system.
• Depending upon how the WFG information is
maintained and search for cycles and knot is
carried out, deadlock detection algorithms are
 Centralised
 Distributed
 Hierarchical
A correct deadlock detetcion Algo must satisfy 2
conditions :
1.1. Progress – No undetected deadlocks.
 The algorithm must detect all existing deadlocks in finite
time.
 Once a deadlock has occurred, the deadlock detection activity
should continously progress until the deadlock is detected.
1.2. Safety – No False Deadlocks
 The algorithm should not report deadlocks which are non
existent( phantom deadlocks).
 In DS, no global mem, comm occurs by msg passing :
difficult to design correct DD algo coz site may obtain out of
date and inconsistent WFG.
 As a result site may detect a cycle that doesn’t exist.
2. Resolution :
 Involves breaking existing wait for dependencies in
the system WFG to resolve the deadlock.

 involves rolling back one or more processes that


are deadlocked and assigning their resources to
blocked processes in the deadlock so that they can
resume execution.
Control Organization for Distributed
Deadlock Detection Algorithms
• Algorithms for detecting distributed deadlock can
be handled in three different ways:

 Centralized
 Distributed
 Hierarchical
Centralized Control
• One designated site (control site) has the responsibility of
constructing a global WFG and searchs for cycles.
• All decisions are made by the central control site.
• It must maintain the global WFG constantly
or
Periodically – message can be sent to coordinator each
time an arc is added or deleted.

Merits :
1. Simple and easy to implement
2. Deadlock resolution is simple – control site has complete
information about deadlock cycle and hence can resolve
• Suppose machine A has a process P0 which holds
resource S and wants resource R.
• Resource R is held by P1.
• This local graph is maintained on machine A.
• Suppose that another machine B, has a process P2, which
is holding resource T and wants resource S.
• Both of these machines send their graphs to the
coordinator, which maintains the union (overall graph).
• The coordinator sees no cycles. Therefore there are no
deadlocks.
• If a cycle was found (hence a deadlock), the coordinator
would have to make a decision on which machine to
notify for killing a process to break the deadlock
• Suppose two events occur:
• process P1 releases resource R and asks machine B for resource T.
• Two messages are sent to the coordinator:
 message 1 (from machine A): releasing R
 message 2 (from machine B): waiting for T
 This should cause no problems (no deadlock) as no cycles will exist.
 However, suppose message 2 arrives first. The coordinator would then
construct the graph shown and detect a deadlock (cycle).
 This condition is known as a false deadlock.
 A way to fix this is to use Lamport’s algorithm to impose global
time ordering on all machines.
 Alternatively, if the coordinator suspects deadlock, it can send a
reliable message to every machine asking whether it has any release
messages. Each machine will then respond with either a release
message or a message indicating that it is not releasing any resources.
Demerits :
• There is one, single point of failure(control site).

• There can be a communication


bottleneck(congestion) around the site due to all the
WFG information messages.
Ho-Ramamoorthy Algorithm: Two-Phase
• 2 centralised deadlock detection algorithms :
 2 Phase Algorithm
1 Phase Algorithm
 Ho-Ramamoorthy Algorithm: Two-Phase

• Each site maintains status table with status of all


local processes at that particular site.
• Status of process includes all resources locked and all
resources being waited upon
• control site (designated site ) periodically requests
status from all sites, builds WFG, and checks for
cycles (deadlock).if no cycle – no deadlock
• if deadlock detected, control site requests status tales
from all the sites and again constrict a WFG using
only those transactions comman to both reports(but
throws out transactions that have changed)
• If the same cycle is detected again, the system is in deadlock
Que:- Why to select comman transactions found in 2
consecutive reports?
Ans :– to get consistent view of the system.
• Consitent view : if it reflects a correct state of the
system.
• If deadlock exist : there could be same wait for
condition in both reports.
• A cycle in wait for condition comman to both report
does not imply deadlock.
• Therefore 2 phase algo may report FALSE
deadlock.
• By getting 2 consecutive reports : it reduces
probability of getting inconsistent view however
One Phase Algorithm :
• Requires only one status report from each site
• Each site maintains two tables : resource status table and
process status table.
• Resource status table : keeps track of transactions that have
locked or are waiting for the resources stored at that site.
• Process status table : keeps track of resources locked by or
waited by the transactions at that site.
• Periodically, a designated site requests both the tables from
every site, constructs a WFG using only those transactions
for which the entry in the resource table matches the
corresponding entry in process table and searches WFG for
cycle.
• If no cycle -> no deadlock.
Merits :
• Faster
• Requires fewer msgs as compared to 2 phase algo.

De-Merits :
1. More storage : as each site maintains 2 status
tables
One Phase Algorithm(contd)
• It does not detect false deadlocks as it eliminates the
inconsistency in state info by using only info that is
comman to both tables.
• This eliminates inconsistency.
• Ex : If resource table at site S1 indicates that resource
R1 is waited upon by process p2(i.e R1 P2) and the
process table at site S2 indicated that process P2 is
waiting for resource R1 i.e( P2 R1) then edge P2R1
in the constructed WFG reflects the correct system state.
• If either of the entries is missing from resource or
process table , then a request or release message or
release message from S2 to S1 is in transit and
• P2  R1 cannot be ascertained.

You might also like