Principles of Operating Systems: Lecture 11 - Deadlocks Ardalan Amiri Sani
Principles of Operating Systems: Lecture 11 - Deadlocks Ardalan Amiri Sani
Operating Systems
Lecture 11 - Deadlocks
Ardalan Amiri Sani (ardalan@uci.edu)
[lecture slides contains some content adapted from previous slides by Prof. Nalini Venkatasubramanian, and
course text slides Silberschatz]
1
Outline
System Model
Deadlock Characterization
Methods for handling deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
2
The Deadlock Problem
3
Definitions
4
Example - Bridge Crossing
5
System Model
System consists of resources
Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances.
Each process utilizes a resource as
follows:
request
use
release
6
Conditions for Deadlock
Deadlock can arise if four conditions hold simultaneously.
8
Resource Allocation Graph
Process
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
9
Graph with no cycles
10
Graph with cycles (but no
deadlock)
11
Graph with cycles and deadlock
12
Basic facts
13
14
Methods for handling deadlocks
18
Deadlock Avoidance
Requires that the system has some additional a priori
information available
19
Safe state
When a process requests an available resource, system
must decide if immediate allocation leaves the system in a
safe state
System is in safe state if there exists a sequence <P1, P2,
, Pn> of ALL the processes in the systems such that for
each Pi, the resources that Pi can still request can be
satisfied by currently available resources + resources held
by all the Pj, with j < i
That is:
If Pi resource needs are not immediately available, then
Pi can wait until all Pj have finished
When Pj is 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 20
Basic Facts
If a system is in safe state no deadlocks
21
Safe, Unsafe, Deadlock State
22
Avoidance Algorithms
Single instance of a resource type
Use a resource-allocation graph
23
Resource Allocation Graph
Scheme
Claim edge Pi Rj indicated that process Pj may
request resource Rj; represented by a dashed line
Claim edge converts to request edge when a process
requests a resource
Request edge converted to an assignment edge when
the resource is allocated to the process
When a resource is released by a process,
assignment edge reconverts to a claim edge
Resources must be claimed a priori in the system
24
Resource Allocation Graph (aka
Claim Graph)
25
Unsafe State in Resource
Allocation Graph
26
Resource Allocation Graph
Algorithm
Suppose that process Pi requests a resource Rj
The request can be granted only if converting the
request edge to an assignment edge does not result in
the formation of a cycle in the resource allocation graph
27
Bankers Algorithm
Multiple instances
28
Data Structures for the Bankers
Algorithm
Let n = number of processes, and m = number of resources types.
29
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work = Available
Finish [i] = false for i = 0, 1, , n- 1
4. If Finish [i] == true for all i, then the system is in a safe state
30
Resource-Request Algorithm for
Process Pi
Requesti = request vector for process Pi. If Requesti [j] = k then
process Pi wants k instances of resource type Rj
1. If Requesti Needi go to step 2. Otherwise, raise error
condition, since process has exceeded its maximum claim
2. If Requesti Available, go to step 3. Otherwise Pi must wait,
since resources are not available
3. Pretend to allocate requested resources to Pi by modifying the
state as follows:
Available = Available Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi Requesti;
If safe the resources are allocated to Pi
If unsafe Pi must wait, and the old resource-allocation state
is restored
31
Example of Bankers Algorithm
5 processes P0 through P4;
3 resource types:
A (10 instances), B (5 instances), and C (7 instances)
Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 0 1 0 753 332
P1 2 0 0 322
P2 3 0 2 902
P3 2 1 1 222
P4 0 0 2 433
32
Example (Cont.)
The content of the matrix Need is defined to be Max Allocation
Need
ABC
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
The system is in a safe state since the sequence < P1, P3, P4, P2, P0>
satisfies safety criteria
33
Example: P1 Request (1,0,2)
Check that Request Available (that is, (1,0,2) (3,3,2) true
Allocation Need Available
ABC ABC ABC
P0 0 1 0 743 230
P1 3 0 2 020
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2>
satisfies safety requirement
34
Deadlock Detection
Allow system to enter deadlock state
Detection algorithm
Recovery scheme
35
Single Instance of Each Resource
Type
Maintain wait-for graph
Nodes are processes
Pi Pj if Pi is waiting for Pj
36
Resource-Allocation Graph and
Wait-for Graph
38
Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively
Initialize:
(a) Work = Available
(b) For i = 1,2, , n, if Allocationi 0, then
Finish[i] = false; otherwise, Finish[i] = true
39
Detection Algorithm (Cont.)
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
40
Example of Detection Algorithm
Five processes P0 through P4; three resource types A (7 instances), B (2
instances), and C (6 instances)
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i
41
Example (Cont.)
P2 requests an additional instance of type C
Request
ABC
P0 0 0 0
P1 2 0 2
P2 0 0 1
P3 1 0 0
P4 0 0 2
State of system?
Can reclaim resources held by process P0, but insufficient
resources to fulfill other processes; requests
Deadlock exists, consisting of processes P1, P2, P3, and P4
42
Detection-Algorithm Usage
When, and how often, to invoke depends on:
How often a deadlock is likely to occur?
How many processes will need to be rolled back?
- one for each disjoint cycle
43
Recovery from Deadlock: Process
Termination
Abort all deadlocked processes
44
Recovery from Deadlock:
Resource Preemption
Selecting a victim minimize cost
Rollback return to some safe state, restart process for that state
45