Transcript Deadlocks

Deadlock
Chapter 10
7/18/2015
A. Berrached::cms::uhd
1
Deadlock
• System Model
• Deadlock Characterization
• Methods for Handling Deadlocks
– Deadlock Prevention
– Deadlock Avoidance
– Deadlock Detection & Recovery
7/18/2015
A. Berrached::cms::uhd
2
The Deadlock Problem
• A state where a set of processes are blocked each
holding a resource and waiting to acquire a resource
held by another process in the set.
• Example: Semaphores A and B initialized to 1
7/18/2015
P0:
wait(A);
wait(B);
P1:
wait(B);
wait(A);
….
…..
A. Berrached::cms::uhd
3
Process 1
….
Request (resource1);
/*holding res 1 */
…..
Request(resource2)
….
7/18/2015
Process 2
….
Request (resource2);
/*holding res 2 */
…..
Request(resource3);
…..
A. Berrached::cms::uhd
Process 3
…...
Request (resource3);
/*holding res 3 */
…..
Request(resource1);
…..
4
System Model
7/18/2015
A. Berrached::cms::uhd
5
Deadlock Characterization
7/18/2015
A. Berrached::cms::uhd
6
Resource Allocation Graph
7/18/2015
A. Berrached::cms::uhd
7
Resource Allocation Graph (cont.)
7/18/2015
A. Berrached::cms::uhd
8
Example of a graph with no
cycles
7/18/2015
A. Berrached::cms::uhd
9
Example of a graph with a cycle
7/18/2015
A. Berrached::cms::uhd
10
Basic Facts
7/18/2015
A. Berrached::cms::uhd
11
Methods for handling deadlocks
7/18/2015
A. Berrached::cms::uhd
12
Deadlock Prevention
7/18/2015
A. Berrached::cms::uhd
13
Deadlock Prevention (cont.)
7/18/2015
A. Berrached::cms::uhd
14
Deadlock Avoidance
7/18/2015
A. Berrached::cms::uhd
15
Safe State
7/18/2015
A. Berrached::cms::uhd
16
Basic Facts
7/18/2015
A. Berrached::cms::uhd
17
Banker's Algorithm
7/18/2015
A. Berrached::cms::uhd
18
Data Structures for the Banker's
Algorithm
7/18/2015
A. Berrached::cms::uhd
19
Safety Algorithm
Else if no i can be found where Finish[i]=flase and
Needi <= Work , then the system is in an unsafe state
7/18/2015
A. Berrached::cms::uhd
20
Resource Request Algorithm for
Process Pi
7/18/2015
A. Berrached::cms::uhd
21
Example of Banker's Algorithm
7/18/2015
A. Berrached::cms::uhd
22
Example (cont.)
7/18/2015
A. Berrached::cms::uhd
23
Example (cont.): P1 requests
(1,0,2)
7/18/2015
A. Berrached::cms::uhd
24
Deadlock Detection
• Allow system to enter deadlock state
• Detection Algorithm
• Recovery scheme
7/18/2015
A. Berrached::cms::uhd
25
Single Instance of Each Resource
Type
• Maintain a resource allocation graph
• Periodically invoke algorithm that searches
for cycles in the graph
• If there is a cycle ==> set of processes in
cycle are in deadlock
7/18/2015
A. Berrached::cms::uhd
26
Deadlock Detection
7/18/2015
A. Berrached::cms::uhd
27
RAG Reduction Algorithm
• Given the resource allocation graph of the
system at the current state. Determine if the
system is in deadlock.
• 1. if a process is not blocked (i.e. if all
resources the process is requesting to finish
its task are available), then reduce graph by
that process: remove the process node from
the graph, return all its allocated resources,
and remove all arrows from and to that
process.
7/18/2015
A. Berrached::cms::uhd
28
Deadlock Detection--RAG
Reduction
7/18/2015
A. Berrached::cms::uhd
29
Deadlock Detection--RAG
Reduction
2. Repeat step 1 until all process node are
removed or the graph can not be reduced
any further.
• If a graph can be reduce by all its processes,
then there is no deadlock
• If a graph can NOT be completely reduced
by all its processes then the "irreducible"
processes constitute the set of deadlocked
processes.
7/18/2015
A. Berrached::cms::uhd
30
7/18/2015
A. Berrached::cms::uhd
31
Deadlock Detection-- Usage
7/18/2015
A. Berrached::cms::uhd
32
Recovery From Deadlock
7/18/2015
A. Berrached::cms::uhd
33
Recovery From Deadlock
7/18/2015
A. Berrached::cms::uhd
34