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

Deadlock Part1

The document discusses deadlock in operating systems, defining it as a situation where processes are permanently blocked due to competing resource needs. It outlines the conditions for deadlock, methods for handling it such as prevention, avoidance, and detection, and introduces concepts like Resource Allocation Graphs to illustrate deadlock scenarios. Additionally, it explains deadlock prevention techniques and avoidance algorithms, including Dijkstra’s banker’s algorithm.

Uploaded by

minm54997
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)
3 views

Deadlock Part1

The document discusses deadlock in operating systems, defining it as a situation where processes are permanently blocked due to competing resource needs. It outlines the conditions for deadlock, methods for handling it such as prevention, avoidance, and detection, and introduces concepts like Resource Allocation Graphs to illustrate deadlock scenarios. Additionally, it explains deadlock prevention techniques and avoidance algorithms, including Dijkstra’s banker’s algorithm.

Uploaded by

minm54997
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/ 23

Operating Systems: CSE 3204

Deadlock

April 30, 2025 1

Operating Systems
Deadlock
• System Model
• Deadlock Characterization
• Methods for handling Deadlock
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection
• Recovery from Deadlock

April 30, 2025 2

Operating Systems
System Model
• A system contains a finite number of resource types (R1, R2, . . .,
Rm) to be distributed among competing processes
• The resource types are partitioned in to several types (e.g files,
I/O devices, CPU cycles, memory), each having a number of
identical instances
• A process must request a resource before using it and release it
after making use of it. Each process utilizes a resource as follows:
• Request
• A process requests for an instance of a resource type. If
the resource is free, the request will be granted. Otherwise
the process should wait until it acquires the resource
• Use
• The process uses the resource for its operations
• Release
• The process releases the resource
April 30, 2025 3

Operating Systems
Deadlock
• Deadlock can be defined as a permanent blocking of
processes that either compete for system resources or
communicate with each other
• The set of blocked processes each hold a resource and wait
to acquire a resource held by another process in the set
• All deadlocks involve conflicting needs for resources by two
or more processes
• A set of processes or threads are deadlocked when each
process or thread is waiting for a resource to be freed which
is controlled by another process

April 30, 2025 4

Operating Systems
Traffic gridlock is an everyday
example of a deadlock situation.
When two trains approach each other at a crossing, both
shall come to a full stop and neither shall start up again
until the other has gone

April 30, 2025 5

Operating Systems
Deadlock characterization
Deadlock can arise if four conditions hold
simultaneously in a system:
1. Mutual exclusion: only one process at a time can use a
resource. No process can access a resource unit that has been
allocated to another process
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: there exists a set {P0, P1, …, Pn} 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, …,
• Pn–1 is waiting for a resource that is held by Pn, and
• 2025
April 30, P n is waiting for a resource that is held by P0 6

Operating Systems
Resource Allocation Graph(RAG)
• Deadlock can be better described by using a directed
graph called resource allocation graph
• The graph consists of a set of vertices V and a set of edges
E
V is partitioned into two types:
• P = {P1, P2, …, Pn}, the set consisting of all the processes
in the system.
• R = {R1, R2, …, Rm}, the set consisting of all resource
types in the system.
• Request edge: directed edge P1  Rj
• Assignment edge: directed edge Rj  Pi
• If a Resource Allocation Graph contains a cycle, then a
deadlock may exist
April 30, 2025 7

Operating Systems
Resource Allocation Graph /RAG
• (cont.)
Diagrammatically, processes and resources in RAG are
represented as follow:

• Process p

• Resource Type with 4 instances

• Pi requests instance of Rj Pi Rj

• Pi is holding an instance of RP
j i
Rj

April 30, 2025 8

Operating Systems
Example of Resource Allocation
Graph

April 30, 2025 9

Operating Systems
Graph With A Cycle But No
Deadlock
Basic Facts
• If graph contains no cycles then
there’s no deadlock

• If however a graph contains a cycle


then there are two possible
situations:
• if there is only one instance per
resource type, then deadlock can
occur

• if there are several instances per


resource type, there’s a possibility
of no deadlock
April 30, 2025 10

Operating Systems
Example of Resource Allocation
Graph • The RAG shown here tells us about the
following situation in a system:
• P= {P1, P2, P3}
• R= {R1,R2, R3, R4}
• E ={P1R1,P2R3, R1P2, R2P1,R3P3}

• The process states


• P1 is holding an instance of R2 and is
waiting for an instance of R1

• P2 is holding an instance of R1 and


instance of R2, and is waiting for an
instance of R3

• P3 is holding an instance of R3
April 30, 2025 11

Operating Systems
Example of Resource Allocation
Graph
• Resource Allocation Graph With a Deadlock

• There are two cycles in this graph


• P1R1P2R3P3R2P1
• P2R3P3R2P2

• Processes P1, P2 and P3 are


deadlocked:

• P1 is waiting for P2 to release R1

• P2 is waiting for R3 held by P3 and

• P3 is waiting for either P1 or P2 to


release R2
April 30, 2025 12

Operating Systems
Methods for handling
Deadlocks
Deadlock problems can be handled in one of the following 3
ways:

1. Using a protocol that prevents or avoids deadlock by


ensuring that a system will never enter a deadlock
state deadlock prevention and deadlock avoidance
scheme are used

2. Allow the system to enter a deadlock state and then


recover

3. Ignore the problem and pretend that deadlocks never


occur in the system; used by most operating systems,
April 30, 2025 13
including UNIX
Operating Systems
Deadlock
Prevention
• By ensuring at least one of the necessary conditions for deadlock
will not hold, deadlock can be prevented.
 This is mainly done by restraining how requests for
resources can be made

• Deadlock prevention methods fall into two classes:


• An indirect method of deadlock prevention prevents the
occurrence of one of the three necessary conditions listed
previously (items 1 through 3)
• A direct method of deadlock prevention prevents the
occurrence of a circular wait (item 4)

April 30, 2025 14

Operating Systems
Deadlock Prevention (contd.)
1. Mutual Exclusion: This is not required for sharable resources;
however to prevent a system from deadlock, the mutual exclusion
condition must hold for non-sharable resources

2. Hold and Wait: in order to prevent the occurrence of this condition in


a system, we 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:
o Since resources may be allocated but not used for a long period,
resource utilization will be low
o A process that needs several popular resources has to wait
indefinitely because one of the resources it needs is allocated to
another
April 30, 2025 process. Hence starvation is possible. 15

Operating Systems
Deadlock Prevention (contd.)
3. No Preemption
• If a process holding certain resources is denied further request,
that process must release its original resources allocated to it
• If a process requests a resource allocated to another process
waiting for some additional resources, and the requested
resource is not being used, then the resource will be
preempted from the waiting process and allocated to
the requesting process
• 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
• This approach is practical to resources whose state can easily
saved and retrieved easily
April 30, 2025 16

Operating Systems
Deadlock Prevention
(contd.)
4. Circular Wait
• A linear ordering of all resource types is defined and each
process requests resources in an increasing order of
enumeration

• So, if a process initially is allocated instances of resource


type R, then it can subsequently request instances of
resources types following R in the ordering
Ri->Ri+1->Ri+2->Ri+3

April 30, 2025 17

Operating Systems
Deadlock Avoidance
• Deadlock avoidance scheme requires each process to declare
the maximum number of resources of each type that it
may need in advance

• Having this full information about the sequence of requests


and release of resources, we can know whether or not the
system is entering unsafe state

• 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.
•AprilA30,deadlock
2025
state is an unsafe state. 18

Operating Systems
Deadlock avoidance: Safe State
Basic Facts
• If a system is in a safe
state, then there are no
deadlocks.

• If a system is in unsafe
state, then there is a
possibility of deadlock.

• Deadlock avoidance
method ensures that a
system will never enter an
unsafe
April 30, 2025
state 19

Operating Systems
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(RAG)

• If there are multiple instances of a resource type,


• Use the Dijkstra’s banker’s algorithm

April 30, 2025 20

Operating Systems
Deadlock Avoidance Algorithms (contd.)
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 safe state
otherwise the system is in unsafe state
• Resources must be claimed a priori in the system
• i.e, before a process starts executing, all its claim edge must show up
in the allocation graph
April 30, 2025 21

Operating Systems
Deadlock Avoidance Algorithms
(contd.)
Resource-Allocation
• Suppose Graph
that process Pi requests Algorithm
a resource Rj

• The request can be granted only if converting the request


edge to an assignment edge does not result in the
formation of a cycle in the resource allocation graph.

• If we suppose P2 requests R2. We


can not allocate it since it will create
a cycle.

April 30, 2025 22

Operating Systems
Deadlock Avoidance Algorithms
(contd.)
Resource-Allocation Graph algorithm

• If P1 requests for R2 and P2


requests for R1, then deadlock
will occur

Unsafe State Resource-Allocation


April 30, 2025 23

Operating Systems

You might also like