Operating Systems Deadlocks
Operating Systems Deadlocks
DEADLOCKS
OPERATING SYSTEM
Deadlocks
What Is In This Chapter?
• What is a deadlock?
• How does a deadlock happen?
• Real world scenario vs OS scenario of Deadlock?
• 4 conditions for a deadlock to occur in OS?
• Dealock handling strategies?
Overview
• Resources
• Why do deadlocks occur?
• Dealing with deadlocks
• Ignoring them: ostrich algorithm
• Detecting & recovering from deadlock
• Avoiding deadlock
• Preventing deadlock
Resources
• Resource: something a process uses
• Usually limited (at least somewhat)
• Examples of computer resources
• Printers
• Semaphores / locks
• Tables (in a database)
• Processes need access to resources in reasonable
order
• Two types of resources:
• Preemptable resources: can be taken away from a process
with no ill effects
• Nonpreemptable resources: will cause the process to fail if
taken away
When do deadlocks happen?
Suppose
• Process 1 holds resource
A and requests resource B Process 1 Process 2
• Process 2 holds B and
requests A A
• Both can be blocked, with
neither able to proceed B
Deadlocks occur when …
• Processes are granted A
exclusive access to
devices or software B
constructs (resources)
• Each deadlocked process
needs a resource held by DEADLOCK!
another deadlocked
process
DEADLOCKS
BACKGROUND:
The cause of deadlocks: Each process needing what another process has. This
results from sharing resources such as memory, devices, links.
P1
R1 R2
P2
DEADLOCKS Bridge Crossing
Example
7: Deadlocks 7
DEADLOCKS DEADLOCK
CHARACTERISATION
NECESSARY CONDITIONS
ALL of these four must happen simultaneously for a deadlock to occur:
Mutual exclusion
One or more than one resource must be held by a process in a non-sharable
(exclusive) mode.
Hold and Wait
A process holds a resource while waiting for another resource.
No Preemption
There is only voluntary release of a resource - nobody else can make a process
give up a resource.
Circular Wait
Process A waits for Process B waits for Process C .... waits for Process A.
7: Deadlocks 8
DEADLOCKS DEADLOCK
CHARACTERISATION
NECESSARY CONDITIONS:
Dead lock can arise if following 4 conditions hold simultaneously ( Necessary
Conditions)
1. Mutual Exclusion 2. Hold & Wait:
One or more than one resource are non-sharable A process is holding at least one resource and waiting for
(only process can use at a time) other resources
P1 waiting for R3 R3 assigned to P2
P1 R3 P2
It want release
R1 assigned to P2 can't access R1 P2 holds R3
P1 P1 R1 P2 R1, R2 R1 & R2 till it
P1 holds R1 R1 can't be shared assigned gets R3
R1 to P1
R2
3. No Preemption 4. Circular wait
A resource can't be taken from a process unless A set of processes are waiting for each other in
the process releases the resource circular form P1 h
1 ol ds
t s R P1 R2
u e s
R1 assigned to P1 req
P1
P1 R1 R1 R2
P1 holds R1
2
OS can't Interrupt P2 h
olds e sts R
OS u
R1 P2 req
P2
DEADLOCKS
hods for handling deadlock:
e are three ways to handle deadlock:
7: Deadlocks 10
DEADLOCKS
3 strategies to handle deadlocks:
Preemption:
>> we can take a resource from one process and give it to other.
>> This will resolve the deadlock situation, but sometimes it does causes problems
Roll back:
>> In situation as where deadlock is a real possibility, the system can periodically
make a record of the state of each process and when deadlock occurs, roll
everything back to the last check point, and restart, but allocating resources
differently so that deadlock does not occur.
7: Deadlocks 11
DEADLOCKS RESOURCE
ALLOCATION GRAPH
A visual ( mathematical ) way to determine if a deadlock has, or may occur.
G = ( V, E ) The graph contains nodes and edges.
V Nodes consist of processes = { P1, P2, P3, ...} and resource types
{ R1, R2, ...}
E Edges are ( Pi, Rj ) or ( Ri, Pj )
An arrow from the process to resource indicates the process is requesting the
resource. An arrow from resource to process shows an instance of the resource
has been allocated to the process.
Process is a circle, resource type is square; dots represent number of instances of
resource in type. Request points to square, assignment comes from dot.
Pi Pi
Pi
7: Deadlocks Rj Rj 12
DEADLOCKS RESOURCE
ALLOCATION GRAPH
• If the graph contains no cycles, then no process is deadlocked.
• If there is a cycle, then:
a) If resource types have multiple instances, then deadlock MAY exist.
b) If each resource type has 1 instance, then deadlock has occurred.
R3 Assigned to P3
P2 Requests P3
7: Deadlocks 13
DEADLOCKS RESOURCE
ALLOCATION GRAPH
7: Deadlocks 14
DEADLOCKS Strategy
HOW TO HANDLE DEADLOCKS – GENERAL STRATEGIES
There are three methods:
Ignore Deadlocks: Most Operating systems do this!!
Mutual exclusion:
a) Automatically holds for printers and other non-sharables.
b) Shared entities (read only files) don't need mutual exclusion (and aren’t
susceptible to deadlock.)
c) Prevention not possible, since some devices are intrinsically non-sharable.
Hold and wait:
a) Collect all resources before execution.
b) A particular resource can only be requested when no others are being
held. A sequence of resources is always collected beginning with the
same one.
c) Utilization is low, starvation possible.
7: Deadlocks 16
DEADLOCKS Deadlock
Prevention
7: Deadlocks 18
DEADLOCKS Deadlock
Avoidance
NOTE: All deadlocks are unsafe, but all unsafes are NOT deadlocks.
UNSAFE
SAFE
DEADLOCK
7: Deadlocks 19
DEADLOCKS Deadlock
Avoidance
Let's assume a very simple model: each process declares its maximum
needs. In this case, algorithms exist that will ensure that no unsafe state is
reached.
There are multiple instances of
the resource in these examples.
EXAMPLE:
There exists a total of 12 tape drives. The current state looks like this:
Process Max Needs Allocated Current
Needs
P0 10 5 5
In this example, < p1, p0, p2 >
is a workable sequence. P1 4 2 2
7: Deadlocks 20
Deadlock
DEADLOCKS Avoidance
Safety Algorithm
A method used to determine if a particular state is safe. It's safe if there exists a
sequence of processes such that for all the processes, there’s a way to avoid
deadlock:
7: Deadlocks 22
DEADLOCKS Deadlock
Safety Algorithm Avoidance
Do these examples:
Consider a system with: five processes, P0 P4, three resource types, A, B, C.
Type A has 10 instances, B has 5 instances, C has 7 instances.
At time T0 the following snapshot of the system is taken.
Is the system Max Needs = allocated + can-be-requested
in a safe state?
Max Alloc Need Avail
A B C A B C A B C
7 5 3
P0 0 1 0 7 4 3 3 3 2
3 2 2 P1 2 0 0 0 2 0
9 0 2 P2 3 0 2 6 0 0
P3 2 1 1 0 1 1
2 2 2
P4 0 0 2 4 3 1
4 3 3
7: Deadlocks 23
DEADLOCKS Deadlock
Safety Algorithm Avoidance
Do these examples:
Consider a system with: five processes, P0 P4, three resource types, A, B, C.
Type A has 10 instances, B has 5 instances, C has 7 instances.
At time T0 the following snapshot of the system is taken.
Max Needs = allocated + can-be-requested
Is the system
in a safe state?
Alloc Req Avail
0 1 2 3 4
A B C A B C A B C
F F F F F
P0 0 1 0 7 4 3 3 3 2
P1 2 0 0 0 2 0
P0: P2 3 0 2 6 0 0
need<=work
7,4,3<= 2,3,0 P3 2 1 1 0 1 1
P4 0 0 2 4 3 1
7: Deadlocks 24
DEADLOCKS Deadlock
Safety Algorithm Avoidance
Do these examples:
Consider a system with: five processes, P0 P4, three resource types, A, B, C.
Type A has 10 instances, B has 5 instances, C has 7 instances.
At time T0 the following snapshot of the system is taken.
Is the system Max Needs = allocated + can-be-requested
in a safe state?
Max Alloc Need Avail
A B C A B C A B C
7 5 3
P0 0 1 0 7 4 3 3 3 2
3 2 2 P1 2 0 0 0 2 0
9 0 2 P2 3 0 2 6 0 0
P3 2 1 1 0 1 1
2 2 2
P4 0 0 2 4 3 1
4 3 3
7: Deadlocks 25
DEADLOCKS Deadlock
Resource req AlgorithmAvoidance
1. If req<=need go to step 2.
2. If req<= available go to step 3
3. Allocation = Allocation + request
Available= Available - request
need = Need - request
4. Initialize work = available
Initialize finish[i] = false, for i = 1,2,3,..n
5. Find an i such that:
finish[i] == false and need[i] <= work
If no such i exists, go to step 4.
6. work = work + allocation[i]
finish[i] = true
goto step 2
7. if finish[i] == true for all i, then the system is in a safe state.
7: Deadlocks 26
DEADLOCKS Deadlock
Safety Algorithm
Avoidance
Do these examples:
Now try it again with only a slight change in the request by P1.
P1 requests one additional resource of type A, and two more of type C.
Request1 = (1,0,2).
Is Request1 < available?
Alloc Req Avail
Produce the state
chart as if the A B C A B C A B C
request is Granted P0 0 1 0 7 4 3 1# 3 0#
and see if it’s safe.
(We’ve drawn the P1 3# 0 2# 0 2 0
chart as if it’s
granted. P2 3 0 2 6 0 0
P3 2 1 1 0 1 1
Can the request P4 0 0 2 4 3 1
be granted?
7: Deadlocks 27
DEADLOCKS Deadlock Detection
Need an algorithm that determines SINGLE INSTANCE OF A RESOURCE TYPE
if deadlock occurred.
• Wait-for graph == remove the resources
Also need a means of recovering from the usual graph and collapse edges.
from that deadlock. • An edge from p(j) to p(i) implies that p(j) is
waiting for p(i) to release.
7: Deadlocks 28
DEADLOCKS Deadlock Detection
7: Deadlocks 29
DEADLOCKS Deadlock Detection
7: Deadlocks 30
DEADLOCKS Deadlock Detection
EXAMPLE
We have three resources, A, B, and C. A has 7 instances, B has 2 instances, and C has 6
instances. At this time, the allocation, etc. looks like this:
7: Deadlocks 31
DEADLOCKS Deadlock Detection
EXAMPLE
Suppose the Request matrix is changed like this. In other words, the maximum amounts to be
allocated are initially declared so that this request matrix results.
Is there now a
sequence that will Alloc Req Avail
allow deadlock to be
avoided? A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
USAGE OF THIS P1 2 0 0 2 0 2
DETECTION ALGORITHM P2 3 0 3 0 0 1#
Frequency of check P3 2 1 1 1 0 0
depends on how often a
deadlock occurs and how P4 0 0 2 0 0 2
many processes will be
affected.
7: Deadlocks 32
DEADLOCKS Deadlock Recovery
So, the deadlock has occurred. Now, how do we get the resources back and gain forward
progress?
PROCESS TERMINATION:
· Could delete all the processes in the deadlock -- this is expensive.
· Delete one at a time until deadlock is broken ( time consuming ).
· Select who to terminate based on priority, time executed, time to completion, needs for
completion, or depth of rollback
· In general, it's easier to preempt the resource, than to terminate the process.
RESOURCE PREEMPTION:
· Select a victim - which process and which resource to preempt.
· Rollback to previously defined "safe" state.
· Prevent one process from always being the one preempted ( starvation ).
7: Deadlocks 33
DEADLOCKS Deadlock Recovery
• Cases include:
Preemption for memory,
Preallocation for swap space,
Avoidance for devices ( can extract Needs from process. )
7: Deadlocks 34
DEADLOCKS
WRAPUP
7: Deadlocks 35