You are on page 1of 34

Operating System

Deadlocks WANG Xiaolin

October 18, 2010

u wx672ster+os@gmail.com

1 / 29

Deadlocks
Resources Introduction to Deadlocks Deadlock Modeling Deadlock Detection and Recovery Deadlock Avoidance Deadlock Prevention The Ostrich Algorithm

2 / 29

A Major Class of Deadlocks involve Resources

Examples of computer resources


printers tape drives tables

Processes need access to resources in reasonable order Suppose a process holds resource A and requests resource B
at same time another process holds B and requests A both are blocked and remain so

3 / 29

Resources
typedef int semaphore; semaphore resource 1; semaphore resource 2; void process A(void) { down(&resource 1); down(&resource 2); use both resources( ); up(&resource 2); up(&resource 1); } void process B(void) { down(&resource 1); down(&resource 2); use both resources( ); up(&resource 2); up(&resource 1); } (a) semaphore resource 1; semaphore resource 2; void process A(void) { down(&resource 1); down(&resource 2); use both resources( ); up(&resource 2); up(&resource 1); } void process B(void) { down(&resource 2); down(&resource 1); use both resources( ); up(&resource 1); up(&resource 2); } (b)
4 / 29

Resources
typedef int semaphore; semaphore resource 1; semaphore resource 2; void process A(void) { down(&resource 1); down(&resource 2); use both resources( ); up(&resource 2); up(&resource 1); } void process B(void) { down(&resource 1); down(&resource 2); use both resources( ); up(&resource 2); up(&resource 1); } (a) semaphore resource 1; semaphore resource 2; void process A(void) { down(&resource 1); down(&resource 2); use both resources( ); up(&resource 2); up(&resource 1); } void process B(void) { down(&resource 2); down(&resource 1); use both resources( ); up(&resource 1); up(&resource 2); } (b)
4 / 29

D .

ea dl oc k!

Resources
Deadlocks occur when ...
processes are granted exclusive access to resources
devices, data records, files, ...

Preemptable resources can be taken away from a process with no ill effects e.g. memory Nonpreemptable resources will cause the process to fail if taken away e.g. CD recorder In general, deadlocks involve nonpreemptable resources
5 / 29

Resources

Sequence of events required to use a resource


request use release

What if request is denied?


Requesting process may be blocked may fail with error code

6 / 29

The Best Illustration of a Deadlock

A law passed by the Kansas legislature early in the 20th century


When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone.

7 / 29

Introduction to Deadlocks

Deadlock A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause. Usually the event is release of a currently held resource None of the processes can ...
run release resources be awakened

8 / 29

Four Conditions For Deadlocks


Mutual exclusion condition each resource assigned to 1 process or is available Hold and wait condition process holding resources can request additional No preemption condition previously granted resources cannot forcibly taken away Circular wait condition must be a circular chain of 2 or more processes each is waiting for resource held by next member of the chain

9 / 29

Four Conditions For Deadlocks

Unlocking a deadlock is to answer 4 questions:


1. Can a resource be assigned to more than one process at once? 2. Can a process hold a resource and ask for another? 3. can resources be preempted? 4. Can circular waits exits?

10 / 29

Resource-Allocation Graph
A S D

h . as

w . ants

R (a)

B (b)

C (c)

Fig. (a) Strategies3-3. Resource allocation graphs.Deadlocks for dealing (c) Deadlock. Holding a resource. (b) Requesting a resource. with

1. just ignore the problem altogether 2. detection and recovery 3. dynamic avoidance careful resource allocation 4. prevention negating one of the four necessary conditions .
11 / 29

Resource-Allocation Graph

12 / 29

Resource-Allocation Graph

Basic facts:
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

13 / 29

Deadlock Detection

Allow system to enter deadlock state Detection algorithm Recovery scheme

14 / 29

Deadlock Detection
Single Instance of Each Resource Type Wait-for Graph

Pi Pj if Pi is waiting for Pj ; Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock. An algorithm to detect a cycle in a graph requires an order of n2 operations, where n is the number of vertices in the graph.
15 / 29

Deadlock Detection
Several Instances of a Resource Type
Resources in existence (E1, E2, E3, , Em) Current allocation matrix C11 C21 Cn1 C12 C22 Cn2 C13 C23 Cn3 C1m C2m Cnm R11 R21 Rn1 Resources available (A1, A2, A3, , Am) Request matrix R12 R22 Rn2 R13 R23 Rn3 R1m R2m Rnm

Row n is current allocation to process n

Row 2 is what process 2 needs

n i=1

Cij + Aj = Ej

Fig. 3-6. The four data structures needed by the deadlock detection e.g. algorithm. (C13 + C23 + . . . + Cn3 ) + A3 = E3
16 / 29

Deadlock Detection
Several Instances of a Resource Type

n: number of processes; m: number of resource classes; E: a vector of existing resources


E = [E1 , E2 , ..., Em ] Ei = 2 means system has 2 resources of class i, (1 i m);

A: a vector of available resources;


A = [A1 , A2 , ...Am ] Ai = 2 means system has 2 resources of class i left unassigned;

Cij : is the number of instances of resource j that process i holds;


C31 = 2 means P3 has 2 resources of class 1;

Rij : is the number of instances of resource j that process i wants;


R43 = 2 means P4 wants 2 resources of class 3;
17 / 29

Deadlock Detection
Several Instances of a Resource Type

Vectors comparison
For two vectors, X and Y XY
e.g.

iff Xi Yi
[ ] 1 2 3 4 [ ] 1 2 3 4

for 0 i m

[ ] 2 3 4 4 [ ] 2 3 2 4

18 / 29

Deadlock Detection
Several Instances of a Resource Type

ive

ive

rs

rs ne an C
0)

ne

om

dr

te

dr

te

an

ot

Ta p

Ta p

ot

Pl

Sc

E=(4

1)

A=(2

Pl

Current allocation matrix 0 0 1 0 2 0 0 1 0 1 2 0

Request matrix 2 0 0 1 1 0 1 0 2 1 0 0

C=

R=

Sc

D
19 / 29

om

rs

rs

Deadlock Detection
Several Instances of a Resource Type

ive

ive

rs

rs ne an C
0)

ne

om

dr

te

dr

te

an

ot

Ta p

Ta p

ot

Pl

Sc

E=(4

1)

A=(2

Pl

A . R ( . 2 1 0 0) R3 , (2 1 0 0)

Current allocation matrix 0 0 1 0 2 0 0 1 0 1 2 0

Request matrix 2 0 0 1 1 0 1 0 2 1 0 0

C=

R=

Sc

om

rs

rs

Deadlock Detection
Several Instances of a Resource Type

ive

ive

rs

rs ne an C
0)

ne

om

dr

te

dr

te

an

ot

Ta p

Ta p

ot

Pl

Sc

E=(4

1)

A=(2

Pl

A . R ( . 2 1 0 0) R3 , (2 1 0 0) ( . 2 2 2 0) R2 , (1 0 1 0)
Current allocation matrix 0 0 1 0 2 0 0 1 0 1 2 0 Request matrix 2 0 0 1 1 0 1 0 2 1 0 0

C=

R=

Sc

om

rs

rs

Deadlock Detection
Several Instances of a Resource Type

ive

ive

rs

rs ne an C
0)

ne

om

dr

te

dr

te

an

ot

Ta p

Ta p

ot

Pl

Sc

E=(4

1)

A=(2

Pl

A . R ( . 2 1 0 0) R3 , (2 1 0 0) ( . 2 2 2 0) R2 , (1 0 1 0) ( . 4 2 2 0) R3 , (2 0 0 1)
Current allocation matrix 0 0 1 0 2 0 0 1 0 1 2 0 Request matrix 2 0 0 1 1 0 1 0 2 1 0 0

C=

R=

.
19 / 29

Sc

om

rs

rs

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

20 / 29

;; ;; ;
Resource Trajectories
B u (Both processes finished) Printer I8 I7 I6 I5 t

Deadlock Avoidance

Plotter

U . nsafe region
A

q Printer

I1

I2

I3

I4 Plotter

B is requesting a resource at point t. The system must decide whether to grant it trajectories. Fig. 3-8. Two process resource or not. .
21 / 29

Deadlock Avoidance
Safe and Unsafe States

Assuming E = 10,

Safe
Has Max A B C 3 2 2 Free: 3 (a) 9 4 7 A B C Has Max 3 4 2 Free: 1 (b) 9 4 7 A B C Has Max 3 0 2 Free: 5 (c) 9 7 A B C Has Max 3 0 7 Free: 0 (d) 9 7 A B C Has Max 3 0 0 Free: 7 (e) 9

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


Has Max A B C 3 2 2 Free: 3 (a) 9 4 7 A B C Has Max 4 2 2 Free: 2 (b) 9 4 7 A B C Has Max 4 4 2 Free: 0 (c) 9 4 7 A B C Has Max 4 2 Free: 4 (d)
22 / 29

9 7

Deadlock Avoidance
The Bankers Algorithm for a Single Resource

The bankers algorithm considers each request as it occurs, and sees if granting it leads to a safe state.
Has Max A B C D 0 0 0 0 Free: 10 (a) 6 5 4 7 A B C D Has Max 1 1 2 4 Free: 2 (b) 6 5 4 7 A B C D Has Max 1 2 2 4 Free: 1 (c) 6 5 4 7

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

Deadlock Avoidance
The Bankers Algorithm for a Single Resource

The bankers algorithm considers each request as it occurs, and sees if granting it leads to a safe state.
Has Max A B C D 0 0 0 0 Free: 10 (a) 6 5 4 7 A B C D Has Max 1 1 2 4 Free: 2 (b) 6 5 4 7 A B C D Has Max 1 6 5 4 7

U .

. Fig. 3-11. Three resource allocation states: (a) Safe. (b) Safe. unsafe = deadlock (c) Unsafe.
23 / 29

ns

Free: 1 (c)

af
2 4

e!

Deadlock Avoidance
The Bankers Algorithm for Multiple Resources
oc e Ta ss pe Pl dr i v ot te es Sc r s an n CD e r s RO M s oc e Ta ss pe Pl dr i v ot te es Sc r s an n CD e r s RO M s
1 0 3 0 2 1 1 1 0 1 0 1 0 1 1 0 2 0 0 0 E = (6342) P = (5322) A = (1020)

Pr

A B C D E

3 0 1 1 0

0 1 1 1 0

1 0 1 0 0

1 0 0 1 0

Resources assigned

Resources still needed

In practiceFig. 3-12. The bankers algorithm with multiple resources.


processes rarely know in advance their max future resource needs; the number of processes is not fixed; the number of available resources is not fixed. Conclusion: deadlock avoidance is essentially a mission impossible.
24 / 29

Pr
A B C D E

Deadlock Prevention
Mutual Exclusion not required for sharable resources; must hold for nonsharable resources. Hold and Wait must guarantee that whenever a process requests a resource, it does not hold any other resources. Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none Low resource utilization; starvation possible

25 / 29

Deadlock Prevention
No Preemption If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released Preempted resources are added to the list of resources for which the process is waiting Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting

26 / 29

Deadlock Prevention
Circular Wait

Circular Wait impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration

1. Imagesetter 2. Scanner 3. Plotter 4. Tape drive 5. CD Rom drive (a)

i (b)

. .ts hard to find an ordering that satisfiesresource Fig. 3-13. (a) Numerically ordered resources. (b) A everyone I graph.

27 / 29

The Ostrich Algorithm

Pretend there is no problem Reasonable if


deadlocks occur very rarely cost of prevention is high

UNIX and Windows takes this approach It is a trade off between


convenience correctness

28 / 29

Reference

Operating System Concepts, 8e, Chapter 7 Modern Operating System, 3e, Chapter 6

29 / 29

You might also like