Upload
camden-brisendine
View
229
Download
2
Tags:
Embed Size (px)
Citation preview
11
Chapter 3: DeadlocksInstructor: Hengming Zou, Ph.D.
In Pursuit of Absolute Simplicity 求于至简,归于永恒
22
Content
Resource Deadlocks Deadlock modeling Deadlock detection and recovery Deadlock avoidance and prevention
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
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
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!
66
Resources
Sequence of events required to use a resource
1. request the resource
2. use the resource
3. release the resource
77
Resources
Must wait if request is denied– requesting process may be blocked
– may fail with error code
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
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
1010
Deadlocks
In a deadlock, none of the processes can …– run
– release resources
– be awakened
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
1212
Deadlocks
Example
thread A thread B
lock(x) lock(y)
lock(y) lock(x)
... ...
unlock(y) unlock(x)
unlock(x) unlock(y)
1313
Deadlocks
Can deadlock occur with code? Will deadlock always occur with this code?
1414
Dining Philosophers Problem
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
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?
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
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
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.
2020
Deadlock Modeling
Modeled with directed graphs
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
2222
Deadlock Modeling
2323
(o) (p) (q)
How deadlock can be avoided
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
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
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?
2727
Detection with Wait-for Graph
2828
Detection with Wait-for Graph
2929
An example for the deadlock detection algorithm
Detection with Wait-for Graph
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
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
3232
Deadlock Avoidance
Don’t get in a position where– Deadlock becomes possible
Use:– Resource Trajectories
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
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
3535
Deadlock Prevention
Idea is to eliminate one of the four necessary conditions Increase resources to decrease waiting
– this minimizes chance of deadlock
3636
Deadlock Prevention
Attacking the Mutual Exclusion Condition Attacking the Hold and Wait Condition Attacking the No Preemption Condition Attacking the Circular Wait Condition
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
3838
Attacking Mutual Exclusion
Principle: Avoid assigning resource when not absolutely necessary As few processes as possible actually claim the resource
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
4040
Attacking Hold & Wait Condition
phase 1a. acquire all resources phase 1b. while (not done) { acquire some resource work } phase 2. release all resources
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
4242
Attacking Hold & Wait Condition
Problems? May not know required resources at start of run Ties up resources others could be using
4343
Attacking Hold & Wait Condition
Variation: Process must give up all resources Then request all immediately needed
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?
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
– !!??
4646
Attacking Circular Wait Condition
Normally ordered resources– Imagesetter scanner plotter printer
A resource graph
4747
Banker’s Algorithm
Derive from methods by bankers to grant loans Similar to reserving all resources at beginning
– but more efficient
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
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
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
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
5252
Banker’s Algorithm
Example: use banker’s algorithm to model a bank loaning money to its customers
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
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
5555
Banker’s Algorithm
Can bank approve all these credit lines– if it promises to give money upon request if available?
5656
Solution #2: Banker’s Algorithm
bank approves all credit limits but customer may have to wait
– when actually asking for the money
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?
5858
Banker’s Algorithm
Allow iff, after giving the money, there exists some sequential order of fulfilling all maximum resources
– worst-case analysis
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
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)
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?
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.
6363
Banker’s Algorithm
How to apply banker’s algorithm to dining philosophers? Unfortunately, it’s difficult to anticipate maximum resources
needed
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
6565
Banker's Algorithm for Multiple Resources
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
6767
Other Issues
Two-Phase Locking Nonresource Deadlocks Starvation
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)
6969
Two-Phase Locking
If phase one succeeds, it starts second phase, – performing updates
– releasing locks
7070
Two-Phase Locking
Note similarity to requesting all resources at once Algorithm works where programmer can arrange
– program can be stopped, restarted
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
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
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
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*/
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*/
}
}
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*/
}
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);
}
7878
A Solution with Semaphore
void test(i)
{
if(state[i]==HUNGRY && state[LEFT]!=EATING && state[RIGHT]!=EATING) {
state[i]=EATING;
up(&s[i]);
}
}
Computer Changes Life