0% found this document useful (0 votes)
26 views

Deadlock

The document discusses different aspects of deadlocks including: 1. Deadlock prevention methods like denying mutual exclusion of resources or ensuring at least one of the four conditions for deadlock is not met. 2. Deadlock avoidance techniques where the system has information on future resource needs and denies requests that could lead to a deadlock. 3. Detecting deadlocks using a resource allocation graph to identify cycles indicating a deadlock, and recovery methods to resolve deadlocks once detected.

Uploaded by

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

Deadlock

The document discusses different aspects of deadlocks including: 1. Deadlock prevention methods like denying mutual exclusion of resources or ensuring at least one of the four conditions for deadlock is not met. 2. Deadlock avoidance techniques where the system has information on future resource needs and denies requests that could lead to a deadlock. 3. Detecting deadlocks using a resource allocation graph to identify cycles indicating a deadlock, and recovery methods to resolve deadlocks once detected.

Uploaded by

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

Deadlocks

Content
• Principles of deadlock,

• Deadlock Prevention,

• Deadlock Avoidance,

• Deadlock Detection,

• Deadlock Recovery.
Potential Deadlock

I need quad I need quad


C and B B and C

I need quad
I need quad A and B
D and A
Actual Deadlock

HALT until D HALT until C


is free is free

HALT until B
HALT until A is free
is free
DEADLOCKS
■ Permanent blocking of a single or set of processes, competing
for system resources or may want to cooperate for
communication.
■ Formal definition :
A set of processes is deadlocked if each process in the set is
waiting for an event that only another process in the set can
cause.
■ Usually the event is release of a currently held resource.
■ Generally it is because of the conflicting needs of different
processes.
■ There is no general solution to solve it completely.
Resource Categories

• Reusable resources vs. Consumable resources


• Physical vs. logical resources
• Preemptable resources vs. Non preemptable
resources
Reusable Resources
• Can be safely used by only one process at a time and is not depleted by
that use

• Processors, I/O channels, memory, devices and data structure such as


database, files,semaphores etc.

• Examples of deadlock with reusable resources

– If each process holds on resource and requests for the other

– Dining Philosophers

• General access pattern:

– Request

– Lock

– Use

– Release
Resources
• Process must wait if request is denied
❑ Requesting process may be blocked

❑ May fail with error code

• Deadlocks

❑ Occur only when processes are granted

exclusive access to resources


Consumable Resources
• One that can be created/produced and
destroyed/consumed
• Typically no limit on the number of consumable
resources of a particular type
• Resource ceases to exist after consumption
• Examples
– Interrupts, signals, messages, contents of I/O buffers
• Can there be deadlock involving consumable resources?
Other Kinds
• Physical
– Printer, tape drive
• Logical
– File, semaphore, data structure
• Preemptable
– Can be taken away from process for some time with no
ill effects
– CPU,memory
• Non Preemptable
– Will cause process to fail if taken away
– Printer
• Resource type (e.g. Printer) vs resource instances (e.g. 2)
Deadlock Example

• utility program A
deadlock
❑ Copies a file from a tape to
disk
❑ Prints the file to a printer
• Resources
❑ Tape
❑ Disk
❑ Printer
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
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
1. Mutual exclusion :Only single process is allowed to use the
resource.
2. Hold and wait :Process holding at least one resource and
waiting to acquire additional resources currently held by other
processes.
3. No preemption :No resource can be removed forcibly from a
process.
4. Circular wait: A closed chain of processes exists, such that
each process holds at least one resource needed by the next
process in the chain
▪ First three are necessary but not sufficient conditions for a
deadlock to exist
Resource-Allocation Graph
Resource-Allocation Graph

■ Characterizes allocation of resources to processes.

■ Directed graph to describe deadlocks

■ A set of vertices V and a set of edges E.

■ V is partitioned into two types:

❑ P = {P1, P2, …, Pn}, the set consisting of all the


processes in the system.

❑ R = {R1, R2, …, Rm}, the set consisting of all resource


types in the system.

■ request edge – directed edge Pi --


Rj

■ assignment edge – directed edge Rj --- Pi


Resource-Allocation Graph (Symbols)

• Process node
Pi

• Resource node Rj

• Assignment edge

• Request edge
Example of a Resource allocation graph
And this?

Yes deadlock
… and this?

Cycle but no deadlock


Basic Facts
▪ Deadlock cycle
▪ No cycle no deadlock
▪ Cycle deadlock
▪ if only one instance per resource type
▪ if several instances per resource type then there is
a possibility of a deadlock
▪ If there is single instance of each resource type then
cycle in the RAG is necessary and sufficient condition
for the existence of a deadlock
▪ If each resource type has multiple instances ,then a
cycle is a necessary but not sufficient condition for the
existence of a deadlock
Dealing with deadlocks
• Four general approaches
– Deadlock prevention – by adopting a policy that
eliminates one of the four conditions
– Deadlock avoidance – by making the appropriate
dynamic choices based on the current state of
resource allocation
– Deadlock detection and recovery – attempt to detect
presence of deadlock and take actions to recover
– Ignore the problem and pretend that deadlocks never
occur in the system
• Used by most operating systems, including UNIX
& windows
• Remains a programmer’s responsibility to write
deadlock free code
Deadlock prevention

• Design a system in such a way that the possibility of


deadlock is excluded by ensuring that at least one of
the necessary conditions cannot hold
• Two main methods
1. Indirect – prevent all three of the necessary conditions
occurring at once (Mutual exclusion,
Hold-wait,No-preemption)
2. Direct – prevent Circular wait
Deadlock Prevention: Deny Mutual Exclusion
■ Mutual Exclusion – not required for sharable resources;
must hold for non-sharable resources.
A process never needs to wait for a sharable resource
But in reality, some resources are intrinsically
non-sharable and hence we cannot prevent deadlock by
denying mutual exclusion
If access to a resource requires mutual exclusion, then
mutual exclusion must be supported by the OS.
Some resources, such as files, may allow multiple
accesses for reads but only exclusive access for writes.
Even in this case, deadlock can occur if more than one
process requires write permission.
Deadlock Avoidance
■ Deadlock prevention leads to inefficient use of resources &
execution of processes.
■ Requires that OS be given in advance additional
information concerning which resources a process will
request and use during its lifetime
■ Based on this info, OS decides whether to grant the request
or delay it
■ System must consider resources currently available,
resources currently allocated, future requests and releases
Deadlock Avoidance
■ With this knowledge of complete sequence of requests and
releases for each process, system can decide for each
request whether or not the process should wait in order to
avoid a future deadlock
■ Variations in algorithms differ in amount and type of
information required
■ Simplest: each process declares the max number of
resources of each type that it may need
■ Dynamically examines the resource allocation state to ensure
that a circular wait condition can never exist
■ State: number of available and allocated resources, maximum
demands of processes
Safe State
■ When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe
state.

■ System is in safe state if there exists a safe sequence of all


processes.

■ Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources
that Pi can still request can be satisfied by currently available
resources + resources held by all the Pj, with j<i.
❑ If Pi resource needs are not immediately available, then Pi
can wait until all Pj have finished.
❑ When Pj is finished, Pi can obtain needed resources,
execute, return allocated resources, and terminate.
❑ When Pi terminates, Pi+1 can obtain its needed resources,
and so on.
Safe, Unsafe , Deadlock State

• Safe state is not a deadlocked


state
• Deadlocked state is an unsafe
state
• Not all unsafe states are
deadlocks
• Unsafe state may lead to a
deadlock
• Better to always be in safe
state
Example
• 12 magnetic tape drives, 3 processes

• Snapshot at t0

Process Maximum needs Currently


holding
P0 10 5
P1 4 2
P2 9 2
• 3 more tape drives available

• System is in safe state with sequence P1, P0, P2


From safe to unsafe state
• Suppose at t1, P2 requests and is allocated 1 more tape drive

Process Maximum Currently


needs holding
P0 10 5
P1 4 2
P2 9 3

• System is no longer in safe state


– Only P1 can be allocated now. When done, both P0 and P2 will have to
wait forever!
• If only we had made P2 wait until either of the other processes had
finished, we could have avoided the deadlock
bottom line of avoidance algorithms
• Grant request only if the system remains in the safe
state
– Even if the resources it is asking for is currently
available
Avoidance algorithms

• When single instance of all resource types use RAG


algorithm
– Uses a variant of the RAG we saw earlier
• When multiple instances of resource types,
- Use Banker’s algorithm
Resource-Allocation Graph Scheme
■ Claim edge Pi → Rj indicated that process Pi may request
resource Rj at some time in future

■ Similar to request edge in direction but is represented by a


dashed line

■ when a process requests a resource, Claim edge converts to


request edge

■ Request edge converted to an assignment edge when the


resource is allocated to the process.

■ When a resource is released by a process, assignment edge


reconverted to a claim edge

■ Resources must be claimed a prior in the system


Resource-Allocation Graph
• Snapshot at time t
• Suppose P2 requests R2
• Although R2 is currently free
it cannot be allocated to P2
– Cycle!
Resource-Allocation Graph Algorithm
■ Suppose that process Pi requests a resource Rj

■ The request can be granted only if converting the request edge


to an assignment edge does not result in the formation of a
cycle in the resource allocation graph.

■ Time complexity: For detecting a cycle in a graph it requires an


order of n2 operations, Where n in number of processes in the
system.
Banker’s Algorithm
■ Multiple instances of each resource type.
■ Each process must a prior claim maximum use.
■ Every process declares it maximum need/requirement.
■ Maximum requirement should not exceed the total number of
resources in the system.
■ When a process requests a resource, system determines whether
allocation will keep the system in safe state or not.
■ If it is, resources get allocated. Otherwise need to wait until
resources get available.
■ When a process gets all its resources it must return them in a finite
amount of time.
Data Structures for the Banker’s Algorithm
Following relationship holds
Safety Algorithm Requires m * n2 operation to
decide whether a state is safe.
Example
• 5 processes P0 through P4
• 3 resource types: A (10 instances), B (5 instances), and
C (7 instances)
• Snapshot of system at t0

Proc Allocation Max Need


ess

A B C A B C A B C
P0 0 1 0 7 5 3
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
Example

• 5 processes P0 through P4
• 3 resource types: A (10 instances), B (5 instances),
and C (7 instances)
• Snapshot of system at t0 : is it safe?
Proc Allocation Max Need
ess

A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3
P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
Safe State?

Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 3 3 2

P0 0 1 0 7 4 3 F
P1 2 0 0 1 2 2 F
P2 3 0 2 6 0 0 F
P3 2 1 1 0 1 1 F
P4 0 0 2 4 3 1 F
Safe State?

Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 3 3 2

P0 0 1 0 7 4 3 F F 5 3 2

P1 2 0 0 1 2 2 F T
P2 3 0 2 6 0 0 F F
P3 2 1 1 0 1 1 F F
P4 0 0 2 4 3 1 F F
Safe State?

Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 3 3 2

P0 0 1 0 7 4 3 F F F 5 3 2
7 4 3
P1 2 0 0 1 2 2 F T T
P2 3 0 2 6 0 0 F F F
P3 2 1 1 0 1 1 F F T
P4 0 0 2 4 3 1 F F F
Safe State?

Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 3 3 2

P0 0 1 0 7 4 3 F F F T 5 3 2
7 4 3
P1 2 0 0 1 2 2 F T T T
7 5 3
P2 3 0 2 6 0 0 F F F F
P3 2 1 1 0 1 1 F F T T
P4 0 0 2 4 3 1 F F F F
Safe State?

Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 3 3 2

P0 0 1 0 7 4 3 F F F T T 5 3 2
7 4 3
P1 2 0 0 1 2 2 F T T T T
7 5 3
P2 3 0 2 6 0 0 F F F F T
10 5 5
P3 2 1 1 0 1 1 F F T T T
P4 0 0 2 4 3 1 F F F F F
Safe State?

Pr Allocation Need Fi Fi Fi Fi Fi Fi
oc ni ni ni ni ni ni
es s s sh sh sh sh Work
s h h
3 3 2
A B C A B C
5 3 2
P0 0 1 0 7 4 3 F F F T T T
7 4 3
P1 2 0 0 1 2 2 F T T T T T
7 5 3
P2 3 0 2 6 0 0 F F F F T T 10 5 5
P3 2 1 1 0 1 1 F F T T T T 10 5 7
P4 0 0 2 4 3 1 F F F F F T

Safe Sequence: <P1, P3, P0, P2, P4>


Resource-Request Algorithm
Example: P1 Request (1,0,2)
■ Check that Request ≤ need that is, (1,0,2) ≤ (1,2,2) ⇒ true

■ Check that Request ≤ Available that is, (1,0,2) ≤ (3,3,2) ⇒ true

Allocation Need Available


ABC ABC ABC
P0 0 1 0 743 230
P1 302 020
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431
■ Can request for (3,3,0) by P4 be granted?

■ Can request for (0,2,0) by P0 be granted?


Request1 = (1, 0, 2)

• Request <need that is, (1,0,2) < (1,2,2) ->true


• Request1 <= Available
– (1, 0, 2) <= (3, 3, 2)
• Pretend to grant
– Allocation1 , Need1 , Available will change (2, 3,
0)
Proc Allocation Max Need
ess

A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
Is the state safe?

Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 2 3 0

P0 0 1 0 7 4 3 F
P1 3 0 2 0 2 0 F
P2 3 0 2 6 0 0 F
P3 2 1 1 0 1 1 F
P4 0 0 2 4 3 1 F
Is the state safe?

Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 2 3 0

P0 0 1 0 7 4 3 F F 5 3 2

P1 3 0 2 0 2 0 F T
P2 3 0 2 6 0 0 F F
P3 2 1 1 0 1 1 F F
P4 0 0 2 4 3 1 F F
Is the state safe?

Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 2 3 0

P0 0 1 0 7 4 3 F F F 5 3 2
7 4 3
P1 3 0 2 0 2 0 F T T
P2 3 0 2 6 0 0 F F F
P3 2 1 1 0 1 1 F F T
P4 0 0 2 4 3 1 F F F
Is the state safe?

Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 2 3 0

P0 0 1 0 7 4 3 F F F T 5 3 2
7 4 3
P1 3 0 2 0 2 0 F T T T
7 5 3
P2 3 0 2 6 0 0 F F F F
P3 2 1 1 0 1 1 F F T T
P4 0 0 2 4 3 1 F F F F
Is the state safe?

Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 2 3 0

P0 0 1 0 7 4 3 F F F T T 5 3 2
7 4 3
P1 3 0 2 0 2 0 F T T T T
7 5 3
P2 3 0 2 6 0 0 F F F F T
10 5 5
P3 2 1 1 0 1 1 F F T T T
P4 0 0 2 4 3 1 F F F F F
Is the state safe?

Pr Allocation Need Fi Fi Fi Fi Fi Fi
oc ni ni ni ni ni ni
es s s sh sh sh sh Work
s h h
2 3 0
A B C A B C
5 3 2
P0 0 1 0 7 4 3 F F F T T T
7 4 3
P1 3 0 2 0 2 0 F T T T T T
7 5 3
P2 3 0 2 6 0 0 F F F F T T 10 5 5
P3 2 1 1 0 1 1 F F T T T T 10 5 7
P4 0 0 2 4 3 1 F F F F F T

Safe Sequence: <P1, P3, P0, P2, P4>


Request4 = (3, 3, 0)

• Request4 <= Available?


– (3, 3, 0) <= (2, 3, 0)?
– No. Thus request from P4 cannot be granted
Request0 = (0, 2, 0)

• Request0 <= Available


– (0, 2, 0) <= (2, 3, 0)
• Pretend to grant
– Allocation0 , Need0 , Available will change (2, 1, 0)

Proc Allocation Max Need


ess

A B C A B C A B C
P0 0 3 0 7 5 3 7 2 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
Is the state safe?

Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 2 1 0

P0 0 3 0 7 2 3 F
P1 3 0 2 0 2 0 F
P2 3 0 2 6 0 0 F
P3 2 1 1 0 1 1 F
P4 0 0 2 4 3 1 F
Is the state safe?

Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 2 1 0

P0 0 3 0 7 2 3 F
P1 3 0 2 0 2 0 F
P2 3 0 2 6 0 0 F
P3 2 1 1 0 1 1 F
P4 0 0 2 4 3 1 F
Example : Is the below system in safe state?

Pr Allocation Max
oc
es
s
A B C D A B C D Available
P0 0 0 1 2 0 0 1 2 1 5 2 0
P1 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
Deadlock Detection

• When neither prevention nor avoidance is


employed, a deadlock situation may arise
• System can provide an algorithm to detect an
algorithm and an algorithm to recover from it if it has
occurred
• Requires overhead that includes not only the
run-time costs of maintaining the necessary
information and executing the detection algorithm
but also the potential losses inherent in recovering
from a deadlock
Detection-Algorithm Usage
■ When, and how often, to invoke algorithm depends on:
■ How often a deadlock is likely to occur?
■ How many processes will be affected by deadlock when it happens?
■ If deadlock occurs frequently, then the detection algorithm should be
invoked frequently.
■ We could invoke the deadlock detection algorithm every time a request for
allocation cannot be granted immediately.
■ By this we can identify deadlock causing process & processes involved in
deadlock also.

■ But this incurs overhead in computation time.


■ So can call algorithms after 1 hour / CPU utilization drops below 40%.
■ If detection algorithm is invoked arbitrarily, there may be many cycles in the
resource graph and so we would not be able to tell which of the many
deadlocked processes “caused” the deadlock.
Deadlock Recovery

• Once detected, several alternatives are


available for recovery inform the operator
that a deadlock has occurred and to let the
operator deal with the deadlock manually
• Abort one or more processes to break the
circular wait
• Preempt some resources from one or
more of the deadlocked processes
Process Termination / Abort
• Two alternatives
• In both, system reclaims all resources held by the terminated
process
• Abort all deadlocked processes
– Everything the processes had done so far has gone down the
drain!
• Abort one process at a time until the deadlock cycle is
eliminated
– Whose turn is next?
• Policy decision similar to scheduling decisions
– Considerable overhead of detecting deadlock after each
termination!
• Aborting comes with several issues
– What if in the middle of updating a file or printing to a printer
Whom to abort next?
• Abort those processes whose termination will incur the minimum
cost

• Cost depends upon

– What is the priority of the process?

– How long the process has computed and how much longer the
process will compute before completing its designated task?

– How many and what type of resources the process has used
(e.g., preemptable, non-preemptable)?

– How many more resources the process needs in order to


complete?

– How many processes will need to be terminated?

– Whether the process is interactive or batch?


Resource Preemption

• We successively preempt some resources


from processes and give these resources to
other processes until the deadlock cycle is
broken
• Three issues need to be addressed:
– Selecting a victim
– Rollback
– Starvation
Selecting a victim

• Which resources and of which processes


next in order to minimize cost
Rollback

• What to do with the process from whom


resource has been preempted? It cannot
continue normal execution
• Rollback to some safe state and restart it
from that state
– What is safe state, also system will require to
keep states of all processes
– Thus some systems prefer total rollback
Starvation

• How can we guarantee that resources will


not always be preempted from the same
process
• If decision to pick is primarily based on
cost factors, same unfortunate fellow may
get picked up every time!
• We thus need an upper bound (small and
finite) on how many times you can be
chosen as a victim
– Include the number of rollbacks in the cost
factor
Summary of Deadlock prevention, Avoidance,Detection

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy