Operating Systems Slides 5 - Deadlock

32
Deadlocks Wang Xiaolin June 19, 2013 [email protected] 1/1

description

Handouts version: http://cs2.swfu.edu.cn/~wx672/lecture_notes/os/slides/deadlock-a.pdf

Transcript of Operating Systems Slides 5 - Deadlock

Page 1: Operating Systems Slides 5 - Deadlock

Deadlocks

Wang Xiaolin

June 19, 2013

) [email protected]

1 /1

Page 2: Operating Systems Slides 5 - Deadlock

A Major Class of Deadlocks Involve Resources

Processes need access to resources in reasonable order.Examples of computer resources..

......

▶ printers▶ memory space▶ data (e.g. a locked record in a DB)▶ semaphores

.Suppose.....

......

▶ a process holds resource A and requests resource B. Atsame time,

▶ another process holds B and requests ABoth are blocked and remain so

2 /1

Page 3: Operating Systems Slides 5 - Deadlock

Resourcestypedef int semaphore;

semaphore resource31; semaphore resource31;semaphore resource32; semaphore resource32;

void process3A(void) { void process3A(void) {down(&resource 31); down(&resource 31);down(&resource 32); down(&resource 32);use3both3resources( ); use3both3resources( );up(&resource32); up(&resource32);up(&resource31); up(&resource31);

} }

void process3B(void) { void process3B(void) {down(&resource 31); down(&resource 32);down(&resource 32); down(&resource 31);use3both3resources( ); use3both3resources( );up(&resource32); up(&resource31);up(&resource31); up(&resource32);

} }

(a) (b)

Fig. 3-2. (a) Deadlock-free code. (b) Code with a potentialdeadlock.

..

Deadlock!

3 /1

Page 4: Operating Systems Slides 5 - Deadlock

Resources

.Deadlocks occur when .....

......

processes are granted exclusive access to resourcese.g. devices, data records, files, ...

Preemptable resources can be taken away from a processwith no ill effectse.g. memory

Nonpreemptable resources will cause the process to fail iftaken awaye.g. CD recorder

In general, deadlocks involve nonpreemptable resources

4 /1

Page 5: Operating Systems Slides 5 - Deadlock

Resources

.Sequence of events required to use a resource..

......

request à use à releaseopen() close()allocate() free()

.What if request is denied?..

......

Requesting process▶ may be blocked▶ may fail with error code

5 /1

Page 6: Operating Systems Slides 5 - Deadlock

The Best Illustration of a Deadlock

.A law passed by the Kansas legislature early inthe 20th century..

......

“When two trains approach each other at a crossing, bothshall come to a full stop and neither shall start up againuntil the other has gone.”

6 /1

Page 7: Operating Systems Slides 5 - Deadlock

Introduction to Deadlocks

.Deadlock..

......

A set of processes is deadlocked if each process in the set iswaiting for an event that only another process in the set cancause.

▶ Usually the event is release of a currently held resource▶ None of the processes can ...

▶ run▶ release resources▶ be awakened

7 /1

Page 8: Operating Systems Slides 5 - Deadlock

Four Conditions For Deadlocks

Mutual exclusion condition each resource can only beassigned to one process or is available

Hold and wait condition process holding resources canrequest additional

No preemption condition previously granted resourcescannot forcibly taken away

Circular wait condition▶ must be a circular chain of 2 or moreprocesses

▶ each is waiting for resource held by nextmember of the chain

8 /1

Page 9: Operating Systems Slides 5 - Deadlock

Four Conditions For Deadlocks

.Unlocking a deadlock is to answer 4 questions:..

......

1. Can a resource be assigned to more than one process atonce?

2. Can a process hold a resource and ask for another?3. can resources be preempted?4. Can circular waits exits?

9 /1

Page 10: Operating Systems Slides 5 - Deadlock

Resource-Allocation Graph

..

(a) (b) (c)

T U

D

C

S

B

A

R

Fig. 3-3. Resource allocation graphs. (a) Holding a resource.(b) Requesting a resource. (c) Deadlock.

.

has

.

wants

.Strategies for dealing with Deadlocks..

......

1. detection and recovery2. dynamic avoidance — careful resource allocation3. prevention — negating one of the four necessary

conditions4. just ignore the problem altogether

10 /1

Page 11: Operating Systems Slides 5 - Deadlock

Resource-Allocation Graph

11 /1

Page 12: Operating Systems Slides 5 - Deadlock

Resource-Allocation Graph

.Basic facts:..

......

▶ No cycles à no deadlock▶ If graph contains a cycle à

▶ if only one instance per resourcetype, then deadlock

▶ if several instances per resourcetype, possibility of deadlock

12 /1

Page 13: Operating Systems Slides 5 - Deadlock

Deadlock Detection

▶ Allow system to enter deadlock state▶ Detection algorithm▶ Recovery scheme

13 /1

Page 14: Operating Systems Slides 5 - Deadlock

Deadlock DetectionSingle Instance of Each Resource Type — Wait-for Graph

▶ Pi → Pj if Pi is waiting for Pj;▶ Periodically invoke an algorithm that searches for acycle in the graph. If there is a cycle, there exists adeadlock.

▶ An algorithm to detect a cycle in a graph requires anorder of n2 operations, where n is the number ofvertices in the graph.

14 /1

Page 15: Operating Systems Slides 5 - Deadlock

Deadlock DetectionSeveral Instances of a Resource Type

Tape

driv

es

Plotte

rs

Scann

ers

CD Rom

s

E = ( 4 2 3 1 )

Tape

driv

es

Plotte

rs

Scann

ers

CD Rom

s

A = ( 2 1 0 0 )

Current allocation matrix

020

001

102

010

Request matrix

212

001

010

100

R =C =

Fig. 3-7. An example for the deadlock detection algorithm.Row n:C: current allocation to process nR: current requirement of process nColumn m:C: current allocation of resource class mR: current requirement of resource class m

15 /1

Page 16: Operating Systems Slides 5 - Deadlock

Deadlock DetectionSeveral Instances of a Resource Type

Resources in existence(E1, E2, E3, …, Em)

Current allocation matrix

C11C21

Cn1

C12C22

Cn2

C13C23

Cn3

C1mC2m

Cnm

Row n is current allocationto process n

Resources available(A1, A2, A3, …, Am)

Request matrix

R11R21

Rn1

R12R22

Rn2

R13R23

Rn3

R1mR2m

Rnm

Row 2 is what process 2 needs

Fig. 3-6. The four data structures needed by the deadlock detectionalgorithm.

n∑i=1

Cij + Aj = Ej

e.g.(C13 +C23 + . . . +Cn3) + A3 = E3

16 /1

Page 17: Operating Systems Slides 5 - Deadlock

Deadlock DetectionSeveral Instances of a Resource Type

.Maths recall: vectors comparison..

......

For two vectors, X and Y

X ≤ Y iff Xi ≤ Yi for 0 ≤ i ≤ m

e.g. [1 2 3 4

]≤

[2 3 4 4

][1 2 3 4

]≰

[2 3 2 4

]

17 /1

Page 18: Operating Systems Slides 5 - Deadlock

Deadlock DetectionSeveral Instances of a Resource Type

Tape

driv

es

Plotte

rs

Scann

ers

CD Rom

s

E = ( 4 2 3 1 )

Tape

driv

es

Plotte

rs

Scann

ers

CD Rom

s

A = ( 2 1 0 0 )

Current allocation matrix

020

001

102

010

Request matrix

212

001

010

100

R =C =

Fig. 3-7. An example for the deadlock detection algorithm.

..

A R

.

(2 1 0 0) ≥ R3, (2 1 0 0)

.

(2 2 2 0) ≥ R2, (1 0 1 0)

.

(4 2 2 1) ≥ R1, (2 0 0 1)

18 /1

Page 19: Operating Systems Slides 5 - Deadlock

Deadlock DetectionSeveral Instances of a Resource Type

Tape

driv

es

Plotte

rs

Scann

ers

CD Rom

s

E = ( 4 2 3 1 )

Tape

driv

es

Plotte

rs

Scann

ers

CD Rom

s

A = ( 2 1 0 0 )

Current allocation matrix

020

001

102

010

Request matrix

212

001

010

100

R =C =

Fig. 3-7. An example for the deadlock detection algorithm...

A R

.

(2 1 0 0) ≥ R3, (2 1 0 0)

.

(2 2 2 0) ≥ R2, (1 0 1 0)

.

(4 2 2 1) ≥ R1, (2 0 0 1)

18 /1

Page 20: Operating Systems Slides 5 - Deadlock

Deadlock DetectionSeveral Instances of a Resource Type

Tape

driv

es

Plotte

rs

Scann

ers

CD Rom

s

E = ( 4 2 3 1 )

Tape

driv

es

Plotte

rs

Scann

ers

CD Rom

s

A = ( 2 1 0 0 )

Current allocation matrix

020

001

102

010

Request matrix

212

001

010

100

R =C =

Fig. 3-7. An example for the deadlock detection algorithm...

A R

.

(2 1 0 0) ≥ R3, (2 1 0 0)

.

(2 2 2 0) ≥ R2, (1 0 1 0)

.

(4 2 2 1) ≥ R1, (2 0 0 1)

18 /1

Page 21: Operating Systems Slides 5 - Deadlock

Deadlock DetectionSeveral Instances of a Resource Type

Tape

driv

es

Plotte

rs

Scann

ers

CD Rom

s

E = ( 4 2 3 1 )

Tape

driv

es

Plotte

rs

Scann

ers

CD Rom

s

A = ( 2 1 0 0 )

Current allocation matrix

020

001

102

010

Request matrix

212

001

010

100

R =C =

Fig. 3-7. An example for the deadlock detection algorithm...

A R

.

(2 1 0 0) ≥ R3, (2 1 0 0)

.

(2 2 2 0) ≥ R2, (1 0 1 0)

.

(4 2 2 1) ≥ R1, (2 0 0 1)

18 /1

Page 22: Operating Systems Slides 5 - Deadlock

Recovery From Deadlock

▶ Recovery through preemption▶ take a resource from some other process▶ depends on nature of the resource

▶ Recovery through rollback▶ checkpoint a process periodically▶ use this saved state▶ restart the process if it is found deadlocked

▶ Recovery through killing processes

19 /1

Page 23: Operating Systems Slides 5 - Deadlock

Deadlock AvoidanceResource Trajectories

..

Plotter

Printer

Printer

Plotter

B

A

u (Both processesfinished)

p q

r

s

t

I8

I7

I6

I5

I4I3I2I1

���� ���

.

dead

.

zone

..

Unsafe region

▶ B is requesting a resource at point t. The system mustdecide whether to grant it or not.

▶ Deadlock is unavoidable if you get into unsafe region.20 /1

Page 24: Operating Systems Slides 5 - Deadlock

Deadlock AvoidanceSafe and Unsafe States

Assuming E = 10,

.Unsafe..

......

..

A

B

C

3

2

2

9

4

7

Free: 3(a)

A

B

C

4

2

2

9

4

7

Free: 2(b)

A

B

C

4

4 —4

2

9

7

Free: 0(c)

A

B

C

4

2

9

7

Free: 4(d)

Has Max Has Max Has Max Has Max

Fig. 3-10. Demonstration that the state in (b) is not safe.

.

unsafe

.Safe..

......

A

B

C

3

2

2

9

4

7

Free: 3(a)

A

B

C

3

4

2

9

4

7

Free: 1(b)

A

B

C

3

0 ––

2

9

7

Free: 5(c)

A

B

C

3

0

7

9

7

Free: 0(d)

A

B

C

3

0

0

9

Free: 7(e)

Has Max Has Max Has Max Has Max Has Max

Fig. 3-9. Demonstration that the state in (a) is safe.

21 /1

Page 25: Operating Systems Slides 5 - Deadlock

Deadlock AvoidanceThe Banker’s Algorithm for a Single Resource

The banker’s algorithm considers each request as it occurs,and sees if granting it leads to a safe state.

..

A

B

C

D

0

0

0

0

6

Has Max

5

4

7

Free: 10

A

B

C

D

1

1

2

4

6

Has Max

5

4

7

Free: 2

A

B

C

D

1

2

2

4

6

Has Max

5

4

7

Free: 1

(a) (b) (c)

Fig. 3-11. Three resource allocation states: (a) Safe. (b) Safe.(c) Unsafe.

.

unsafe!

unsafe ̸= deadlock

22 /1

Page 26: Operating Systems Slides 5 - Deadlock

Deadlock AvoidanceThe Banker’s Algorithm for Multiple Resources

Proce

ssTa

pe d

rives

Plotte

rsA

B

C

D

E

3

0

1

1

0

0

1

1

1

0

1

0

1

0

0

1

0

0

1

0

E = (6342)P = (5322)A = (1020)

Resources assignedPro

cess

Tape

driv

esPlo

tters

A

B

C

D

E

1

0

3

0

2

1

1

1

0

1

0

1

0

1

1

0

2

0

0

0

Resources still needed

Scann

ers

CD RO

Ms

Scann

ers

CD RO

Ms

Fig. 3-12.Thebanker’salgorithmwith multiple resources.D → A → B,C,ED → E → A → B,C

23 /1

Page 27: Operating Systems Slides 5 - Deadlock

Deadlock AvoidanceMission Impossible

.In practice..

......

▶ processes rarely know in advance their max futureresource needs;

▶ the number of processes is not fixed;▶ the number of available resources is not fixed.

Conclusion: Deadlock avoidance is essentially a missionimpossible.

24 /1

Page 28: Operating Systems Slides 5 - Deadlock

Deadlock Prevention— Break The Four Conditions

.Attacking the Mutual Exclusion Condition..

......

▶ For example, using a printing daemon to avoid exclusiveaccess to a printer.

▶ Not always possible▶ Not required for sharable resources;▶ must hold for nonsharable resources.

▶ The best we can do is to avoid mutual exclusion as muchas possible

▶ Avoid assigning a resource when not really necessary▶ Try to make sure as few processes as possible mayactually claim the resource

25 /1

Page 29: Operating Systems Slides 5 - Deadlock

.Attacking the Hold and Wait Condition..

......

Must guarantee that whenever a process requests aresource, it does not hold any other resources.

Try: the processes must request all their resourcesbefore starting executionif everything is available

then can runif one or more resources are busy

then nothing will be allocated (just wait)Problem: ▶ many processes don’t know what they will

need before running▶ Low resource utilization; starvation possible

26 /1

Page 30: Operating Systems Slides 5 - Deadlock

.Attacking the No Preemption Condition..

......

if a process that is holding some resources requestsanother resource that cannot be immediately allocatedto it

then 1. All resources currently being held are released2. Preempted resources are added to the list of resources for

which the process is waiting3. Process will be restarted only when it can regain its old

resources, as well as the new ones that it is requesting

Low resource utilization; starvation possible

27 /1

Page 31: Operating Systems Slides 5 - Deadlock

.Attacking Circular Wait Condition..

......

Impose a total ordering of all resource types, and requirethat each process requests resources in an increasing orderof enumeration

A1. Imagesetter2. Scanner3. Plotter4. Tape drive5. CD Rom drive

i

B

j

(a) (b)

Fig. 3-13. (a) Numerically ordered resources. (b) A resourcegraph.

It’s hard to find an ordering that satisfies everyone

28 /1

Page 32: Operating Systems Slides 5 - Deadlock

The Ostrich Algorithm

▶ Pretend there is no problem▶ Reasonable if

▶ deadlocks occur very rarely▶ cost of prevention is high

▶ UNIX and Windows takes thisapproach

▶ It is a trade off between▶ convenience▶ correctness

29 /1