0% found this document useful (0 votes)
0 views20 pages

Deadlocks

The document discusses operating systems as resource managers, focusing on deadlocks, their conditions, and methods for prevention and recovery. It explains the concept of deadlocks using examples, including a bridge crossing scenario, and introduces resource allocation graphs for modeling. Additionally, it covers Dijkstra's Banker’s Algorithm for deadlock avoidance and provides various examples to illustrate its application.
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)
0 views20 pages

Deadlocks

The document discusses operating systems as resource managers, focusing on deadlocks, their conditions, and methods for prevention and recovery. It explains the concept of deadlocks using examples, including a bridge crossing scenario, and introduces resource allocation graphs for modeling. Additionally, it covers Dijkstra's Banker’s Algorithm for deadlock avoidance and provides various examples to illustrate its application.
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/ 20

08/11/2021

CpE 422: Operating Systems


Computer Engineering Department
College of Engineering

Operating Systems as Resource


Managers
 Example of resources
 Sharable VS non-sharable resources
 Preemptible VS non-preemptible resources
 Potential problem: Deadlocks

1
08/11/2021

What are deadlocks?


 A deadlock occurs when each process in a set of
processes is waiting for some event that only another
process in the set can cause. [after Tannenbaum]
 Example:
 P1: needs CD-ROM and sound card
 P1: asks for CD-ROM and receives it
 P2: needs CD-ROM and sound card
 P2: asks for sound card and gets it
 P1: asks for sound card ➔ blocks
 P2: asks for CD-ROM ➔ blocks

Bridge Crossing Example

 Traffic only in one direction.


 Each section of a bridge can be viewed as a resource.
 If a deadlock occurs, it can be resolved if one car backs
up (preempt resources and rollback).
 Several cars may have to be backed up if a deadlock
occurs.
 Starvation is possible.

2
08/11/2021

Conditions for Deadlocks


 Mutual Exclusion: only one process at a time can use a
resource.
 Hold-and-Wait: a process holding at least one resource is
waiting to acquire additional resources held by other
processes.
 No Preemption: a resource can be released only
voluntarily by the process holding it, after that process has
completed its task.
 Circular Wait: there exists a set {P0, P1, …, Pn} 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.

Digression: Directed Graphs


 Many areas of CS require us to think of objects and
relationships between them; e.g., paths between
locations, data dependencies, constraints in logic
puzzles
 Can represent this formally as a graph :
 Vertices (or nodes) represent the objects
 Edges (or arcs, or links) represent the relationships
 Sometimes, relationship is directional – think “one-
way streets”
 Now the edges have direction, and the graph is called a
directed graph or digraph

3
08/11/2021

Modeling Deadlocks as Digraphs


 Circles: processes
 Squares: resources
 Link from process → resource: process requests
resource
 Link from resource→ process: process has control of
resource
R1 R1

P1 P2

Resource Allocation Graph


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 P1 → Rj
 assignment edge – directed edge Rj → Pi

4
08/11/2021

Example of a Resource Allocation


Graph
R1 R3

P1 P1 P1

R2a

R2b

Resource Allocation Graph with


Deadlock
R1 R3

P1 P1 P1

R2a

R2b

5
08/11/2021

Basic Facts
 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.

What do we do about Deadlocks?


 Ignore them
 Detect them and (try to) recover
 Prevent them altogether
 Predict and avoid them

6
08/11/2021

Ignoring deadlocks:
The Ostrich Algorithm
 Sounds stupid, but...
 Consider:
 How often will a deadlock happen?
 How severe will it be if it does happen?
 How hard would it be to avoid/prevent/detect?

Deadlock detection/recovery
 Detection:
 Monitor resource allocation using (e.g.) a digraph
 If detect a cycle ➔ deadlock has occurred
 Recovery:
 Kill one of the processes
 If that doesn’t work: kill another, etc.
 Another alternative: just look for processes that have
been idle for a long time and kill them
 May be okay when aborting and restarting is okay
(e.g., batch jobs)

7
08/11/2021

Deadlock prevention
 Set things up so that deadlocks cannot occur at all
 Done by attacking one of the deadlock conditions
 Attacking mutual exclusion condition:
 Don’t let non-sharable resources be assigned to anyone
 E.g., spooling

Deadlock prevention
 Attacking hold-and-wait condition:
 Process can’t request a resource if holding any
 One way: processes request all resources up front
 Problem: may not know ahead of time what you need!
 Problem: hold resources too long in general
 Another approach: release all you’re holding
momentarily to request another
 Attacking no preemption condition: not realistic

8
08/11/2021

Deadlock prevention
 Attacking the circular wait condition:
 Stupid way: processes can only hold a single resource at
a time
 Better way:
 Number the resources
 Process can request whatever it wants, whenever it wants...as
long as the requests are in numerical order
 No cycles – why not?

Deadlock prevention
 Attacking the circular wait condition (cont’d):
 Resource allocation graph can’t have cycles in this
scheme – why not?
 Consider the case where process A holds resource i and B
holds j – deadlock only possible if A requests j and B requests i
 If i > j, then A can’t request j
 If j > i, then B can’t request i
 Problem – may not be able to find an ordering that
satisfies everyone!

9
08/11/2021

Deadlock avoidance
 Idea: predict when some action → deadlock, avoid it
 Dijkstra’s Banker’s Algorithm (single resource version)
 Modeled on the way a banker might deal with lines of credit
to customers
 Deadlock if there is no way to guarantee that all customers
can borrow up to their maximum resource limit
 Bank: let Bmax be maximum amount of money bank has to
loan
 Each customer: let Ci be maximum amount he/she can
max
borrow
 Banker can’t let C  Bmax
imax

Dijkstra’s Banker’s Algorithm


 Safety:
A state is safe if some sequence of other possible states
exists that allows all customers (processes) to get up to
their maximum resource limit at some time
 Keep track of Ci and current allocation for each
max
customer
 Start in a safe state
 When process requests additional amount of resource,
make sure that next state will also be safe
 If so, allow request, else disallow it

10
08/11/2021

Banker’s Algorithm Example


 Initial state:
Process Current Maximum
A 0 7
B 0 3
C 0 2
D 0 4
Remaining: 8

Banker’s Algorithm Example1


 Initial state:
Process Current Maximum
A 3 7
B 2 3
C 0 2
D 2 4
Remaining: 1
 Safe or not?

11
08/11/2021

Banker’s Algorithm Example1


 Initial state:
Process Current Maximum
A 3 7
B 2 3
C 0 2
D 2 4
Remaining: 1
 Safe or not?
 Safe
 Possible sequence of processes running to
 completion: B → D → C → A

Banker’s Algorithm Example1


 Initial state:
Process Current Maximum
A 3 7
B 3 3
C 0 2
D 2 4
Remaining: 0
 Safe or not?
 Safe
 Possible sequence of processes running to
 completion: B → D → C → A

12
08/11/2021

Banker’s Algorithm Example1


 Initial state:
Process Current Maximum
A 3 7
B - -
C 0 2
D 2 4
Remaining: 3
 Safe or not?
 Safe
 Possible sequence of processes running to
 completion: B → D → C → A

Banker’s Algorithm Example1


 Initial state:
Process Current Maximum
A 3 7
B - -
C 0 2
D 4 4
Remaining: 1
 Safe or not?
 Safe
 Possible sequence of processes running to
 completion: B → D → C → A

13
08/11/2021

Banker’s Algorithm Example1


 Initial state:
Process Current Maximum
A 3 7
B - -
C 0 2
D - -
Remaining: 5
 Safe or not?
 Safe
 Possible sequence of processes running to
 completion: B → D → C → A

Banker’s Algorithm Example1


 Initial state:
Process Current Maximum
A 3 7
B - -
C 2 2
D - -
Remaining: 3
 Safe or not?
 Safe
 Possible sequence of processes running to
 completion: B → D → C → A

14
08/11/2021

Banker’s Algorithm Example1


 Initial state:
Process Current Maximum
A 3 7
B - -
C - -
D - -
Remaining: 5
 Safe or not?
 Safe
 Possible sequence of processes running to
 completion: B → D → C → A

Banker’s Algorithm Example1


 Initial state:
Process Current Maximum
A 7 7
B - -
C - -
D - -
Remaining: 1
 Safe or not?
 Safe
 Possible sequence of processes running to
 completion: B → D → C → A

15
08/11/2021

Banker’s Algorithm Example1


 Initial state:
Process Current Maximum
A - -
B - -
C - -
D - -
Remaining: 8
 Safe or not?
 Safe
 Possible sequence of processes running to
 completion: B → D → C → A

Banker’s Algorithm Example2


Proc Curr Max Proc Curr Max

A 3 7 A 3 7

B 2 3 B 3 3
B wants 1
C 0 2 C 0 2

D 2 4 D 2 4

Remaining: 1 Remaining: 0

Allow the request?

16
08/11/2021

Banker’s Algorithm Example2


Proc Curr Max Proc Curr Max

A 3 7 A 3 7

B 2 3 B 3 3
B wants 1
C 0 2 C 0 2

D 2 4 D 2 4

Remaining: 1 Remaining: 0

Allow the request?


• Yes
•Possible sequence of processes running to completion:
B→D→C→A

Banker’s Algorithm Example3


Proc Curr Max Proc Curr Max

A 6 7 A 7 7

B 0 3 B 0 3
A wants 1
C 0 2 C 0 2

D 1 4 D 1 4

Remaining: 1 Remaining: 0

Allow the request?

17
08/11/2021

Banker’s Algorithm Example3


Proc Curr Max Proc Curr Max

A 6 7 A 7 7

B 0 3 B 0 3
A wants 1
C 0 2 C 0 2

D 1 4 D 1 4

Remaining: 1 Remaining: 0

Allow the request?


• Yes
•Possible sequence of processes running to completion:
A→B→C→D

Banker’s Algorithm Example4


Proc Curr Max Proc Curr Max

A 4 7 A 4 7

B 1 3 B 1 3
D wants 1
C 0 2 C 0 2

D 1 4 D 2 4

Remaining: 2 Remaining: 1

Allow the request?

18
08/11/2021

Banker’s Algorithm Example4


Proc Curr Max Proc Curr Max

A 4 7 A 4 7

B 1 3 B 1 3
D wants 1
C 0 2 C 0 2

D 1 4 D 2 4

Remaining: 2 Remaining: 1

Allow the request?


• NO!
•No sequence possible where they all can finish

Banker’s Algorithm: Critique


 Is it too strong?
 After all – no guarantee that in the previous
 example:

D might not give back 1 immediately, moving back


into safe state
 But we’re interested in guarantee that there will be no
deadlock, so this is what we need.

19
08/11/2021

Banker’s Algorithm: Critique


 Scales to multiple processes/resources
 Problems:
 Need to know maximum resources needed per process –
often (usually?) impossible for multiprocess system
 Number of processes constantly changes
 Resources can disappear
 But: Really no better general-purpose algorithm exists
for this

Summary
 Handling deadlocks is difficult
 No best general solution
 How you choose to handle it depends on your
situation: trade-offs

20

You might also like