You are on page 1of 20

Operating Systems (CS-205)

Lecture 19
Operating System – Deadlocks

Hafiz Tayyeb Javed


Lecturer
Department of Computer Science
Tayyeb.javed@nu.edu.pk
Outline
What’s deadlock?
Necessary conditions for deadlock
Deadlock prevention
Deadlock avoidance
Deadlock detection and recovery

2 CS-205 Operating System 2-May-2016


Example

3 CS-205 Operating System 2-May-2016


Three Deadlocked Processes

Deadlock: Two or more processes are each


waiting for each other to release a resource, and
thus none ever do
CS-205 Operating System
4 2-May-2016
Necessary Condition for Deadlock
P1 Mutual
R1 Exclusion
R1 Hold and Weight
Circular Weight
P1 Resource No preemption
Allocation Graph P1

R1

P1 R1

5 CS-205 Operating System 2-May-2016


Does Deadlock Exists?
Mutual exclusion?
Hold and wait?
Circular waiting?
No preemption?

6 CS-205 Operating System 2-May-2016


Methods of Handling Deadlocks
Create protocol to prevent or avoid deadlocks
Never enter a deadlocked state
Allow system to enter deadlocked state
Detect it and recover
Ignore the problem
Pretend it won’t happen

7 CS-205 Operating System 2-May-2016


Addressing Deadlock
Prevention: Design the system so that deadlock is
impossible
Avoidance: Construct a model of system states,
then choose a strategy that will not allow the
system to go to a deadlock state
Detection & Recovery: Check for deadlock
(periodically or sporadically), then recover
Manual intervention: Have the operator reboot
the machine if it seems too slow

8 CS-205 Operating System 2-May-2016


Prevention
Necessary conditions for deadlock
Mutual exclusion
Hold and wait
Circular waiting
No preemption
Ensure that at least one of the necessary conditions
is false at all times
Mutual exclusion? must hold at all times

9 CS-205 Operating System 2-May-2016


Hold and Wait
Need to be sure a process does not hold one
resource while requesting another
Approach 1: Force a process to request all
resources it needs at one time
Approach 2: If a process needs to acquire a new
resource, then …?
it must first release all resources it holds, then reacquire
all it needs Process
1

Resource 1 Resource 2
10 CS-205 Operating System 2-May-2016
Circular Wait
There is a cycle in the graph of processes and
resources
Choose a resource request strategy by which no
cycle will be introduced
Total order on all resource types, then can only ask
for Rj if Ri < Rj for all Ri the process is currently
holding
Requires proper use of resource requests

11 CS-205 Operating System 2-May-2016


Allow Preemption
Allow preemption to prevent a blocked request

 Preempt P1’s
resources
 Preempt P2’s
resource

12 CS-205 Operating System 2-May-2016


Avoidance
Define a model of system states, then choose a
strategy that will guarantee that the system will not
go to a deadlock state
Requires extra information, e.g., the maximum
claim for each process
Allows resource manager to see the worst case that
could happen, then to allow transitions based on
that knowledge

13 CS-205 Operating System 2-May-2016


Safe States
Safe state: there exists a safe sequence
Even if all exercise their maximum claim, there is an
allocation strategy by which all claims can be met
Might have some processes wait for other processes to
complete and release resources
Safe sequence for <P1, P2, …, Pn> for current
allocation state if, for each Pi, the resource
requests Pi can still make can be satisfied by
currently available resources plus those held by all
Pj, where j < i

14 CS-205 Operating System 2-May-2016


Unsafe States
Unsafe state: there does not exist a safe sequence
Unsafe state may lead to a deadlock state if too many
processes exercise their maximum claim at once
Behavior of processes controls unsafe states

15 CS-205 Operating System 2-May-2016


Avoidance Algorithms
Resource-allocation-graph algorithm
Only valid for single instance resource types
In your book.
Banker’s algorithm
Less efficient than resource-allocation-graph
Works for multiple instance resources

16 CS-205 Operating System 2-May-2016


Banker’s Algorithm
New process enters system
Declares the maximum number of instances of each
resource type it may need
Can’t exceed total in system
When resources are requested
System determines if allocation leaves system in safe
state
If yes, then allocate resources else wait until other
processes release resources

17 CS-205 Operating System 2-May-2016


Banker’s Algorithm
Let max[i, j] be the maximum claim for Rj by Pi
Let alloc[i, j] be the number of units of Rj held by
Pi
Let avail[j] be the number of units of Rj available,
C[j] = total available to start
Let need[i, j] be the number of units of Rj that Pi
may still need
Can be computed as: max[i, j] - alloc[i, j]

18 CS-205 Operating System 2-May-2016


Example
Allocation Max Available Need
ABC AB AB C ABC
C P0 7 4 3
P0 010 753 332
P1 1 2 2
P1 200 322
P2 6 0 0
P2 302 902
P3 0 1 1
P3 211 222
P4 4 3 1
P4 002 433
Claim: The system is in a safe state
Sequence <P1, P3, P4, P2, P0> satisfies the safety criteria
P1 requests (1, 0, 2)
Again safe <P1, P3, P4, P0, P2>

19 CS-205 Operating System 2-May-2016


Quiz
Allocation Max Available Need
ABC AB AB C ABC
C P0 7 4 3
P0 010 753 230
P1 0 2 0
P1 302 322
P2 6 0 0
P2 302 902
P3 0 1 1
P3 211 222
P4 4 3 1
P4 002 433
Now P4 requests (3, 3, 0) Will this be granted or not and Why?
Now P0 requests (0, 2, 0) Will this be granted or not and Why?

20 CS-205 Operating System 2-May-2016

You might also like