






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A chapter from cs 1550 course at the university of pittsburgh, discussing deadlocks in computer science. It covers the concept of resources, why deadlocks occur, dealing with deadlocks through various methods, and prevention strategies. Resources include printers, semaphores, tables, and processes, which need to be accessed in a reasonable order to avoid deadlocks.
Typology: Study notes
1 / 30
This page cannot be seen from the preview
Don't miss anything!























Chapter 3
Chapter 3
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Resources
Why do deadlocks occur?
Dealing with deadlocks^ Ignoring them: ostrich algorithm Detecting & recovering from deadlock Avoiding deadlock Preventing deadlock
Chapter 3
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
^
Process 1 holds resource Aand requests resource B ^
Process 2 holds B andrequests A ^
Both can be blocked, withneither able to proceed ^
Processes are grantedexclusive access to devices orsoftware constructs(resources) ^
Each deadlocked processneeds a resource held byanother deadlocked process
A
B
B
A
Process 1
Process 2
Chapter 3
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Sequence of events required to use a resource^ Request the resource Use the resource Release the resource
Can
’t use the resource if request is denied ^
Requesting process has options^
Block and wait for resource Continue (if possible) without it: may be able to use an alternateresource Process fails with error code ^
Some of these may be able to prevent deadlock
…
Chapter 3
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Mutual exclusion^ Each resource is assigned to at most one process
Hold and wait^ A process holding resources can request more resources
No preemption^ Previously granted resources cannot be forcibly takenaway
Circular wait^ There must be a circular chain of 2 or more processeswhere each is waiting for a resource held by the nextmember of the chain
Chapter 3
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
^
Resource allocationmodeled by directed graphs ^
Example 1:^ ^
Resource R assigned toprocess A ^
Example 2:^ ^
Process B is requesting /waiting for resource S ^
Example 3:^ ^
Process C holds T, waitingfor U ^
Process D holds U, waitingfor T ^
C and D are in deadlock!
A R
B S
T U
D
C
Chapter 3
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
A^
B^
C
Acquire RAcquire SRelease RRelease S
Acquire SAcquire TRelease SRelease T
Acquire TAcquire RRelease TRelease R
A R
B S
C T
Acquire R
A R
B S
C T
Acquire S
A R
B S
C T
Acquire T
A R
B S
C T
Acquire S
A R
B S
C T
Acquire T
A R
B S
C T
Deadlock! Acquire R
Chapter 3
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Many situations
may
result in deadlock (but don
’t
have to)^ In previous example, A could release R before C requestsR, resulting in no deadlock Can we always get out of it this way?
Find ways to:^ Detect deadlock and reverse it Stop it from happening in the first place
Chapter 3
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
^
Process holdings and requests in the table and in the graph(they
’re equivalent)
^
Graph contains a cycle => deadlock!^ ^
Easy to pick out by looking at it (in this case) ^
Need to mechanically detect deadlock ^
Not all processes are deadlocked (A, C, F not in deadlock)
R^
A S F W
C
Wants Holds
Process
E
D G
B T
V
U
Chapter 3
14
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
^
General idea: try to findcycles in the resourceallocation graph ^
Algorithm: depth-firstsearch at each node^ ^
Mark arcs as they
’re
traversed ^
Build list of visited nodes ^
If node to be added is alreadyon the list, a cycle exists! ^
Cycle == deadlock
Foreach node N i
n the gr
aph {
SetL = em pt
y li st
unm ar
k al
lar
cs
Traver
se (
} Ifno deadl
ock r
epor
ted by now ,t
her
e isn’
t
any def
ine Tr
aver
se (
IfC i
n L,r
epor
tdeadl
ock!
Add C t
o L Foreach unm ar
ked ar
c fr
om C {
M ar
k the ar
c
SetA = ar
c dest
inat
ion
/*N O TE:L i
s a
localvar
iabl
e */
Traver
se (
Chapter 3
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
1 0 3 2
Avail
D C B A
D C B A
Process
D C B A
Process Hold Want
cur
rent
=avai
l;
for(
j= 0;j< N ;j
for(
k=0;k<N ;k++){ if(f
inished[
k])
cont
inue; if(w ant
[k]< cur
rent
fini shed[
k]= 1; cur
rent+= hol
d[k]
break;} if(k==N ){print
f“D eadl
ock!
\n”;
//fi
nished[
k]==0 m eans pr
ocess i
s in
//the deadl
ock
break;} } Note: want[j],hold[j],current,avail are arrays!
Chapter 3
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
^
Recovery through preemption^ ^
Take a resource from some other process ^
Depends on nature of the resource and the process ^
Recovery through rollback^ ^
Checkpoint a process periodically ^
Use this saved state to restart the process if it is found deadlocked ^
May present a problem if the process affects lots of
“external
”^ things
^
Recovery through killing processes^ ^
Crudest but simplest way to break a deadlock: kill one of theprocesses in the deadlock cycle ^
Other processes can get its resources ^
Preferably, choose a process that can be rerun from the beginning^
Pick one that hasn
’t run too far already
Chapter 3
19
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Free: 3
7 2 C
4 2 B
9 3 A
Max Has
Free: 1
7 2 C
4 4 B
9 3 A
Max Has
Free: 5
7 2 C
0 B
9 3 A
Max Has
Free: 0
7 7 C
0 B
9 3 A
Max Has
Free: 7
0 C
0 B
9 3 A
Max Has
Demonstration that the first state is safe
Free: 3
7 2 C
4 2 B
9 3 A
Max Has
Free: 2
7 2 C
4 2 B
9 4 A
Max Has
Free: 0
7 2 C
4 4 B
9 4 A
Max Has
Free: 4
7 2 C
0 B
9 4 A
Max Has
Demonstration that the second state is unsafe
Chapter 3
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Banker's Algorithm for a single resource
4 0 C^ Free: 10
7 0 D
5 0 B
6 0 A
Max Has
4 2 C
Free: 2
7 4 D
5 1 B
6 1 A
Max Has
4 2 C
Free: 1
7 4 D
5 2 B
6 1 A
Max Has
^
Bankers
’^ algorithm: before granting a request, ensure that a
sequence exists that will allow all processes to complete^ ^
Use previous methods to find such a sequence ^
If a sequence exists, allow the requests ^
If there
’s no such sequence, deny the request
^
Can be slow: must be done on each request! Any sequence finishes
C,B,A,D finishes
Deadlock (unsafe state)