Deadlocks - UiO

44
Deadlocks Thomas Plagemann With slides from C. Griwodz, K. Li, A. Tanenbaum and M. van Steen

Transcript of Deadlocks - UiO

Deadlocks

Thomas Plagemann

With slides from C. Griwodz, K. Li, A. Tanenbaum and M. van Steen

Preempting Scheduler Activations

n  Scheduler activations are completely preemptable

Kernel "space"

User"space"

Preempting Scheduler Activations

n  Maintaining the run queue needs to be a protected critical section

n  Let’s use spin locks for protection Thread 1" Upcall"

Acquire spin lock""

Start queue maintenance"

Interrupt Thread 1""Acquire spin lock -> block"

Resources

n  Examples of computer resources n  CPU n  Memory n  Disk drive n  Tape drives n  Printers n  Plotter n  Loudspeaker

Resources n  Processes

n  Need access to resources in reasonable order

n  Typical way to use a resource n  Request n  Use n  Release

n  Suppose a process holds resource A and requests resource B n  At same time another process holds B and requests A n  Both are blocked and remain so

Resources n  Active resource

n  Provides a service n  E.g. CPU, network adaptor

n  Passive resource n  System capabilities that are required by active resources n  E.g. memory, network bandwidth

n  Exclusive resource n  Only one process at a time can use it n  E.g. loudspeaker, processor

n  Shared resource n  Can be used by multiple processes n  E.g. memory, bandwidth

Resources n  Single resource

n  Exists only once in the system n  E.g. loudspeaker

n  Multiple resource n  Exists several time in the system n  E.g. processor in a multiprocessor system

n  Preemptable resource n  Resource that can be taken away from a process n  E.g. CPU can be taken away from processes in user space

n  Non-preemptable resource n  Taking it away will cause processes to fail n  E.g. Disk, files

Resources

n  Process must wait if request is denied n  Requesting process may

be blocked n  May fail with error code

n  Deadlocks n  Occur only when

processes are granted exclusive access to resources

block"acquire"

use"

acquire"

use"

Deadlocks

n  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

n  Usually the event is release of a currently held resource

n  None of the processes can … n  Run n  Release resources n  Be awakened

Four Conditions for Deadlock

1.  Mutual exclusion condition n  Each resource assigned to 1 process or is available

2.  Hold and wait condition n  Process holding resources can request additional

3.  No preemption condition n  Previously granted resources cannot forcibly taken away

4.  Circular wait condition n  Must be a circular chain of 2 or more processes n  Each is waiting for resource held by next member of the

chain

Deadlock Modeling n  Modeled with directed graphs

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

A"

R" B"

S" C"

U" D"

T"

Deadlock Example

n  A utility program n  Copies a file from a tape

to disk n  Prints the file to a

printer

n  Resources n  Tape n  Disk n  Printer

n  A deadlock

tape disk printer

A

B

Deadlock Modeling n  How deadlock occurs

A"Requests R"Requests S"Releases S"Releases R"

B"Requests S"Requests T"Releases T"Releases S"

C"Requests T"Requests R"Releases R"Releases T"

A requests R"B requests S"C requests T"A requests S"B requests T"C requests R"

Resources"

Processes" A" B" C"

R" S" T"

Deadlock Modeling n  How deadlock can be avoided

A"Requests R"Requests S"Releases S"Releases R"

B"Requests S"Requests T"Releases T"Releases S"

C"Requests T"Requests R"Releases R"Releases T"

A requests R"C requests T"A requests S"B requests S"B requests T"C requests R"A releases S"A releases R"C releases R"C releases T"

Resources"

Processes" A" B" C"

R" S" T"

Deadlocks: Strategies

n  Ignore the problem n  It is user’s fault

n  Detection and recovery n  Fix the problem afterwards

n  Dynamic avoidance n  Careful allocation

n  Prevention n  Negate one of the four conditions

The Ostrich Algorithm

n  Pretend there is no problem

n  Reasonable if n  Deadlocks occur very rarely n  Cost of prevention is high

n  UNIX and Windows take this approach n  It is a trade-off between

n  Convenience n  Correctness

Deadlock Detection and Recovery One Resource of Each Type

n  A cycle can be found within the graph, denoting deadlock

A" B"

C"

R"

S" T"D" E"

F"

G"

U" V"

W"

init notebook L

add current node CN to L

is CN two times in L?cycle detected

is an unmarkedoutgoing arc at CN?

follow the arcCN = next node

backtrackingCN = previous node

Deadlock Detection and Recovery One Resource of Each Type

Deadlock Detection and Recovery Multiple Resources of Each Type

The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

⎥⎥⎥⎥

⎢⎢⎢⎢

nmnnn

m

m

CCCC

CCCCCCCC

..................

...

...

321

2232221

1131211

⎥⎥⎥⎥

⎢⎢⎢⎢

nmnnn

m

m

RRRR

RRRRRRRR

..................

...

...

321

2232221

1131211

Existing resources" Available resources"

The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

( )mEEEE ,...,,, 321 ( )mAAAA ,...,,, 321

Current allocation matrix" Request matrix"

Deadlock Detection and Recovery Multiple Resources of Each Type

4" 2" 3" 1" 2" 1" 0" 0"

0" 0" 1" 0"2" 0" 0" 1"0" 1" 2" 0"

2" 0" 0" 1"1" 0" 1" 0"2" 1" 0" 0"

E=("

Current allocation matrix" Request matrix"

A=(")" )"

R="C="

Deadlock Detection and Recovery Recovery

n  Recovery through preemption n  Take a resource from some other process n  Depends on nature of the resource

n  Recovery through rollback n  Checkpoint a process periodically n  Use this saved state n  Restart the process if it is found deadlocked

n  Recovery through killing processes n  Crudest but simplest way to break a deadlock n  Kill one of the processes in the deadlock cycle n  The other processes get its resources n  Choose process that can be rerun from the beginning

Deadlock Avoidance Resource Trajectories

finished"

request" release"request" release"

Printer"

Plotter"

A!

Prin

ter"

release"

release"

request"

request"

Plot

ter"

B!

start"

Two process"resource trajectories"

Deadlock Avoidance Safe and Unsafe States

state is safe

A"B"C"

3"2"2"

9"4"7"

Free: 3"

has" max"A"B"C"

3"4"2"

9"4"7"

Free: 1"

has" max"A"B"C"

3"0"2"

9"

7"Free: 5"

has" max"A"B"C"

3"0"7"

9"

7"Free: 0"

has" max"A"B"C"

3"0"0"

9"

Free: 7"

has" max"

Deadlock Avoidance Safe and Unsafe States

state is safe

A"B"C"

3"2"2"

9"4"7"

Free: 3"

has" max"A"B"C"

4"2"2"

9"4"7"

Free: 2"

has" max"A"B"C"

3"0"2"

9"

7"Free: 4"

has" max"A"B"C"

4"4"2"

9"4"7"

Free: 0"

has" max"

Deadlock Avoidance Banker’s Algorithm for a Single Resource

n  Each process has a credit n  System knows how many resources a process

requests at most before releasing resources

n  Total resources may not satisfy all credits n  Keep track of resources assigned and needed n  Check on each allocation whether it is safe

n  Safe: there exists a sequence of other states that all processes can terminate correctly

Deadlock Avoidance Banker's Algorithm for a Single Resource

Free: 10"

has"max"

0"0"0"0"

6"5"4"7"

Free: 2"

has"max"

1"1"2"4"

6"5"4"7"

Free: 1"

has"max"

1"2"2"4"

6"5"4"7"

6"5"0" 5"0" 6"

4"0" 4"7"0" 7"

4"2" 4"

A"B"C"D"

3"3"5"5"1" 5"6"1" 6"

7"4" 7"

A"B"C"D"

1"2"2"4"

A"B"C"D"

Free: 4"Free: 5"Free: 6"Free: 2"

Resource allocation state"

Deadlock Detection and Recovery Banker’s Algorithm for Multiple Resources

An example for the deadlock detection algorithm

A" 3" 0" 1"B" 0" 1" 0"C" 1" 1" 1"

Assigned resources" Resources still needed"

6" 3" 4" 2"5" 3" 2" 2"

E=("P=("

)")"

1" 0" 2" 0"A=(" )"1"0"0"

D" 1" 1" 0" 1"E" 0" 0" 0" 0"

A" 1" 1" 0"B" 0" 1" 1"C" 3" 1" 0"

0"2"0"

D" 0" 0" 1" 0"E" 2" 1" 1" 0"

Deadlock Detection and Recovery Banker’s Algorithm for Multiple Resources

An example for the deadlock detection algorithm

A" 3" 0" 1"B" 0" 1" 0"C" 1" 1" 1"

6" 3" 4" 2"4" 2" 2" 1"

E=("P=("

)")"

2" 1" 2" 1"A=(" )"1"0"0"

D" 0" 0" 0" 0"E" 0" 0" 0" 0"

A" 1" 1" 0"B" 0" 1" 1"C" 3" 1" 0"

0"2"0"

D" -" -" -" -"E" 2" 1" 1" 0"

Assigned resources" Resources still needed"

Deadlock Detection and Recovery Banker’s Algorithm for Multiple Resources

An example for the deadlock detection algorithm

A" 0" 0" 0"B" 0" 1" 0"C" 1" 1" 1"

6" 3" 4" 2"1" 2" 1" 0"

E=("P=("

)")"

5" 1" 3" 2"A=(" )"0"0"0"

D" 0" 0" 0" 0"E" 0" 0" 0" 0"

A" -" -" -"B" 0" 1" 1"C" 3" 1" 0"

-"2"0"

D" -" -" -" -"E" 2" 1" 1" 0"

Assigned resources" Resources still needed"

Deadlock Detection and Recovery Banker’s Algorithm for Multiple Resources

An example for the deadlock detection algorithm

A" 0" 0" 0"B" 0" 0" 0"C" 1" 1" 1"

6" 3" 4" 2"1" 1" 1" 0"

E=("P=("

)")"

5" 2" 3" 2"A=(" )"0"0"0"

D" 0" 0" 0" 0"E" 0" 0" 0" 0"

A" -" -" -"B" -" -" -"C" 3" 1" 0"

-"-"0"

D" -" -" -" -"E" 2" 1" 1" 0"

Assigned resources" Resources still needed"

Deadlock Detection and Recovery Banker’s Algorithm for Multiple Resources

An example for the deadlock detection algorithm

A" 0" 0" 0"B" 0" 0" 0"C" 0" 0" 0"

6" 3" 4" 2"0" 0" 0" 0"

E=("P=("

)")"

6" 3" 4" 2"A=(" )"0"0"0"

D" 0" 0" 0" 0"E" 0" 0" 0" 0"

A" -" -" -"B" -" -" -"C" -" -" -"

-"-"-"

D" -" -" -" -"E" 2" 1" 1" 0"

Assigned resources" Resources still needed"

Deadlock Detection and Recovery Banker’s Algorithm for Multiple Resources

An example for the deadlock detection algorithm

A" 3" 0" 1"B" 0" 1" 0"C" 1" 1" 1"

6" 3" 4" 2"5" 3" 2" 2"

E=("P=("

)")"

1" 0" 2" 0"A=(" )"1"0"0"

D" 1" 1" 0" 1"E" 0" 0" 0" 0"

A" 1" 1" 0"B" 0" 1" 1"C" 3" 1" 0"

0"2"0"

D" 0" 0" 1" 0"E" 2" 1" 1" 0"

SAFE"

Assigned resources" Resources still needed"

Deadlock Avoidance Practical Avoidance

n  Two Phase Locking n  Phase I

n  Process tries to lock all resources it needs, one at a time n  If needed resources found locked, start over n  (no real work done in phase one)

n  Phase II n  Run n  Releasing locks

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

Deadlock Prevention R: Conditions for Deadlock

1.  Mutual exclusion condition n  Each resource assigned to 1 process or is available

2.  Hold and wait condition n  Process holding resources can request additional

3.  No preemption condition n  Previously granted resources cannot forcibly taken away

4.  Circular wait condition n  Must be a circular chain of 2 or more processes n  Each is waiting for resource held by next member of the

chain

Deadlock Prevention Mutual Exclusion Condition

n  Some resources are not sharable n  Printer, tape, etc

n  Some resources can be made sharable n  Some resources can be made virtual

n  Spooling - Printer n  Does spooling apply to all non-sharable resources?

n  Mixing - Soundcard

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

Deadlock Prevention Hold and Wait Condition

n  Require processes to request resources before starting n  A process never has to wait for what it needs n  Telephone companies do this

n  Problems n  May not know required resources at start of run n  Also ties up resources other processes could be using

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

Deadlock Prevention No Preemption Condition

n  This is not a viable option n  Consider a process given the printer

n  Halfway through its job n  No forcibly take away printer n  !!??

Deadlock Prevention Circular Wait Condition

A"

1" 5"4"3"2"

n  Normally ordered resources n  A resource graph

1.  CD Rom drive"2.  Tape drive"3.  Plotter"4.  Scanner"5.  Imagesetter"

Deadlock Prevention Circular Wait Condition

n  Impose an order of requests for all resources n  Method

n  Assign a unique id to each resource n  All resource requests must be in an ascending

order of the ids n  Release resources in a descending order

n  Can you prove this method has no circular wait?

n  Is this generally feasible?

Deadlock Prevention Overview

Condition Approach

Mutual exclusion Spool everything

Hold and wait Request all resource initially No preemption Take resources away

Circular wait Order resources numerically

Non-resource Deadlocks

n  Possible for two processes to deadlock n  Each is waiting for the other to do some task

n  Can happen with semaphores n  Each process required to do a down() on two

semaphores (mutex and another) n  If done in wrong order, deadlock results

Preempting Scheduler Activations

n  So how do they handle this deadlock?

Thread 1" Upcall"

Acquire spin lock""

Start queue maintenance"

Interrupt Thread 1""Acquire spin lock -> block"

Preempting Scheduler Activations n  Detection and recovery (like in Mach) n  Basic idea:

n  Upcall handler checks first the state of each interrupted thread

n  If it is in a critical section allow it to finish this n  Implementation:

n  Protect critical sections with spin locks n  Acquire_spin_lock() increments a counter in the

thread’s descriptor n  Upcall handler checks spin lock count of all interrupted

threads n  If there are threads that hold spin locks set flag n  Switch to the context of these threads n  Afterwards switch back to upcall handler

Summary

n  Resource n  Introduction to deadlocks n  Strategies

n  Ostrich algorithm n  Deadlock detection and recovery n  Deadlock avoidance n  Deadlock prevention

n  Non-resource deadlocks