Lect 05
Lect 05
Deadlocks
6.1. Resources
6.2. Introduction to deadlocks
6.3. The ostrich algorithm
6.4. Deadlock detection and recovery
6.5. Deadlock avoidance
6.6. Deadlock prevention
6.7. Other issues
1
Learning Outcomes
• Understand what deadlock is and how it
can occur when giving mutually exclusive
access to multiple resources.
• Understand several approaches to
mitigating the issue of deadlock in
operating systems.
– Including deadlock detection and recovery,
deadlock avoidance, and deadlock
prevention.
2
Resources
• Examples of computer resources
– printers
– tape drives
– Tables in a database
• 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
3
Resources & Deadlocks
4
Resource Access
5
Two example resource usage patterns
semaphore res_1, res_2; semaphore res_1, res_2;
void proc_A() { void proc_A() {
down(&res_1); down(&res_1);
down(&res_2); down(&res_2);
use_both_res(); use_both_res();
up(&res_2); up(&res_2);
up(&res_1); up(&res_1);
} }
void proc_B() { void proc_B() {
down(&res_1); down(&res_2);
down(&res_2); down(&res_1);
use_both_res(); use_both_res();
up(&res_2); up(&res_1);
up(&res_1); up(&res_2);
} }
6
Introduction to 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 currently held
resource
• None of the processes can …
– run
– release resources
– be awakened
7
Four Conditions for Deadlock
1. Mutual exclusion condition
• each resource assigned to 1 process or is available
2. Hold and wait condition
• process holding resources can request additional
3. No preemption condition
• previously granted resources cannot forcibly taken
away
4. 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
8
Deadlock Modeling
• Modeled with directed graphs
10
How deadlock occurs
Deadlock Modeling
11
How deadlock can be avoided
Deadlock
Strategies for dealing with Deadlocks
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
12
Approach 1: The Ostrich Algorithm
• Pretend there is no problem
• Reasonable if
– deadlocks occur very rarely
– cost of prevention is high
• Example of “cost”, only one process runs at a time
• UNIX and Windows takes this approach for
some of the more complex resource
relationships to manage
• It’s a trade off between
– Convenience (engineering approach)
– Correctness (mathematical approach)
13
Approach 2: Detection and
Recovery
• Need a method to determine if a system is
deadlocked.
• Assuming deadlocked is detected, we
need a method of recovery to restore
progress to the system.
14
Approach 2
Detection with One Resource of Each Type
16
Detection with Multiple Resources of Each
Type
∑ ij j j
C +
i =1
A = E
18
Detection with Multiple Resources of Each
Type
20
Example Deadlock Detection
E = (4 2 3 1) A = ( 2 1 0 0)
0 0 1 0 2 0 0 1
C = 2 0 0 1 R = 1 0 1 0
0 1 2 0 2 1 0 0
21
Example Deadlock Detection
E = (4 2 3 1) A = ( 2 1 0 0)
0 0 1 0 2 0 0 1
C = 2 0 0 1 R = 1 0 1 0
0 1 2 0 2 1 0 0
22
Example Deadlock Detection
E = (4 2 3 1) A = ( 2 2 2 0)
0 0 1 0 2 0 0 1
C = 2 0 0 1 R = 1 0 1 0
0 1 2 0 2 1 0 0
23
Example Deadlock Detection
E = (4 2 3 1) A = ( 2 2 2 0)
0 0 1 0 2 0 0 1
C = 2 0 0 1 R = 1 0 1 0
0 1 2 0 2 1 0 0
24
Example Deadlock Detection
E = (4 2 3 1) A = (4 2 2 1)
0 0 1 0 2 0 0 1
C = 2 0 0 1 R = 1 0 1 0
0 1 2 0 2 1 0 0
25
Example Deadlock Detection
E = (4 2 3 1) A = (4 2 2 1)
0 0 1 0 2 0 0 1
C = 2 0 0 1 R = 1 0 1 0
0 1 2 0 2 1 0 0
26
Example Deadlock Detection
E = (4 2 3 1) A = (4 2 2 1)
0 0 1 0 2 0 0 1
C = 2 0 0 1 R = 1 0 1 0
0 1 2 0 2 1 0 0
27
Example Deadlock Detection
E = (4 2 3 1) A = (4 2 3 1)
0 0 1 0 2 0 0 1
C = 2 0 0 1 R = 1 0 1 0
0 1 2 0 2 1 0 0
28
Example Deadlock Detection
• Algorithm terminates with no unmarked
processes
– We have no dead lock
29
Example 2: Deadlock Detection
• Suppose, P3 needs a CD-ROM as well as
2 Tapes and a Plotter
E = (4 2 3 1) A = ( 2 1 0 0)
0 0 1 0 2 0 0 1
C = 2 0 0 1 R = 1 0 1 0
0 1 2 0 2 1 0 1
30
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
31
Recovery from Deadlock
32
Approach 3
Deadlock Avoidance
• Instead of detecting deadlock, can we
simply avoid it?
– YES, but only if enough information is
available in advance.
• Maximum number of each resource required
33
Deadlock Avoidance
Resource Trajectories
35
Safe and Unsafe States
Note: We have 10 units
of the resource
36
Safe and Unsafe States
A requests one extra unit resulting in (b)
37
Safe and Unsafe State
• Unsafe states are not necessarily deadlocked
– With a lucky sequence, all processes may complete
– However, we cannot guarantee that they will
complete (not deadlock)
• Safe states guarantee we will eventually
complete all processes
• Deadlock avoidance algorithm
– Only grant requests that result in safe states
38
Bankers Algorithm
• Modelled on a Banker with Customers
– The banker has a limited amount of money to loan customers
• Limited number of resources
– Each customer can borrow money up to the customer’s credit
limit
• Maximum number of resources required
• Basic Idea
– Keep the bank in a safe state
• So all customers are happy even if they all request to borrow up to
their credit limit at the same time.
– Customers wishing to borrow such that the bank would enter an
unsafe state must wait until somebody else repays their loan
such that the the transaction becomes safe.
39
The Banker's Algorithm for a Single Resource
44
Approach 4: Deadlock Prevention
• Resource allocation rules prevent
deadlock by prevent one of the four
conditions required for deadlock from
occurring
– Mutual exclusion
– Hold and wait
– No preemption
– Circular Wait
45
Approach 4
Deadlock Prevention
Attacking the Mutual Exclusion Condition
• Not feasible in general
– Some devices/resource are intrinsically not
shareable.
46
Attacking the Hold and Wait
Condition
• Require processes to request resources before starting
– a process never has to wait for what it needs
• Issues
– may not know required resources at start of run
• ⇒ not always possible
– also ties up resources other processes could be using
• Variations:
– process must give up all resources if it would block hold a resource
– then request all immediately needed
– prone to starvation
47
Attacking the No Preemption Condition
48
Attacking the Circular Wait Condition
(a) (b)
49
Attacking the Circular Wait
Condition
• The displayed deadlock
cannot happen
1 2
– If A requires 1, it must
acquire it before
acquiring 2
– Note: If B has 1, all
higher numbered
resources must be free or A B
held by processes who
doesn’t need 1
• Resources ordering is a
common technique in
practice!!!!! 50
51
Summary of approaches to
deadlock prevention
Condition Approach
• Mutual Exclusion • Not feasible
• Hold and Wait • Request resources
initially
• No Preemption • Take resources away
• Circular Wait • Order resources
52
Starvation
• A process never receives the resource it is waiting for,
despite the resource (repeatedly) becoming free, the
resource is always allocated to another waiting process.
• One solution:
– First-come, first-serve policy
53