79
1 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity 求求求求 求求求求

11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

Embed Size (px)

Citation preview

Page 1: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

11

Chapter 3: DeadlocksInstructor: Hengming Zou, Ph.D.

In Pursuit of Absolute Simplicity 求于至简,归于永恒

Page 2: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

22

Content

Resource Deadlocks Deadlock modeling Deadlock detection and recovery Deadlock avoidance and prevention

Page 3: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

33

Resources

Something needed by a thread A thread waits for resources Examples of computer resources

– Printers, tape drives, tables,

– locks, disk space, memory, CPU

Page 4: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

44

Resources

Processes need access to resources– in reasonable order

Preemptable resources– can be taken away from a process with no ill effects

Nonpreemptable resources– will cause the process to fail if taken away

Page 5: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

55

Resources

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– Deadlock!

Page 6: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

66

Resources

Sequence of events required to use a resource

1. request the resource

2. use the resource

3. release the resource

Page 7: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

77

Resources

Must wait if request is denied– requesting process may be blocked

– may fail with error code

Page 8: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

88

Deadlocks

A circular waiting for resources– leading to threads involved not being able to make progress

Deadlocks occur when …

– processes are granted exclusive access to resources

Page 9: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

99

Deadlocks

Formal definition: 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 held resource

Page 10: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

1010

Deadlocks

In a deadlock, none of the processes can …– run

– release resources

– be awakened

Page 11: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

1111

Deadlocks

General structure of thread code– phase 1. while (not done) {

– acquire some resources

– work

– }

– phase 2. release all resources

Assume phase 1 has finite amount of work

Page 12: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

1212

Deadlocks

Example

thread A thread B

lock(x) lock(y)

lock(y) lock(x)

... ...

unlock(y) unlock(x)

unlock(x) unlock(y)

Page 13: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

1313

Deadlocks

Can deadlock occur with code? Will deadlock always occur with this code?

Page 14: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

1414

Dining Philosophers Problem

Page 15: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

1515

Dining Philosophers

5 philosophers sitting around a round table 1 chopstick in between each pair of philosophers

– 5 chopsticks total

Each philosopher needs two chopsticks to eat How to prevent deadlock

Page 16: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

1616

Dining Philosophers

Algorithm for each philosopher– wait for chopstick on right to be free, then pick it up

– wait for chopstick on left to be free, then pick it up

– eat

– put both chopsticks down

Can this deadlock?

Page 17: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

1717

Conditions for Deadlock

4 conditions must all be true for deadlock to occur Limited resource:

– not enough resources to serve all threads simultaneously

Hold and wait: – threads hold resources while waiting to acquire others

Page 18: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

1818

Conditions for Deadlock

No preemption– can’t force thread to give up resource

Circular chain of requests

Resource BResource A

Thread A

Thread B

Page 19: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

1919

Four Conditions for Deadlock

The first condition is sometimes called Mutual exclusion condition

– each resource assigned to 1 process or is available

Tanebaum, etc.

Page 20: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

2020

Deadlock Modeling

Modeled with directed graphs

Page 21: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

2121

Deadlock Modeling

Resource R assigned to process A Process B is requesting/waiting for resource S Process C and D are in deadlock

– over resources T and U

Page 22: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

2222

Deadlock Modeling

Page 23: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

2323

(o) (p) (q)

How deadlock can be avoided

Page 24: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

2424

Strategies for dealing with Deadlocks

Ignore: just ignore the problem altogether Detection and recovery Dynamic avoidance: careful resource allocation Prevention:

– negating one of the four necessary conditions

Page 25: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

2525

Ignore Strategy

Pretend there is no problem Reasonable if

– deadlocks occur very rarely

– cost of prevention is high

UNIX and Windows takes this approach a trade off between convenience & correctness

Page 26: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

2626

Detect and Fix

Note the resource ownership and requests Detect by looking for cycles in the wait-for graph How to fix once detected?

Page 27: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

2727

Detection with Wait-for Graph

Page 28: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

2828

Detection with Wait-for Graph

Page 29: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

2929

An example for the deadlock detection algorithm

Detection with Wait-for Graph

Page 30: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

3030

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

Page 31: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

3131

Recovery from Deadlock

Recovery through killing processes– crudest but simplest way to break a deadlock

– kill one of the processes in the deadlock cycle

– the other processes get its resources

– choose process that can be rerun from the beginning

Page 32: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

3232

Deadlock Avoidance

Don’t get in a position where– Deadlock becomes possible

Use:– Resource Trajectories

Page 33: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

3333

Safe and Unsafe States

Demonstration that the state in (a) is safe

A 3 9

B 2 4

C 2 7

Has Max

(a) Free: 3

A 3 9

B 4 4

C 2 7

Has Max

(b) Free: 1

A 3 9

B 0 -C 2 7

Has Max

(c) Free: 5

A 3 9

B 0 -C 7 7

Has Max

(d) Free: 0

Page 34: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

3434

Safe and Unsafe States

Demonstration that the state in b is not safe

A 3 9

B 2 4

C 2 7

Has Max

(a) Free: 3

A 4 9

B 2 4

C 2 7

Has Max

(b) Free: 2

A 4 9

B 4 4

C 2 7

Has Max

(c) Free: 0

A 4 9

B - -C 2 7

Has Max

(d) Free: 4

Page 35: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

3535

Deadlock Prevention

Idea is to eliminate one of the four necessary conditions Increase resources to decrease waiting

– this minimizes chance of deadlock

Page 36: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

3636

Deadlock Prevention

Attacking the Mutual Exclusion Condition Attacking the Hold and Wait Condition Attacking the No Preemption Condition Attacking the Circular Wait Condition

Page 37: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

3737

Attacking Mutual Exclusion

Some devices (such as printer) can be spooled– only the printer daemon uses printer resource

– thus deadlock for printer eliminated

Not all devices can be spooled

Page 38: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

3838

Attacking Mutual Exclusion

Principle: Avoid assigning resource when not absolutely necessary As few processes as possible actually claim the resource

Page 39: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

3939

Attacking Hold & Wait Condition

Processes to request resources before starting– a process never has to wait for what it needs

Eliminate hold and wait

Page 40: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

4040

Attacking Hold & Wait Condition

phase 1a. acquire all resources phase 1b. while (not done) { acquire some resource work } phase 2. release all resources

Page 41: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

4141

Attacking Hold & Wait Condition

A. wait until all resources you’ll need are free, then grab them all at once

(or) B. if you find resource busy, release all acquired resources and go back to beginning

Page 42: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

4242

Attacking Hold & Wait Condition

Problems? May not know required resources at start of run Ties up resources others could be using

Page 43: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

4343

Attacking Hold & Wait Condition

Variation: Process must give up all resources Then request all immediately needed

Page 44: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

4444

Attacking No Preemption Condition

Allow Preemption! Can preempt CPU by saving its state to thread control block

and resuming later Can preempt memory by swapping memory out to disk and

loading it back later Can we preempt the holding of a lock?

Page 45: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

4545

Attacking No Preemption Condition

Some resource cannot be preempted Consider a process given the printer

– halfway through its job

– now forcibly take away printer

– !!??

Page 46: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

4646

Attacking Circular Wait Condition

Normally ordered resources– Imagesetter scanner plotter printer

A resource graph

Page 47: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

4747

Banker’s Algorithm

Derive from methods by bankers to grant loans Similar to reserving all resources at beginning

– but more efficient

Page 48: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

4848

Banker’s Algorithm

State maximum resource needs in advance– but don’t actually acquire the resources

When thread later tries to acquire a resource, Banker’s algorithm determines:

– when it’s safe to satisfy the request

– and blocks the thread when it’s not safe

Page 49: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

4949

Banker’s Algorithm

General structure of thread code– phase 1a. state maximum resource needed

– phase 1b. while (not done) {

– acquire some resources

– work

– }

– phase 2. release all resources

Page 50: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

5050

Banker’s Algorithm

Preventing deadlock by requesting all resources at beginning would block thread in phase 1a above

– but phase 1b can proceed without waiting

Page 51: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

5151

Banker’s Algorithm

Phase 1a provides information needed to determine – when it’s safe to satisfy each resource request in phase 1b

“Safe” means guaranteeing the ability for all threads to finish – no possibility of deadlock

Page 52: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

5252

Banker’s Algorithm

Example: use banker’s algorithm to model a bank loaning money to its customers

Page 53: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

5353

Banker’s Algorithm

Bank has $6000 Customers sign up with bank and establish a credit limit

(maximum resource needed) They borrow money in stages (up to credit limit) When they’re done, they return all the money

Page 54: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

5454

Solution #1

Bank gives money when requested– as long as the money is available

– Bank must reserve all resources when customer starts

Ann asks for credit limit of $2000 Bob asks for credit limit of $4000 Charlie asks for credit limit of $6000

Page 55: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

5555

Banker’s Algorithm

Can bank approve all these credit lines– if it promises to give money upon request if available?

Page 56: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

5656

Solution #2: Banker’s Algorithm

bank approves all credit limits but customer may have to wait

– when actually asking for the money

Page 57: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

5757

Banker’s Algorithm

Ann asks for credit limit of $2000 (bank oks) Bob asks for credit limit of $4000 (bank oks) Charlie asks for credit limit of $6000 (bank oks) Ann takes out $1000 (bank has $5000 left) Bob takes out $2000 (bank has $3000 left) Charlie wants to take out $2000. Is this allowed?

Page 58: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

5858

Banker’s Algorithm

Allow iff, after giving the money, there exists some sequential order of fulfilling all maximum resources

– worst-case analysis

Page 59: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

5959

Banker’s Algorithm

If give $2000 to Charlie, bank will have $1000 left Ann can finish even if she takes out her max

– i.e. another $1000

When Ann finishes, she returns her money– bank will have $2000

Page 60: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

6060

Banker’s Algorithm

After Ann finishes, Bob can take out his max – another $2000, then finish

Then Charlie can finish– even if he takes out his max (another $4000)

Page 61: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

6161

Banker’s Algorithm

What about this scenario?– Ann asks for credit limit of $2000 (bank oks)

– Bob asks for credit limit of $4000 (bank oks)

– Charlie asks for credit limit of $6000 (bank oks)

– Ann takes out $1000 (bank has $5000 left)

– Bob takes out $2000 (bank has $3000 left)

– Charlie wants to take out $2500. Is this allowed?

Page 62: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

6262

Banker’s Algorithm

Banker allows system to over commit resources without introducing the possibility of deadlock

Sum of max resource needs of all current threads can be greater than total resources

as long as there’s some way for the all the threads to finish without getting into deadlock.

Page 63: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

6363

Banker’s Algorithm

How to apply banker’s algorithm to dining philosophers? Unfortunately, it’s difficult to anticipate maximum resources

needed

Page 64: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

6464

Three resource allocation states

A 0 6

B 0 5

C 0 4

D 0 7

Has Max

(a) Free: 10

A 1 6

B 1 5

C 2 4

D 4 7

Has Max

(b) Free: 2

A 1 6

B 2 5

C 2 4

D 4 7

Has Max

(c) Free: 1

Banker's Algorithm for a Single Resource

Page 65: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

6565

Banker's Algorithm for Multiple Resources

Page 66: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

6666

Summary of Deadlock Prevention

Mutual Exclusion: spool everything Hold and wait: request all resources initially No preemption: takes resource away Circular wait: order resource numerically

Page 67: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

6767

Other Issues

Two-Phase Locking Nonresource Deadlocks Starvation

Page 68: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

6868

Two-Phase Locking

Phase One– process tries to lock all records it needs, one at a time

– if needed record found locked, start over

– (no real work done in phase one)

Page 69: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

6969

Two-Phase Locking

If phase one succeeds, it starts second phase, – performing updates

– releasing locks

Page 70: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

7070

Two-Phase Locking

Note similarity to requesting all resources at once Algorithm works where programmer can arrange

– program can be stopped, restarted

Page 71: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

7171

Nonresource Deadlocks

Possible for two processes to deadlock– each is waiting for the other to do some task

Can happen with semaphores– each process required to do a down() on two semaphores

(mutex and another)

– if done in wrong order, deadlock results

Page 72: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

7272

Starvation

Algorithm to allocate a resource – may be to give to shortest job first

Works great for multiple short jobs in a system May cause long job to be postponed indefinitely

– even though not blocked

Solution:– First-come, first-serve policy

Page 73: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

7373

Let’s return to dining philosophers problem Approach:

– Attacking the Hold and Wait Condition

– Attacking the Circular Wait Condition

Solution to Dinning Philosopher Problem

Page 74: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

7474

A Solution with Semaphore

#define N 5 /*# of philosophers*/

#define LEFT (i+N-1)%N /*# of I’s left neighbor*/

#define RIGHT (i+1)%N /*# of I’s right neighbor*/

#define THINKING 0

#define HUNGRY 1

#define EATING 2

typedef int semaphore;

int state[N]; /*array to keep track of everyone’s state*/

semaphore mutex=1;

semaphore s[N]; /*one semaphore per philosopher*/

Page 75: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

7575

A Solution with Semaphore

void philosopher(int i)

{

while(TRUE) {

think();

take_forks(i); /*acquire two forks or block*/

eat();

put_forks(i); /*put both forks on the table*/

}

}

Page 76: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

7676

A Solution with Semaphore

void take_forks(int i)

{

down(&mutex);

state[i]=HUNGRY;

test(i); /*try to acquire 2 forks*/

up(&mutex);

down(&s[i]); /*block if forks were not acquired*/

}

Page 77: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

7777

A Solution with Semaphore

void put_forks(i)

{

down(&mutex);

state[i]=THINKING;

test(LEFT); /*see if left neighbor can eat*/

test(RIGHT); /*see if right neighbor can eat*/

up(&mutex);

}

Page 78: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

7878

A Solution with Semaphore

void test(i)

{

if(state[i]==HUNGRY && state[LEFT]!=EATING && state[RIGHT]!=EATING) {

state[i]=EATING;

up(&s[i]);

}

}

Page 79: 11 Chapter 3: Deadlocks Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity

Computer Changes Life