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

Deadlocks Slides

The document discusses different approaches to handling deadlocks in an operating system, including prevention, avoidance, and detection/recovery. Deadlock prevention imposes constraints on resource requests to preclude necessary conditions for deadlocks. Deadlock avoidance uses algorithms like the Banker's algorithm to ensure resource allocation states remain safe. Deadlock detection periodically checks for deadlocks and chooses a process to release resources from if detected. However, in reality resources are increasingly shareable, so livelock is more common than deadlock and systems typically restart processes if performance degrades severely.

Uploaded by

SACHIN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Deadlocks Slides

The document discusses different approaches to handling deadlocks in an operating system, including prevention, avoidance, and detection/recovery. Deadlock prevention imposes constraints on resource requests to preclude necessary conditions for deadlocks. Deadlock avoidance uses algorithms like the Banker's algorithm to ensure resource allocation states remain safe. Deadlock detection periodically checks for deadlocks and chooses a process to release resources from if detected. However, in reality resources are increasingly shareable, so livelock is more common than deadlock and systems typically restart processes if performance degrades severely.

Uploaded by

SACHIN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Operating Systems:

Deadlocks

Shankar

April 14, 2021


Outline overview

1. Deadlocks Overview

2. Deadlock Prevention

3. Deadlock Avoidance

4. Deadlock Detection & Recovery

5. Handling Deadlocks in Reality


What are Deadlocks overview

Deadlock: Set of processes P1 , · · · , PN deadlocked i

every Pi is blocked, and


every Pi is waiting for an event doable only by some Pj
// event: release, signal, V, interrupt enable, ...

Deadlock freedom: desired property of multi-threaded programs

ensuring this is hard for a general multi-threaded program


but easier for a resource-manager system examined next

Aside: Livelock is deadlock without blocking

processes are in fruitless loops


harder to detect (unless loops are very localized)
deadlock can be livelock at a lower (spin-lock) level
Resource Manager System overview

System = resource manager + user processes


processes: request resources, get them, release them
RES : set of all resources, initially held by manager
alloc(p): resources currently held by (user process) p
avail : resources currently held by manager
Function req(p, res): request by p for resources res
call only if res + alloc(p) ⊆ RES
blocking call
p gets res at return; happens only if res ⊆ avail
Function rel(p, res): release by p of res
call only if res ⊆ alloc(p)
nonblocking

System can deadlock without further constraints

3 approaches: prevention, avoidance, detection/recovery


Outline prevention

1. Deadlocks Overview

2. Deadlock Prevention

3. Deadlock Avoidance

4. Deadlock Detection & Recovery

5. Handling Deadlocks in Reality


Deadlock Prevention Approach prevention

Impose further constraints on req calls to preclude deadlock

no further constraints on req returns


Step 1: identify a necessary condition for deadlock, eg:

resource that is non-shareable and non-preemptable


process holds a resource and requests more resources
cycle of processes: each requesting a resource held by the next

Step 2: constrain req calls to preclude a necessary condition

Henceforth assume non-shareable/non-preemptable resources

Examples of deadlock prevention rules

req(p, res) alloc(p) is empty


can be called only when

Impose a total ordering on all resources in RES


req(p, res) can be called only when res > max(alloc(p))
Outline avoidance

1. Deadlocks Overview

2. Deadlock Prevention

3. Deadlock Avoidance

4. Deadlock Detection & Recovery

5. Handling Deadlocks in Reality


Deadlock Avoidance Approach avoidance

Deadlock avoidance:

impose further constraints on req returns to preclude deadlock

so req(p, res) return may wait even if res ⊆ avail


may also involve weak constraints on req calls
eg, limit on total resources that a process can hold

can allow more parallelism than deadlock prevention


burden is on manager (unlike deadlock prevention)

Classical deadlock avoidance solution uses the Banker's


algorithm
Deadlock Avoidance Solution  1 avoidance

Resources: organized into types 1, · · · ,M


Tot = [Tot1 , · · · , TotM ] // total # of each resource type

Processes: 1, · · · ,N
Maxi : [Maxi,1 , · · · Maxi,M ] // max total need of process i

Variables

alloci : [alloci,1 , · · · , alloci,M ] // resources held by process i


avail : [avail1 , · · · , availM ] // resources held by manager
reqi : [reqi,1 , · · · , reqi,M ] // process i 's ongoing request
needi : Maxi − alloci // process i 's max possible request
Deadlock Avoidance Solution  2 avoidance

Assumption: If a process i always gets the resources it asks for, it


eventually releases all its resources

So if needi ≤ avail and the manager grants only requests of i,


then it eventually gets alloci back

A state is safe i it has a safe sequence

A safe sequence is a permutation i1 , · · · , iN of process ids s.t.

needi1 ≤ avail
needi2 ≤ avail + alloci1
...
neediN ≤ avail + alloci1 + · · · + allociN−1

A safe state is not deadlocked and cannot lead to a deadlock


Deadlock Avoidance Solution  2 avoidance

Banker's Algorithm: determines whether or not a state is safe

Variables
xavail ← avail // temporary avail
done[i] ← false, for i = 1, · · · , N // true i i accounted for

While (there is an i s.t.


done[i] = false and needi ≤ xavail )
xavail ← xavail + alloci
done[i] ← true
Safe i done[i] = true for every i

Return req(p, res) only if the resulting state would be safe,


ie, apply Banker's algorithm to the current state with

avail decreased by res


alloci increased by res
Banker's Algorithm Example avoidance

5 processes, 3 resource types

Tot : [10 5 7]

State

Max alloc
P1 7 5 3 0 1 0
P2 3 2 2 2 0 0
P3 9 0 2 3 0 2
P4 2 2 2 2 1 1
P5 4 3 3 0 0 2

Safe?
Banker's Algorithm Example avoidance

5 processes, 3 resource types

Tot : [10 5 7]

State

Max alloc need


P1 7 5 3 0 1 0 7 4 3
P2 3 2 2 2 0 0 1 2 2
P3 9 0 2 3 0 2 6 0 0
P4 2 2 2 2 1 1 0 1 1
P5 4 3 3 0 0 2 4 3 1

Safe?
Banker's Algorithm Example avoidance

5 processes, 3 resource types

Tot : [10 5 7]

State

Max alloc need avail


P1 7 5 3 0 1 0 7 4 3 3 3 2
P2 3 2 2 2 0 0 1 2 2
P3 9 0 2 3 0 2 6 0 0
P4 2 2 2 2 1 1 0 1 1
P5 4 3 3 0 0 2 4 3 1

Safe?
Banker's Algorithm Example avoidance

5 processes, 3 resource types

Tot : [10 5 7]

State

Max alloc need done avail


P1 7 5 3 0 1 0 7 4 3 3 3 2
P2 3 2 2 2 0 0 1 2 2 P2 5 3 2
P3 9 0 2 3 0 2 6 0 0 P4 7 4 3
P4 2 2 2 2 1 1 0 1 1 P5 7 4 5
P5 4 3 3 0 0 2 4 3 1 P1 7 5 5
P3 10 5 7

Safe? Yes. Safe sequence: P2, P4, P1, P3


Outline detection-recovery

1. Deadlocks Overview

2. Deadlock Prevention

3. Deadlock Avoidance

4. Deadlock Detection & Recovery

5. Handling Deadlocks in Reality


Deadlock Detection and Recovery detection-recovery

Do not constrain req calls or returns

Instead periodically check for deadlock.


If yes, choose a process i and forceably release alloci

Deadlock detection algorithm for M resource types


// variation of Baker's algorithm

Variables
xavail ← avail // temporary avail
done[i] ← false, for i = 1, · · · , N // true i i accounted for

While (there is an i s.t.


done[i] = false and reqi ≤ xavail )
xavail ← xavail + alloci
done[i] ← true
If done[i] = true for every i, then no deadlock.
Otherwise, processes whose done is false are in a deadlock.
Outline reality

1. Deadlocks Overview

2. Deadlock Prevention

3. Deadlock Avoidance

4. Deadlock Detection & Recovery

5. Handling Deadlocks in Reality


What happens in real-life reality

Resources are increasingly shareable

disks (vs tapes)


demand-paging (vs entire process space in physical memory)
virtualization of everything

Hence livelock (or thrashing) is more common than deadlock

Hence deadlock prevention/avoidance/detection is rarely used

Instead, if system appears to be in deadlock (or livelock),


kill and/or restart processes or entire system

You might also like