Deadlock
Deadlock
Deadlock
Objectives Outline
• To develop a description of • The Deadlock Problem
deadlocks, which prevent sets of • System Model
concurrent processes from completing • Deadlock Characterization
their tasks • Methods for Handling Deadlocks
• Deadlock Prevention
• To present a number of different • Deadlock Avoidance
methods for preventing or avoiding
• Deadlock Detection
deadlocks in a computer system
• Recovery from Deadlock
1
The Deadlock Problem
2
Bridge Crossing Example
section1 section2
3
System Model
4
Deadlock Characterization
5
Resource-Allocation Graph
6
Resource-Allocation Graph (Cont.)
• Process
• Pi requests instance of Rj
Pi
Rj
• Pi is holding an instance of Rj
Pi
Rj
7
Resource Allocation Graph With A
Deadlock
There is a cycle
and
Deadlock
8
Graph With A Cycle But No Deadlock
There is a cycle
but
No Deadlock
9
Basic Facts
10
Methods for Handling Deadlocks
• Ignore the problem and pretend that deadlocks never occur in the system;
– Used by most operating systems, including UNIX
– OS does not bother with deadlocks that can occur in applications
11
Deadlock Prevention
• Mutual Exclusion – not required for sharable resources; must hold for
nonsharable resources
– Example: spooling for printer
• Hold and Wait – must guarantee that whenever a process requests a
resource, it does not hold any other resources
– Require process to request and allocated all its resources at beginning
– Allow process to request resources only when it has none
12
Deadlock Prevention (Cont.)
• No Preemption –
– A process holding resources makes request: if request cannot be granted,
release (preempt) the held resources, and try again later.
– Preempted resources are added to the list of resources for which the
process is waiting
– Process will be restarted only when it can regain its old resources, as well
as the new ones that it was requesting.
• Circular Wait – impose a total ordering of all resource types, and require that
each process requests resources in an increasing order
13
Deadlock Prevention (Cont.)
Resources
R1 R2 R3 R4 R5
Example:
Process 1 Process 2 Process 3
Request R2 Request R1 Request R3
Request R4 Request R2 Request R4
Request R3
14
Deadlock Avoidance
• Simplest and most useful model requires that each process declare the
maximum number of resources of each type that it may need
to hold simultaneously. (maximum demand)
15
Safe state
• A state is safe if the system can allocate resources to each process (up to its
maximum) in some order and still avoid a deadlock.
16
Safe state
• More formally: A system state is safe if there exists a safe sequence of all
processes (<P1, P2, …, Pn>) such that for each Pi, the resources that Pi can
still request can be satisfied by
currently available resources + resources held by all processes earlier in
the sequence (all Pj, with j < i)
• That is:
– If Pi resource needs are not immediately available, then Pi can wait until all
Pj (j < i) have finished
– When Pj finished, Pi can obtain needed resources, execute, return
allocated resources, and terminate
– When Pi terminates, Pi +1 can obtain its needed resources, and so on.
17
Basic Facts
18
Safe, Unsafe , Deadlock State
19
Avoidance Algorithms
20
Resource-Allocation Graph Scheme
21
Resource-Allocation Graph
22
Resource-Allocation Graph
24
Resource-Allocation Graph Algorithm
25
Banker’s Algorithm
• Multiple instances
•
– System will know maximum demand of each process
• When a process gets all its resources, it must return them in a finite amount of
time
26
Simple Example
27
Simple Example
28
Data Structures for the Banker’s Algorithm
• Detection algorithm
• Recovery scheme
47
Single Instance of Each Resource Type
48
References
• The slides here are adapted/modified from the textbook and its slides:
Operating System Concepts, Silberschatz et al., 7th, 8th, 9th editions, Wiley.
REFERENCES
• Operating System Concepts, Silberschatz et al. Wiley.
• Modern Operating Systems, Andrew S. Tanenbaum.
49
50