Title: Deadlocks
1Deadlocks
2Chapter 8 Deadlocks
- System Model
- Deadlock Characterization
- Methods for Handling Deadlocks
- Deadlock Prevention
- Deadlock Avoidance
- Deadlock Detection
- Deadlock Recovery
- Combined Approach
3The 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 two tape drives.
- P1 and P2 each hold one tape drive and each needs
another one. - Example
- Semaphores A and B, initialized to 1
- P0 P1
- wait (A) wait(B)
- wait (B) wait(A)
4Deadlock by Out of Order Mutex
- semaphore S 1
- semaphore Q 1
- P0 P1
- wait(S) wait(Q)
- wait(Q) wait(S)
- ? ?
- signal(S) signal(Q)
- signal(Q) signal(S)
- If P0 waits on S and P1 waits on Q,
simultaneously, then if P0 waits on Q, it will
wait indefinitely for P1 to signal Q.
5Deadlock by Out of Order Mutex
Deadlock-free code Code with potential deadlock
- Typedef int semaphore
- semaphore resource_1
- semaphore resource_2
- void process_A(void)
- wait(resource_1)
- wait(resource_2)
- use_both_resources()
- signal(resource_2)
- signal(resource_1)
-
- void process_B(void)
- wait(resource_1)
- wait(resource_2)
- use_both_resources()
- signal(resource_2)
- signal(resource_1)
Typedef int semaphore semaphore
resource_1 semaphore resource_2 void
process_A(void) wait(resource_1)
wait(resource_2) use_both_resources()
signal(resource_2) signal(resource_1) void
process_B(void) wait(resource_2)
wait(resource_1) use_both_resources()
signal(resource_2) signal(resource_1)
6Deadlock in Multithreading
- define _REENTRANT
- include ltstdio.hgt
- include ltthread.hgt
- void counter(void ) //prototype for thread
counter subroutine - int count
- mutex_t count_lock
- main()
-
- char str80
- thread_t ctid
- //create the thread counter subroutine
- thr_create(NULL, 0, counter, 0, THR_NEW_LWP
THR_DETACHED, ctid)
7Deadlock in Multithreading
void counter(void arg) int i while(1)
printf(.) fflush(stdout) mutex_lock(count
_lock) count for (i0ilt5000i) mutex_un
lock(count_lock) for (i0ilt5000i) Ret
urn 0
- while(1)
- gets(str)
- thr_suspend(ctid)
- mutex_lock(count_lock)
- printf(\n\nCount d\n\n,
- count)
- mutex_unlock(count_lock)
- thr_continue(ctid)
-
-
- Return 0
-
- Problem arises when main thread suspends the
counter thread while the counter thread is
holding the mutex lock. Then main thread then
tries to lock the mutex variable which is already
held by the counter thread.
8Locking Guidelines
- Try not to hold locks across long operations such
as I/O - Dont hold locks when calling a function that is
outside the module and that might reenter the
module - Fix only those locks that have contention.
- When using multiple locks, make sure all threads
acquire the locks in the same order - Do not thread global process operations (e.g file
I/O) - For thread-specific behaviour, use thread
facilities - (e.g. thr_exit() on main() if you want to
terminate only the thread that is exiting main()).
9Bridge Crossing Example
- Traffic only in one direction.
- Each section of a bridge can be viewed as a
resource. - If a deadlock occurs, it can be resolved if one
car backs up (preempt resources and rollback). - Several cars may have to be backed up if a
deadlock occurs. - Starvation is possible.
10System Model
- 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 the resource (may have to wait...)
- Use the resource
- Release the resource
11Deadlock Characterization
Deadlock can arise if four conditions hold
simultaneously.
- Mutual exclusion only one process at a time can
use a resource. - Hold and wait a process holding at least one
resource is waiting to acquire additional
resources held by other processes. - No preemption a resource can be released only
voluntarily by the process holding it, after that
process has completed its task. - Circular wait there exists a set P0, P1, ,
P0 of waiting processes such that - P0 is waiting for a resource that is held by P1,
- P1 is waiting for a resource that is held by P2,
, - Pn1 is waiting for a resource that is held by
Pn, and - P0 is waiting for a resource that is held by P0.
12Resource-Allocation Graph
A set of vertices and a set of edges.
- Vertices are of two types
- P P1, P2, , Pn, processes in the system.
- R R1, R2, , Rm, resource types in the
system. - Request edge directed edge Pi ? Rj
- Assignment edge directed edge Rj ? Pi
13Resource-Allocation Graph
- ProcessResource Type with 4 instances
- Pi requests instance of Rj
- Pi is holding an instance of Rj
Pi
Rj
14Example of a Resource Allocation Graph
P1 is requesting an instance of resource type R1.
P1 is holding an instance of resource type R2.
15Resource Allocation Graph with a Deadlock
- If a graph contains a cycle then there might be a
deadlock.
16Resource Allocation Graph with a Cycle but No
Deadlock
- P4 could release resource R2, so there is no
deadlock.
17Basic Facts
- If graph contains no cycles ?
- no deadlock.
- If graph contains a cycle ?
- if only one instance per resource type, then
deadlock. - if several instances per resource type,
possibility of deadlock.
18Methods for Handling Deadlocks
- Prevention by structurally negating one of the
four conditions necessary to cause deadlock. - Dynamic avoidance by careful resource allocation.
- Detection and recovery Let deadlocks occur,
detect them, and take action. - Ignore the problem altogether.
19Osterich Algorithm
- Depends on how often the system crashes, and on
how serious a deadlock is. - e.g. Most systems have a limited number of
process table slots, open files or swap space on
the disk. Deadlocks could occur. - Windows and UNIX ignore the problem.
- The price for eliminating deadlocks is high, in
terms of restrictions that would be imposed.
Actually, this is folklore. Osteriches can
run at 60 km/hour and their kick is powerful
enough to kill a lion with visions of a big
chicken dinner! Tanenbaum, p. 167
201. Deadlock Prevention
Restrain the ways request can be made.
- Mutual Exclusion spool everything possible.
- But not all devices can be spooled
- (eg. Process table)
- Hold and Wait
- Require process to request and be allocated all
its resources before it begins execution, or - Allow process to request resources only when the
process has none. - gt But low resource utilization starvation
possible.
21Deadlock Prevention
- No Preemption
- Could release all resources that a job is holding
if it cant get one more. - 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. - gt But cant just preempt a plotter job in the
middle!
22Deadlock Prevention
- Circular Wait
- Allow one resource at a time? gt Not always
possible. - Provide a global numbering of all resource types,
and require that each process requests resources
in numerical order. - e.g. a process may request first a printer then
a tape drive, but not first a plotter then a
printer. - gt But when the resources include process table
slots, disk spooler space, locked database
records, etc. then the number of potential
resources and different uses may be so large that
no ordering could possibly work.
232. Dynamic Avoidance
Requires that the system has some additional a
priori information .
- Simplest and most useful model requires that each
process declare the maximum number of resources
of each type that it may need. - The deadlock-avoidance algorithm dynamically
examines the resource-allocation state to ensure
that there can never be a circular-wait
condition. - Resource-allocation state is defined by the
number of available and allocated resources, and
the maximum demands of the processes.
24Deadlock Detection
R
S
T
V
U
W
25Deadlock Detection
One resource of each type
- Process A holds R and wants S
- Process B holds nothing but wants T
- Process C holds nothing but wants S
- Process D holds U and wants S and T
- Process E holds T and wants V
- Process F holds W and wants S
- Process G holds V and wants U
26Deadlock Detection
- One algorithm out of many possibilities
- uses a list L of nodes and marks edges after
inspection. - For each node N, start at node N, and
- Initialize L to the empty list, unmark all edges
- Add current node to the end of L, check to see if
the node appears in L two times. If it does, a
cycle has been found. Exit algorithm. - From the given node, see if there any unmarked
outgoing edges. If none, go to Step 6. - Pick an unmarked outgoing edge at random and mark
it. Follow to new node, go to Step 3. - Now at a dead end. Remove last edge, go back to
last node. If not an initial node, go to Step 4. - If an initial node, go to Step 4 until all nodes
tested.
27Deadlock Detection
Yes
No
No
Yes
No
Yes
Yes
No
28Deadlock Detection
Multiple instances of each resource
Resources in existence
Resources available
(E1 , E2, E3 , Em)
(A1 , A2, A3 , Am)
Request matrix
Current allocation matrix
R11 R12 R13 R1m R21 R22 R23 R2m
.. .. .. .. .. Rn1 Rn2 Rn3 Rnm
- C11 C12 C13 C1m
- C21 C22 C23 C2m
- .. .. .. .. ..
- Cn1 Cn2 Cn3 Cnm
Row n is current allocation to process n
Row n is what process n needs.
29Resource Allocation Example
Tape Drives
Scanners
Tape Drives
Scanners
CD Drives
Plotters
CD Drives
Plotters
E ( 4 2 3 1 )
A ( 2 1 0 0 )
Resources in existence
Resources available to be allocated
C
Request matrix
Current allocation matrix
30Deadlock Detection
Multiple instances of each resource
- Define A ? B to mean that each element of vector
A is less than or equal to each element of vector
B. - Look for an unmarked process Pi for which the
i-th row of R is less than or equal to A - If such a process is found, add the i-th row of C
to A, mark the process and go back to Step 1 - If no such process exists, the algorithm
terminates. - On termination, all unmarked processes are
deadlocked.
31Resource Allocation Example
- Process 0 has one scanner
- Process 1 has two tape drives and a CD ROM.
- Process 2 has a plotter and two scanners
- Algorithm
- Process 0 cannot run because no CD is available.
- Process 1 cannot run because no scanner is free
- Process 2 can run.
- Process 2 returns all resources, giving A ( 2
2 2 0 ) - Then Process 1 runs, then gives A ( 4 2 2 1
) - Then Process 0 runs
32Resource Allocation Example
Tape Drives
Scanners
Tape Drives
Scanners
CD Drives
Plotters
CD Drives
Plotters
E ( 4 2 3 1 )
A ( 2 1 0 0 )
Resources in existence
Resources available to be allocated
C
Request matrix
Current allocation matrix
Causes deadlock!