Chapter 2 Deadlock
Chapter 2 Deadlock
4 Deadlock
05/15/2025 1
Contents
Introduction
Resource
Deadlock characterization
Resource allocation graph
Deadlock handling mechanism
Deadlock prevention
Deadlock detection and recovery
Deadlock avoidance
Two phase locking
Communication deadlock
Live lock
starvation
05/15/2025 2
What is Deadlock in Operating System (OS)?
05/15/2025 3
System Model
• Basically in the Normal mode of Operation utilization
of resources by a process is in the following
sequence:
- Request
• Firstly, the process requests the resource. In a
case, if the request cannot be granted
immediately(e.g: resource is being used by any
other process), then the requesting process
must wait until it can acquire the resource.
– Use
• The Process can operate on the resource ( e.g: if
the resource is a printer then in that case
process can print on the printer).
– Release
• The process releases the resource
05/15/2025 4
05/15/2025 5
Necessary conditions for Deadlocks
• Process p
Pi Rj
• Pi requests instance of Rj
Pi
• Pi is holding an instance of Rj Rj
05/15/2025 8
Example of Resource Allocation Graph
05/15/2025 9
If a Resource Allocation Graph contains a cycle,
then a deadlock may or may not exist.
Case 1: The system has one instance per resource
type
• If a cycle exists, the system in a deadlock state.
• Each process involved in the cycle is deadlocked.
P1
Cycle:
R2 R1 P1 R1 P2 R2
Cycle
(Deadlock
)
As show, no chance to break the cycle
P2 because:
• No process can finish
execution.
05/15/2025 10
• No way to free a resource.
Case 2: The system has more than one instance per
resource type
• If a cycle exists, the system may or may not be
in a deadlock state.
• Ex 1: a cycle with deadlock:
Two Cycles exists:
P1 R1 P2 R3 P3 R2
P2 R3 P3 R2
05/15/2025 11
Ex 2: a cycle without deadlock:
Cycle:
P1 R1 P3 R2
13 05/15/2025
Deadlock prevention
1.Mutual Exclusion – not required for
sharable resources; must hold for non
sharable resources
2.Hold and Wait – must guarantee that
whenever a process requests a resource, it
does not hold any other resources. Two
protocols are used to implement this
1. Require a process to request and be allocated all its
resources before it begins execution or
2. Allow a process to request resources only when the
process has none
• Both protocols have two main disadvantages:
1. Since resources may be allocated but not used for a
long period,
14 resource utilization will be 05/15/2025
low
…Deadlock
3. No Preemption –
prevention
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
15 05/15/2025
Deadlock Avoidance
Requires that the system has some additional a
prior information
Simplest and most useful model requires that each
process declare the maximum number of resources of
each type that it may need
05/15/2025 18
…Recovery from Deadlock
o While implementing this strategy, there are three
issues to be considered
Selecting a victim – which resources and
process should be selected to minimize cost just
like in process termination. The cost factors may
include parameters like the number of resources a
deadlocked process is holding, number of
resources it used so far
Rollback (Total)– if a resource is preempted from
a process, then it can not continue its normal
execution
• The process must be rolled back to some safe
state and started
Starvation – same process may always be picked
as victim several times. As a result, starvation may
05/15/2025 19
Deadlock Avoidance
Algorithms
• Based on the concept of safe state, we can define
algorithms that ensures the system will never
deadlock.
05/15/2025 20
…Deadlock Avoidance Algorithms
Assignment Request
Edge Edge
Claim
Edge
Suppose that
P2 requests R2
Cycle
05/15/2025 23
…Deadlock Avoidance Algorithms
Banker’s Algorithm
Need
05/15/2025 [i,j] = Max[i,j] – Allocation [i,j] 25
…Deadlock Avoidance Algorithms
Banker’s Algorithm
Safety
Algorithm
• It is used to identify whether or not a system is in a safe
state. The algorithm can be described as follow:
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.
2. Find an i such that both:
(a) Finish [i] = false
(b) Needi Work; If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish [i] = true; go to step 2.
4. If Finish [i] == true for all i, then the system is in a safe
state.
05/15/2025 26
…Deadlock Avoidance Algorithms
Banker’s Algorithm
consider the following example of a system, check whether the
system is in safe or not using banker’s algorithm. Determine the
sequence of if it is safe.
Remaining Need
Allocation Max Need. ABc
Available P0 7 4 3
ABC ABC AB P1 1 2 2
C
P2 6 0 0
P0 0 1 0 753 332
P3 0 1 1
P1 2 0 0 322
P4 4 3 1
P2 3 0 2 902
P3 2 1 1 222
•P4The
0 0system
2 is 4in3a3safe state since the sequence < P1, P3, P4,
P2, P0> satisfies safety criteria
05/15/2025 27
Two-Phase Locking
05/15/2025 28
Communication deadlock
o Process wants something that another process has and must
wait until the first one gives it up.
o Another kind of deadlock can occur in communication systems
(e.g., networks), in which two or more processes communicate
by sending messages.
o A common arrangement is that process A sends a request
message to process B, and then blocks until B sends back a
reply message.
o Communication deadlocks cannot be prevented by ordering
the resources (since there are none) or avoided by careful
scheduling (since there are no moments when a request could
be postponed).
o The technique that can usually be employed to break
communication deadlocks: timeouts.
o In most network communication systems, whenever a message
is sent to which a reply is expected a timer is also started.
o If the timer goes off before the reply arrives, the sender of the
message assumes that the message has been lost and sends it
again (and again and again if needed).
05/15/2025 29
Livelock
05/15/2025 30
Starvation
05/15/2025 31
Assignment II
1. For each of the resource allocation graphs below,
determine whether there is a deadlock or not. Give
explanations for each of your answers.
b.
a.
c. d.
05/15/2025 32
Cont.,…
2. There are four processes in a system and they are going to
share nine tape drives. Their current and maximum number of
allocation numbers are as follows :
a. Is the system in a safe
state? Why or why not?
b. Is the system deadlocked?
Why or why not?
05/15/2025 33
Cont.’…
4. Given the resource allocation graph below:
a. Apply the deadlock detection
algorithm and either indicate why
the system is deadlocked, or specify
a safe allocation sequence