Deadlocks: Understanding and Prevention in Computer Science, Study notes of Computer Science

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

Pre 2010

Uploaded on 09/02/2009

koofers-user-t9o-1
koofers-user-t9o-1 🇺🇸

9 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 3
Chapter 3: Deadlocks
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download Deadlocks: Understanding and Prevention in Computer Science and more Study notes Computer Science in PDF only on Docsity!

Chapter 3

Chapter 3: Deadlocks

Chapter 3

CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)

Overview

„^

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)

When do deadlocks happen?

„^

Suppose^ „^

Process 1 holds resource Aand requests resource B „^

Process 2 holds B andrequests A „^

Both can be blocked, withneither able to proceed „^

Deadlocks occur when …^ „^

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

DEADLOCK!

Chapter 3

CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)

Using resources

„^

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)

Four conditions for deadlock

„^

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 allocation graphs

„^

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)

Getting into deadlock

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)

Not getting into deadlock…

„^

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)

Detecting deadlocks using graphs

„^

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

U

V

G

S

W

F

V

T

E

S,T

U

D

S

C

T

B

S

R

A

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)

Deadlock detection algorithm

„^

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 (

N ,L)

} Ifno deadl

ock r

epor

ted by now ,t

her

e isn’

t

any def

ine Tr

aver

se (

C ,L) {

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 (

A,L)

Chapter 3

CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)

Deadlock detection algorithm

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)

Recovering from deadlock

„^

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)

Safe and unsafe states

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)