+ All Categories
Home > Documents > CH 3 Deadlock

CH 3 Deadlock

Date post: 25-Feb-2016
Category:
Upload: benjy
View: 15 times
Download: 0 times
Share this document with a friend
Description:
CH 3 Deadlock. When 2 (or more) processes remain blocked forever !. Process a Down x Gets x Down y Blocks. Process b Down y Gets y Down x Blocks. How can this happen?. Both are blocked forever !. Resources. Things for which we request exclusive access. - PowerPoint PPT Presentation
50
CH 3 Deadlock When 2 (or more) processes remain blocked forever !
Transcript
Page 1: CH 3 Deadlock

CH 3 Deadlock

When 2 (or more) processes remain blocked forever!

Page 2: CH 3 Deadlock

How can this happen?

• Process a– Down x– Gets x

– Down y– Blocks

• Process b

– Down y– Gets y

– Down x– Blocks

Both are blocked forever!

Page 3: CH 3 Deadlock

Resources

• Things for which we request exclusive access.

– Ex. db, files, shared memory, printer, cd/dvd writer, tape drive, etc.

• Types1. Preemptable – can be taken away

w/out ill effects2. Non preemptable – cannot be take

away w/out causing a failure!

Page 4: CH 3 Deadlock

Resource examples

• Memory – preemptable• CPU – preemptable• CD writing – non preemptable• Printing – non preemptable

• We will only consider non preemptable (the harder problem).

Page 5: CH 3 Deadlock

Using a semaphore to protect one resource.

Page 6: CH 3 Deadlock

Using two semaphores to protect two resource.

Page 7: CH 3 Deadlock
Page 8: CH 3 Deadlock

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.

Page 9: CH 3 Deadlock

Conditions for deadlock

1. Mutual exclusion2. Hold and wait3. No preemption4. Circular wait

(Need all of these (all necessary).)

Page 10: CH 3 Deadlock

Resource allocation graph

process resource

Page 11: CH 3 Deadlock

(Directed)Graphs• A graph G = (V,E) where

V = vertex set andE = edge set

• Ordered pairs of vertices

• Ex. Let G = (V,E) whereV = { C, D, T, U } andE = { <D,U>, <U,C>, <C,T>, <T,D> }

Page 12: CH 3 Deadlock

Representing digraphs

• The simplest method is to use a 2D array where g[i][j] = 1 indicates that an edge exists from i to j.

• Initially, set all elements of g to 0.• Let processes be represented by a

single, lowercase letter: a..z.• Let resources be represented by

integers in the range [1..50].

Page 13: CH 3 Deadlock

Representing digraphs• The simplest method is to use a 2D array where g[i][j] =

1 indicates that an edge exists from i to j.• Input to your program consists of lines read in from an

ASCII text file. Edges in the graph are represented by each line in the file. For example, consider the following:10 ab 2

• The line 10 a is an edge from resource 10 to process a in the graph indicating that process a holds resource 10.

• The line b 2 is an edge from process b to resource 2 in the graph indicating that process b wants (is requesting) resource 2.

• Note that this graph does not contain any cycles.• So how do we use g to represent this graph?

a

b

10

2

Page 14: CH 3 Deadlock

Representing digraphs• The simplest method is to use a 2D array where g[i][j] =

1 indicates that an edge exists from i to j.• Input to your program consists of lines read in from an

ASCII text file. Edges in the graph are represented by each line in the file. For example, consider the following:10 ab 2

• The line 10 a is an edge from resource 10 to process a in the graph indicating that process a holds resource 10.

• The line b 2 is an edge from process b to resource 2 in the graph indicating that process b wants (is requesting) resource 2.

• Note that this graph does not contain any cycles.• So how do we use g to represent this graph?

g[10][‘a’] = 1;g[‘b’][2] = 1;

a

b

10

2

Page 15: CH 3 Deadlock

Another example Here is another example: d 1 1 c c 2 2 dwhich could also be represented by: c 2 d 1 1 c 2 d

Note that the order of lines in the input file is arbitrary. These graphs contain a cycle.

c

d

1

2

Page 16: CH 3 Deadlock

Dealing w/ deadlock

1. Ignore it (ostrich algorithm).2. Detect and recover.3. Avoid by careful resource

allocation.4. Disallow one or more of the

conditions necessary for deadlock.

Page 17: CH 3 Deadlock

1. Ostrich algorithm

• Ignore it; pretend it doesn’t happen!

Page 18: CH 3 Deadlock

2. Detect and recover

1. Detection with one resource of each type

2. Detection with multiple resources of each type

Page 19: CH 3 Deadlock

Detection with one resource of each type• Detect cycle in digraph

– For each vertex v in graph,• search the subtree rooted at v• see if we visit any vertex twice (by

keeping a record of already visited vertices).

Page 20: CH 3 Deadlock

T

Page 21: CH 3 Deadlock

Detection with multiple resources of each type

• Skip.

Page 22: CH 3 Deadlock

Recovery

1. Preemption

2. Rollback (using checkpoints)

3. Kill process

Page 23: CH 3 Deadlock

Recovery: preemption

• Temporarily take back needed resource

• Ex. Printer• Pause at end of page k• Start printing other job• Resume printing original job

starting at page k+1

Page 24: CH 3 Deadlock

Recovery: rollback

• Checkpoint – save entire process state (typically right before the resource was allocated)

• When deadlock is detected, we kill the checkpointed process, freeing the resource, and then later restart the killed process starting at the checkpoint.– Requires apps that can be restarted in

this manner.

Page 25: CH 3 Deadlock

Rdb’s & commit/rollback

From p. 62, http://www.postgresql.org/files/documentation/pdf/8.0/postgresql-8.0-US.pdf

Page 26: CH 3 Deadlock

Recovery: kill process

• Requires apps that can be restarted from the beginning.

Page 27: CH 3 Deadlock

DEADLOCK AVOIDANCE

Page 28: CH 3 Deadlock

Deadlock avoidance

• Safe state = not deadlocked and there exists some scheduling order in which every process can run to completion even if all of them suddenly request their max number of resources immediately

• Unsafe != deadlocked

Page 29: CH 3 Deadlock

Safe states

Page 30: CH 3 Deadlock

Unsafe states

With only 4 free, neither A nor C can be completely satisfied.

Page 31: CH 3 Deadlock

BANKER’S ALGORITHM (SR = FOR SINGLE RESOURCE TYPE)

Page 32: CH 3 Deadlock

Banker’s algorithm (SR)• Grant only those requests that lead to safe

states.• Requires future information.

Page 33: CH 3 Deadlock

Banker’s algorithm (SR)• (b) is safe because from (b) we

can give C 2 more (free=0) then C completes (free=4) then give either B or D . . .

Page 34: CH 3 Deadlock

Banker’s algorithm (SR)• (c) is unsafe because no max

can be satisfied

Page 35: CH 3 Deadlock

BANKER’S ALGORITHM (MR = FOR MULTIPLE RESOURCE TYPES)

Page 36: CH 3 Deadlock

Banker’s algorithm (MR)

N:(need)

E=existing (total); constant

P=possessed (allocated/held)

A=available (free)

Page 37: CH 3 Deadlock

Banker’s algorithm (MR)

To check for a safe state:1. Look for a row, R, in N <= A.2. Assume the process of the

chosen row requests all resources, runs to completion, and terminates (giving back all of its resources to A).

3. Repeat steps 1 and 2 until either all processes either terminate (indicating that the initial state was safe) or deadlock occurs.

Page 38: CH 3 Deadlock

Banker’s algorithm (MR)To check for a safe state:

1. Look for a row, R, in N <= A.

A (available) can completely satisfy D’s needs.

Page 39: CH 3 Deadlock

Banker’s algorithm (MR)To check for a safe state:

1. Look for a row, R, in N <= A.2. Assume the process of the chosen row

requests all resources, runs to completion, and terminates (giving back all of its resources to A).

After D terminates:

E=(6342)

P=(4221)

A=(2121)

Previous:

E=(6342)

P=(5322)

A=(1020)

Page 40: CH 3 Deadlock

Banker’s algorithm (MR)To check for a safe state:

1. Look for a row, R, in N <= A.

E=(6342)

P=(4221)

A=(2121)

Previous:

E=(6342)

P=(5322)

A=(1020)

Page 41: CH 3 Deadlock

Banker’s algorithm (MR)To check for a safe state:

1. Look for a row, R, in N <= A.2. Assume the process of the chosen row

requests all resources, runs to completion, and terminates (giving back all of its resources

After A terminates:

E=(6342)

P=(1210)

A=(5132)

Previous:

E=(6342)

P=(4221)

A=(2121)

Page 42: CH 3 Deadlock

Banker’s algorithm (MR)To check for a safe state:

1. Look for a row, R, in N <= A.

E=(6342)

P=(1210)

A=(5132)

Previous:

E=(6342)

P=(4221)

A=(2121)

Page 43: CH 3 Deadlock

Banker’s algorithm (MR)To check for a safe state:

1. Look for a row, R, in N <= A.

E=(6342)

P=(1110)

A=(5232)

Previous:

E=(6342)

P=(1210)

A=(5132)

Page 44: CH 3 Deadlock

Banker’s algorithm (MR)To check for a safe state:

1. Look for a row, R, in N <= A.

E=(6342)

P=(1110)

A=(5232)

Previous:

E=(6342)

P=(1210)

A=(5132)

Page 45: CH 3 Deadlock

DEADLOCK PREVENTION

Page 46: CH 3 Deadlock

Deadlock prevention

• Attack (disallow):– Mutual exclusion– Hold and wait– No preemption– Circular wait

Page 47: CH 3 Deadlock

Deadlock prevention: attack mutex• Ex. Use spooling instead of mutex

on printer

• Not all problems lend themselves to spooling

• Still have contention for disk space with spooling

Page 48: CH 3 Deadlock

Deadlock prevention: attack hold & wait• Request (wait for) all resources

prior to process execution– Problems:

• We may not know a priori.• We tie up resources for entire time.

• Before requesting a resource, we must first temporarily release all allocated resources and then try to acquire all of them again.

Page 49: CH 3 Deadlock

Deadlock prevention: attack no preemption• Tricky at best.

• Impossible at worst.

Page 50: CH 3 Deadlock

Deadlock prevention: attack circular wait1. Only allow one resource at a

time.2. Request all resources in (some

globally assigned) numerical order.

– But no numerical ordering may exist!


Recommended