0% found this document useful (0 votes)
0 views

Chapter 2 Deadlock

The document discusses deadlock in operating systems, defining it as a situation where blocked processes hold resources while waiting for others. It covers necessary conditions for deadlocks, methods for handling them (prevention, avoidance, detection, and recovery), and introduces concepts like resource allocation graphs and the Banker's algorithm. Additionally, it touches on related concepts such as livelock and communication deadlock.

Uploaded by

ruhashime2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Chapter 2 Deadlock

The document discusses deadlock in operating systems, defining it as a situation where blocked processes hold resources while waiting for others. It covers necessary conditions for deadlocks, methods for handling them (prevention, avoidance, detection, and recovery), and introduces concepts like resource allocation graphs and the Banker's algorithm. Additionally, it touches on related concepts such as livelock and communication deadlock.

Uploaded by

ruhashime2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

2.

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)?

• Deadlocks are a set of blocked


processes each holding a resource
and waiting to acquire a resource
held by another process.

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

1. Mutual Exclusion: only one


process at a time can use a
resource
2. Hold and Wait: a process
holding at least one resource is
waiting to acquire additional
resources held by other processes
3. No preemption: a resource can
be released only voluntarily by the
process holding it, after that process
has completed its task
4. Circular Wait: in this
condition, the set of processes
are waiting for each other in the
05/15/2025 6
• In order to solve the deadlock problem, we
must Resource Allocation
find a method to express it.
• This Graph
can be achieved using resource
allocation graph.
• The graphs have two kinds of nodes and
directed arc.
• Processes, shown as circles
• Resources, shown as squares.
• A directed arc from a resource node to a
process node means that the resource has
previously been requested by, granted to,
and is currently held by that process.
• A directed arc from a process to a
resource means that the process is
currently blocked waiting for that
05/15/2025 7
…Resource Allocation Graph
• Diagrammatically, processes and resources in
RAG are represented as follow:

• Process p

• Resource Type with 4 instances

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

As show, P1, P2, and P3 are deadlocked

05/15/2025 11
Ex 2: a cycle without deadlock:

Cycle:
P1  R1  P3  R2

There is no deadlock because:


• P4 may release its instance of
R2.
• This resource can be then
allocated to P3 which breaking
the cycle.

• Also P2 may release its instance


of R1.
• This resource can be allocated to
05/15/2025 12
P1.
Deadlock handling
mechanism
Deadlock problems can be handled in one of the following 4
ways:
1. Using a protocol avoids deadlock by ensuring that a
system will never enter a deadlock state (deadlock
avoidance)
2. Structurally negating one of the four required
conditions (deadlock prevention).
3. Allow the system to enter a deadlock state and then
recover (deadlock detection and recovery)
4. Ignore the problem and pretend that deadlocks never
occur in the system(ostrich algorithm); used by most
operating systems, including UNIX

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

4. Circular Wait – impose a total ordering of all


resource types, and require that each process
requests resources in an increasing order of
enumeration

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

 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
A state is safe if the system can allocate resources to
each process in some order avoiding a deadlock.
 A deadlock state is an unsafe state.
05/15/2025 16
Recovery from Deadlock
o Once a deadlock has been detected, recovery strategy is
needed. There are two possible recovery approaches:
Killing process
Resource preemption
– Killing the process (process termination):
• Killing all the process involved in the deadlock.
• Abort one process at a time. After killing each
process check for deadlock again keep repeating
the process till system recover from deadlock.
– Cost factors:-
• Priority process
• How long the process has computed? 1%, 50% , 90%...?
• How much longer a process will compute before completion?
• How many and what type of resources process has used?
• How many the process needs to complete the execution ?
05/15/2025 17
Recovery from Deadlock
• Resource Pre-emption: Resources are
preempted from the processes involved in the
deadlock, preempted resources are allocated to
other processes so that there is a possibility of
recovering the system from deadlock.

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.

• If there is a single instance of a resource type,


Use a resource-allocation graph
• If there are multiple instances of a resource
type,
Use the banker’s algorithm

05/15/2025 20
…Deadlock Avoidance Algorithms

Resource-Allocation Graph Scheme


• A new type of edge (Claim edge), in addition to the
request and assignment edge is introduced.
• Claim edge Pi  Rj indicates that process Pi may
request resource Rj at some point in the future. The
edge resembles a request edge but is represented by a
dashed line in the graph
– Claim edge is converted to request edge when a
process requests a resource
– Request edge is 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
– If no cycle exists in the allocation, then system is in
05/15/2025 21
safe state otherwise the system is in unsafe state
…Deadlock Avoidance Algorithms

Assignment Request
Edge Edge

Claim
Edge

Suppose that process Pi requests a resource Rj


The request can be granted only if converting
the request edge to an assignment edge does
not result in a cycle in the resource allocation
graph
05/15/2025 22
…Deadlock Avoidance Algorithms
Example: consider the following resource
allocation graph:

Suppose that
P2 requests R2

Although R2 is free, we can not allocate it to P2 since this


creates a cycle

Cycle

05/15/2025 23
…Deadlock Avoidance Algorithms
Banker’s Algorithm

• This algorithm is used when there are multiple


instances of resources

• When a process enters a system, it must declare the


maximum number of each instance of resource
types it may need
– The number however may not exceed the total
number of
resource types in the system
• When a process requests a resource it may have to
wait
• When a process gets all its resources it must return
them in a finite amount of time
05/15/2025 24
…Deadlock Avoidance
Algorithms
Banker’s Algorithm
• The following data structures are used in the
algorithm:
Let n = number of processes, and
m = number of resources types.
• Available[m]: 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.
– If Allocation[i,j] = k then Pi is currently allocated k
instances of Rj.
• Need: n x m matrix.
– If Need[i,j] = k, then Pi may need k more instances of Rj to
complete its task

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

o Deadlock avoidance and prevention are not


terribly promising in the general case for
deadlock handling.
o When multiple processes are running at the
same time, there is a real danger of deadlock.
o The approach often used is called two-phase
locking.
o In the first phase, the process tries to lock all
the records it needs, one at a time.
o If it succeeds, it begins the second phase,
performing its updates and releasing the
locks.

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

o In some situations, polling (busy waiting) is used to


enter a critical region or access a resource.
o Let pair of processes (process A and process
B)using two resources.
o Process A is use resource 1 and request resource 2
and process 2 use resource 2 and request
o If the processes wait for required resource by
spooling rather than blocking, this situation is
called livelock.
o Thus we do not have a deadlock (because no
process is blocked) but we have something
functionally equivalent to deadlock; will make no
further progress.

05/15/2025 30
Starvation

o A problem closely related to deadlock and livelock is


starvation.
o When processes wait for execution, but it cannot be
possible to get CPU to be run, because of scheduler
or something else these processes are said to be
under 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?

3. Explain if the system in the


given resource allocation
graph here is deadlocked. If
not, give an execution order
of the processes which
successfully terminates

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

b. If the process P2 also request 2


instances of resource r1, does the
system enter a deadlock? Why?

5. Consider the following snapshot of a system:

• Use the banker’s algorithm to


answer the following questions:
a. What is the content of the
matrix Need?
b. Is the system in a safe state?
c. If a request from process P1
arrives for (0,4,2,0), can the
request be granted
05/15/2025 immediately? 34

You might also like