OS-Chap-7 Deadlocks
OS-Chap-7 Deadlocks
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 7: Deadlocks
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
Operating System Concepts – 9th Edition 7.2 Silberschatz, Galvin and Gagne ©2013
Chapter Objectives
Operating System Concepts – 9th Edition 7.3 Silberschatz, Galvin and Gagne ©2013
The Deadlock Problem
A set of blocked processes each holding a resource and waiting to
acquire a resource held by another process in the set.
Example:
System has 2 disk drives.
P1 and P2 each hold one disk drive, and each needs another one.
Example:
semaphores A and B, initialized to 1
P0 P1
wait (A); wait (B)
wait (B); wait (A)
Operating System Concepts – 9th Edition 7.4 Silberschatz, Galvin and Gagne ©2013
Bridge Crossing Example
S1 S2
Operating System Concepts – 9th Edition 7.5 Silberschatz, Galvin and Gagne ©2013
System Model
Operating System Concepts – 9th Edition 7.6 Silberschatz, Galvin and Gagne ©2013
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously:
Operating System Concepts – 9th Edition 7.7 Silberschatz, Galvin and Gagne ©2013
Deadlock with Mutex Locks
Deadlocks can occur via system calls, locking, etc.
See example box in text page 318 for mutex deadlock.
Operating System Concepts – 9th Edition 7.8 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph
A set of vertices V and a set of edges E.
V is partitioned into two types:
P = {P1, P2, …, Pn}, the set consisting of all the processes
in the system.
Operating System Concepts – 9th Edition 7.9 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph (Cont.)
Process:
Pi
Rj
Pi is holding an instance of Rj (Hold)
Pi
Rj
Operating System Concepts – 9th Edition 7.10 Silberschatz, Galvin and Gagne ©2013
Example of a Resource Allocation Graph
Operating System Concepts – 9th Edition 7.12 Silberschatz, Galvin and Gagne ©2013
Graph With A Cycle But No Deadlock
Operating System Concepts – 9th Edition 7.13 Silberschatz, Galvin and Gagne ©2013
Basic Facts
Operating System Concepts – 9th Edition 7.14 Silberschatz, Galvin and Gagne ©2013
Methods for Handling Deadlocks
1. Ensure that the system will never enter a deadlock state (using a
protocol to prevent or avoid deadlocks).
Deadlock prevention ensures that at least one of the
necessary conditions cannot hold.
This way deadlock will never happen.
Deadlock avoidance requires that the operating system be
given additional information in advance concerning which
resources a process will request and use during its lifetime.
Request is granted if it is not going to cause deadlock.
Request is rejected if it is going to cause deadlock.
2. Allow the system to enter a deadlock state and then recover.
3. Ignore the problem and pretend that deadlocks never occur in the
system; used by most operating systems, including UNIX.
Operating System Concepts – 9th Edition 7.15 Silberschatz, Galvin and Gagne ©2013
Deadlock Prevention
Restrain (control) the ways request can be made. Example: Read-
Only files are
How to break Mutual Exclusion – not required for sharable sharable. So, two
resources (e.g., read-only files); must hold for non-sharable processes can use a
resources. read-only file at the
same time. In this
How to break Hold and Wait – must guarantee that whenever a
case, we do not
process requests a resource, it does not hold any other resources. need “mutual
Case 1: Require process to request and be allocated all its exclusion”. So, no
resources before it begins execution. deadlock on using
read-only files.
Case 2: or allow process to request resources only when the
process has none allocated to it.
Example: Printer is
Low resource utilization; starvation possible. not sharable. So,
we must have
• Example of Case 1 • Example of Case 2 “mutual
• A process needs to copy data from • A process needs to copy data from exclusion” here.
tape to disk, then it prints data tape to disk, then it prints data So, deadlock is
from disk to printer. from disk to printer. possible in this case.
• Here, this process can only start if it • First, hold tape and disk and make
is given all 3 resources at the same copy.
time. • Second, release tape and disk and
• Disadvantage: Low resource then
utilization. Why holding the printer • Third, request disk and printer to
from the start while we really need it make print.
at the end.
Operating System Concepts – 9th Edition 7.16 Silberschatz, Galvin and Gagne ©2013
Deadlock Prevention (Cont.)
How to break No Preemption –
If a process that is holding some resources requests another resource that
cannot be immediately allocated to it, then all resources currently being held are
released.
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 is requesting.
How to break Circular Wait – impose a total ordering of all resource types and
require that each process requests resources in an increasing order of enumeration.
Operating System Concepts – 9th Edition 7.17 Silberschatz, Galvin and Gagne ©2013
Deadlock Example
/* thread one runs in this function */
void *do_work_one(void *param)
{
pthread_mutex_lock(&first_mutex);
Suppose sequence of execution:
pthread_mutex_lock(&second_mutex); 1- Thread 1 obtaining lock on first mutex.
/** * Do some work */ 2- Thread 2 obtaining lock on second mutex.
pthread_mutex_unlock(&second_mutex); 3- Thread 1 try to obtain lock on second mutex.
4- Thread 2 try to obtain lock on first mutex.
pthread_mutex_unlock(&first_mutex);
pthread_exit(0);
• Thread 1 cannot obtain lock on second mutex
} because the lock is being held by thread 2.
/* thread two runs in this function */ • Thread 2 cannot obtain lock on first mutex
because the lock is being held by thread 1.
void *do_work_two(void *param)
{
pthread_mutex_lock(&second_mutex);
pthread_mutex_lock(&first_mutex);
/** * Do some work */
pthread_mutex_unlock(&first_mutex);
pthread_mutex_unlock(&second_mutex);
pthread_exit(0);
}
Operating System Concepts – 9th Edition 7.18 Silberschatz, Galvin and Gagne ©2013
Deadlock Example with Lock Ordering
void transaction(Account from, Account to, double amount)
{
mutex lock1, lock2;
lock1 = get_lock(from); Suppose sequence of execution:
lock2 = get_lock(to); 1- Thread 1 obtaining lock on “from” (Account A).
2- Thread 2 obtaining lock on “from” (Account B).
acquire(lock1); 3- Thread 1 try to obtain lock on ”To” (Account B).
acquire(lock2); 3- Thread 2 try to obtain lock on ”To” (Account A).
withdraw(from, amount);
deposit(to, amount); • Thread 1 cannot obtain lock on Account B because
the lock is being held by thread 2.
release(lock2);
• Thread 2 cannot obtain lock on Account A because
release(lock1); the lock is being held by thread 1.
}
Operating System Concepts – 9th Edition 7.19 Silberschatz, Galvin and Gagne ©2013
Deadlock Avoidance
Requires that the system has some additional a priori information
available:
Operating System Concepts – 9th Edition 7.20 Silberschatz, Galvin and Gagne ©2013
Safe State
Operating System Concepts – 9th Edition 7.21 Silberschatz, Galvin and Gagne ©2013
Example of Safe State
Number of existing tape drives = 12
P0, P1, P2 are currently holding 5, 2, 2 tape drives, respectively.
Operating System Concepts – 9th Edition 7.22 Silberschatz, Galvin and Gagne ©2013
Example of Unsafe State
Suppose P2 requests 1 more tape than previous example and it is given the
tape drive. Is the system in a safe state now?
Operating System Concepts – 9th Edition 7.24 Silberschatz, Galvin and Gagne ©2013
Safe, Unsafe, Deadlock State
Operating System Concepts – 9th Edition 7.25 Silberschatz, Galvin and Gagne ©2013
Avoidance Algorithms
Operating System Concepts – 9th Edition 7.26 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph Scheme
Claim edge Pi Rj indicated that process Pi 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.
Operating System Concepts – 9th Edition 7.27 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph Algorithm
Operating System Concepts – 9th Edition 7.28 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph
Assign Edge
R1 is assigned
Request Edge
to P1. So, it can
P2 is requesting
Use it.
Resource R1.
Claim Edge
P1 may use
R2 in future. • Suppose P2 requests R2.
• Should R2 be assigned to P2?
• Avoiding deadlocks using • Look Next Slide.
resource allocation graph only
works when each resource in the
graph have only one instance.
Operating System Concepts – 9th Edition 7.29 Silberschatz, Galvin and Gagne ©2013
Unsafe State in Resource-Allocation Graph
1 2 3 4
Operating System Concepts – 9th Edition 7.30 Silberschatz, Galvin and Gagne ©2013
Banker’s Algorithm
Multiple instances.
Operating System Concepts – 9th Edition 7.31 Silberschatz, Galvin and Gagne ©2013
Data Structures for the Banker’s Algorithm
Operating System Concepts – 9th Edition 7.32 Silberschatz, Galvin and Gagne ©2013
Data Structures for the Banker’s Algorithm
Five processes P0 through P4; Three resource types:
A (10 instances), B (5 instances, and C (7 instances).
Snapshot at time T0:
Allocation Max Need Available
ABC ABC ABC ABC
P0 010 753 743 332
P1 200 322 122
P2 302 902 600
P3 211 222 011
P4 002 433 431
When we say Needi <= work, the answer is true only if Need[i, j] <= Work[j] for all j.
Example: Suppose Need1 = {1, 2, 2} and work = {3, 3, 2}
Need1 <= Work because 1 <= 3 and 2 <= 3 and 2 <= 2.
Operating System Concepts – 9th Edition 7.34 Silberschatz, Galvin and Gagne ©2013
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 Important: If the algorithm
does not lead to safe state,
Allocationi = Allocationi + Requesti then, “Available”, “Allocation”,
Needi = Needi – Requesti and “Need” should get their
old values.
4. If safe The resources are allocated to Pi
If unsafe Pi must wait, and the old resource-allocation state is restored.
The idea behind this algorithm is simple.
When a process request resource, then:
1- Pretend that you allocated the requested resources for the process.
2- Is the system in safe state?
3- If yes, then the process can be given resources it wants.
4- If no, then the process cannot be given resources it wants.
Operating System Concepts – 9th Edition 7.35 Silberschatz, Galvin and Gagne ©2013
Example of Banker’s Algorithm
Need[i, j] = Max[i, j] – Allocation[i, j] Initially, Work = Available
If (Finish[i]==false and Needi <= Work) Work = Work + Allocationi
5 processes P0 through P4;
3 resource types: A B C
A (10 instances), B (5 instances), and C (7 instances) 10 5 7
Snapshot at time T0: Need
<=
Allocation Max Available Finish Work
Work
A B C
ABC ABC ABC
3 3 2 P0 010 753 332 F T N Y
5 3 2 F T Y
7 4 3
P1 200 322
7 4 5 P2 302 902 F T N Y
7 5 5 T Y
P3 211 222 F
10 5 7
P4 002 433 F T Y
The system is in a safe state since the sequence < P1, P3, P4, P0, P2>
satisfies safety criteria.
Operating System Concepts – 9th Edition 7.36 Silberschatz, Galvin and Gagne ©2013
Example (Cont.)
The content of the matrix Need is defined to be Max – Allocation
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
The system is in a safe state since the sequence < P1, P3, P4, P0, P2>
satisfies safety criteria.
We knew the system is in safe state by following
the safety algorithm (Look Next Slide).
Operating System Concepts – 9th Edition 7.37 Silberschatz, Galvin and Gagne ©2013
Example (Cont.)
1) Finish = {false, false, false, false, false} All these also lead to safe state
• <P1, P3, P4, P2, P0> or
2) Work = Available = {3, 3, 2} • <P1, P3, P2, P4, P0> or
3) P0, Need0 = {7, 4, 3} is Need0 <= work ? No • <P3, P1, P4, P2, P0> or
• <P3, P1, P2, P4, P0>
P1, Need1 = {1, 2, 2} is Need1 <= work ? Yes
Finish = {false, true, false, false, false} and Work = {5, 3, 2}
P2, Need2 = {6, 0, 0} is Need2 <= work ? No
P3, Need3 = {0, 1, 1} is Need3 <= work ? Yes
Finish = {false, true, false, true, false} and Work = {7, 4, 3} Notice that when all
P4, Need4 = {4, 3, 1} is Need4 <= work ? Yes processes finish their
work, then the number of
Finish = {false, true, false, true, true} and Work = {7, 4, 5}
available resources
P0, Need0 = {7, 4, 3} is Need0 <= work ? Yes before and after applying
Finish = {true, true, false, true, true} and Work = {7, 5, 5} algorithm must be equal.
Otherwise, you know you
P2, Need2 = {6, 0, 0} is Need2 <= work ? Yes
did something wrong.
Finish = {true, true, true, true, true} and Work = {10, 5, 7}
Now, all values in Finish vector is true, so, the system is in safe state
In other words, all processes can finish their work. The sequence is:
P1, P3, P4, P0, P2
Operating System Concepts – 9th Edition 7.38 Silberschatz, Galvin and Gagne ©2013
Example (Cont.)
Operating System Concepts – 9th Edition 7.39 Silberschatz, Galvin and Gagne ©2013
Example: P1 Request (1,0,2)
Initially, Work = Available
If (Finish[i]==false and Needi <= Work) Work = Work + Allocationi
1. Check that Request Need (that is, (1,0,2) (1,2,2) true
2. Check that Request Available (that is, (1,0,2) (3,3,2) true
3. Available = Available – Requesti | Allocationi = Allocationi + Requesti | Needi = Needi – Requesti
4. Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies safety
requirement so request can be granted. What about <P1,P4,P3,P0,P2>,
<P1,P4,P3,P2, P0 >?
Operating System Concepts – 9th Edition 7.40 Silberschatz, Galvin and Gagne ©2013
Example P1 Request (1,0,2) (Cont.)
Notice that when all processes
finish their work, then the number
1) Finish = {false, false, false, false, false}
of available resources after
2) Work = Available = {2, 3, 0} applying algorithm must be equal
to original number of resources.
3) P0, Need0 = {7, 4, 3} is Need0 <= work ? No Otherwise, you know you did
P1, Need1 = {0, 2, 0} is Need1 <= work ? Yes something wrong.
Finish = {false, true, false, false, false} and Work = {5, 3, 2}
P2, Need2 = {6, 0, 0} is Need2 <= work ? No
P3, Need3 = {0, 1, 1} is Need3 <= work ? Yes
Finish = {false, true, false, true, false} and Work = {7, 4, 3}
P4, Need4 = {4, 3, 1} is Need4 <= work ? Yes
Result is safe
Finish = {false, true, false, true, true} and Work = {7, 4, 5}
state, So, P1
P0, Need0 = {7, 4, 3} is Need0 <= work ? Yes request is
granted.
Finish = {true, true, false, true, true} and Work = {7, 5, 5}
P2, Need2 = {6, 0, 0} is Need2 <= work ? Yes
Finish = {true, true, true, true, true} and Work = {10, 5, 7}
Operating System Concepts – 9th Edition 7.41 Silberschatz, Galvin and Gagne ©2013
Example: P4 Request (1,2,0)
Initially, Work = Available
If (Finish[i]==false and Needi <= Work) Work = Work + Allocationi
1. Check that Request Need (that is, (1,2,0) (4,3,1) true
2. Check that Request Available (that is, (1,2,0) (2,3,0) true
3. Available = Available – Requesti | Allocationi = Allocationi + Requesti | Needi = Needi – Requesti
4. Executing safety algorithm shows that we can’t find a sequence in which all
processes finish their work. The system is not in safe state.
Operating System Concepts – 9th Edition 7.42 Silberschatz, Galvin and Gagne ©2013
Example P4 Request (1,2,0) (Cont.)
Could not find a sequence in which all processes finish their work.
Unsafe system state. So, restore allocation, need, and available values:
• Need4 = {4, 3, 1}
• Allocation4 = {0, 0, 2}
• Available = {2, 3, 0}
Operating System Concepts – 9th Edition 7.43 Silberschatz, Galvin and Gagne ©2013
Example of Banker’s Algorithm
Need [i,j] = Max[i,j] – Allocation [i,j] Initially, Work = Available
If (Finish[i]==false and Needi <= Work) Work = Work + Allocationi
5 processes P0 through P4;
3 resource types: A B C
10 5 7
A (10 instances), B (5instances), and C (7 instances)
Snapshot at time T0:
Need
Allocation Max Available Finish <=
Work Work
A B C
ABC ABC ABC
5 3 2 P0 010 953 332 F N
7 4 3
P1 200 322 F T Y
7 4 5
P2 302 11 0 2 F N
P3 211 222 F T Y
P4 002 433 F T Y
• Check if the system is in a safe state?
<P1, P3, P4> unsafe state (Next Slide)
<P1, P4, P3> is also unsafe
Operating System Concepts – 9th Edition 7.44 Silberschatz, Galvin and Gagne ©2013
Example of Banker’s Algorithm (Cont.)
Notice that when all
processes finish their work,
1) Finish = {false, false, false, false, false}
then the number of available
2) Work = Available = {3, 3, 2} resources before and after
applying algorithm must be
3) P0, Need0 = {9, 4, 3} is Need0 <= work ? No equal. Otherwise, you know
P1, Need1 = {1, 2, 2} is Need1 <= work ? Yes you did something wrong.
Finish = {false, true, false, false, false} and Work = {5, 3, 2}
With this
P2, Need2 = {8, 0, 0} is Need2 <= work ? No sequence, the
P3, Need3 = {0, 1, 1} is Need3 <= work ? Yes system is not in
safe state.
Finish = {false, true, false, true, false} and Work = {7, 4, 3}
P4, Need4 = {4, 3, 1} is Need4 <= work ? Yes Try to find any
Finish = {false, true, false, true, true} and Work = {7, 4, 5} other sequence
that leaves the
P0, Need0 = {9, 4, 3} is Need0 <= work ? No system in safe
P2, Need2 = {8, 0, 0} is Need2 <= work ? No state. None
can be found.
Operating System Concepts – 9th Edition 7.45 Silberschatz, Galvin and Gagne ©2013
Deadlock Detection
Detection algorithm
Recovery scheme
Operating System Concepts – 9th Edition 7.46 Silberschatz, Galvin and Gagne ©2013
Single Instance of Each Resource Type
Operating System Concepts – 9th Edition 7.47 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph and Wait-for Graph
Operating System Concepts – 9th Edition 7.48 Silberschatz, Galvin and Gagne ©2013
Several Instances of a Resource Type
Available: A vector of length m indicates the number of
available resources of each type
Allocation: An n x m matrix defines the number of resources
of each type currently allocated to each process
Request: An n x m matrix indicates the current request of
each process. If Request [i][j] = k, then process Pi is
requesting k more instances of resource type Rj.
Operating System Concepts – 9th Edition 7.49 Silberschatz, Galvin and Gagne ©2013
Detection Algorithm
Operating System Concepts – 9th Edition 7.50 Silberschatz, Galvin and Gagne ©2013
Detection Algorithm (Cont.)
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
Operating System Concepts – 9th Edition 7.51 Silberschatz, Galvin and Gagne ©2013
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
Operating System Concepts – 9th Edition 7.52 Silberschatz, Galvin and Gagne ©2013
Example (Cont.)
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
Operating System Concepts – 9th Edition 7.53 Silberschatz, Galvin and Gagne ©2013
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
Operating System Concepts – 9th Edition 7.54 Silberschatz, Galvin and Gagne ©2013
Recovery from Deadlock: Process Termination
Operating System Concepts – 9th Edition 7.55 Silberschatz, Galvin and Gagne ©2013
Recovery from Deadlock: Resource Preemption
Operating System Concepts – 9th Edition 7.56 Silberschatz, Galvin and Gagne ©2013
End of Chapter 7
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013