Deadlock - 20240214
Deadlock - 20240214
02/25/2024 1
Contents
• a. Introduction
• b. Conditions of deadlock
• Let us assume that there are three processes P1, P2 and P3. There are three different
resources R1, R2 and R3. R1 is assigned to P1, R2 is assigned to P2 and R3 is assigned to
P3.
• After some time, P1 demands for R1 which is being used by P2. P1 halts its execution
since it can't complete without R2. P2 also demands for R3 which is being used by P3.
P2 also stops its execution because it can't continue without R3. P3 also demands for R1
which is being used by P1 therefore P3 also stops its execution.
02/25/2024 3
02/25/2024 4
02/25/2024 5
R1 is assigned to P1
R2 is assigned to P2
R3 is assigned to P3
P1 DEMANDS R2,
P2 DEMAND R3
AND P3 DEMAND R1 WHICH IS BEING USED BY P1
In this scenario, a cycle is being formed among the three processes. None of the process is progressing and they are all
waiting. The computer becomes unresponsive since all the processes got blocked.
02/25/2024 6
Deadlock is a situation where two or more processes are waiting for
each other. For example, let us assume, we have two processes P1 and
P2.
Now, process P1 is holding the resource R1 and is waiting for the
resource R2.
At the same time, the process P2 is having the resource R2 and is
waiting for the resource R1.
So, the process P1 is waiting for process P2 to release its resource and
at the same time, the process P2 is waiting for process P1 to release its
resource. And no one is releasing any resource. So, both are waiting for
each other to release the resource.
This leads to infinite waiting and no work is done here. This is called
Deadlock.
02/25/2024 7
02/25/2024 8
In operating systems, a deadlock occurs when two or more processes
are unable to proceed because each is waiting for the other to release a
resource. This situation typically arises in a system where resources are
allocated to processes in a way that allows them to be used exclusively.
A process in operating system uses resources in the following way.
a) Requests a resource
b) Use the resource
c) Releases the resource
A deadlock is a situation where a set of processes are blocked because
each process is holding a resource and waiting for another resource
acquired by some other process.
02/25/2024 9
Deadlock Starvation
A deadlock is a situation where a set of Starvation is a situation where the low
processes are blocked because each priority process got blocked and the high
process is holding a resource and waiting priority processes proceed.
for another resource acquired by some
other process.
Deadlock is an infinite waiting. Starvation is a long waiting but not
infinite.
Every Deadlock is always a starvation. Every starvation need not be deadlock.
02/25/2024 10
Necessary Conditions for Deadlock
There are four different conditions that result in Deadlock. These four
conditions are also known as Coffman conditions and these conditions
are not mutually exclusive. Let's look at them one by one.
1. Mutual Exclusion.
2. Hold and Wait
3. No Preemption
4. Circular Wait
02/25/2024 11
Mutual Exclusion
• Mutual Exclusion: A resource can be held by only one process at a
time.
• In other words, if a process P1 is using some resource R at a particular
instant of time, then some other process P2 can't hold or use the
same resource R at that particular instant of time.
• The process P2 can make a request for that resource R but it can't use
that resource simultaneously with process P1.
02/25/2024 12
02/25/2024 13
Hold and Wait
• A process can hold a number of resources at a time and at the same
time, it can request for other resources that are being held by some
other process.
• For example, a process P1 can hold two resources R1 and R2 and at
the same time, it can request some resource R3 that is currently held
by process P2.
02/25/2024 14
02/25/2024 15
No preemption:
• A resource can't be preempted from the process by another process,
forcefully. For example, if a process P1 is using some resource R, then
some other process P2 can't forcefully take that resource.
• If it is so, then what's the need for various scheduling algorithm. The
process P2 can request for the resource R and can wait for that
resource to be freed by the process P1.
02/25/2024 16
Circular Wait:
Circular wait is a condition when the first process is waiting for the
resource held by the second process, the second process is waiting for
the resource held by the third process, and so on.
At last, the last process is waiting for the resource held by the first
process. So, every process is waiting for each other to release the
resource and no one is releasing their own resource.
Everyone is waiting here for getting the resource. This is called a
circular wait.
02/25/2024 17
02/25/2024 18
Summary
Mutual Exclusion: At least one resource must be held in a non-sharable
mode. This means only one process can use the resource at a time. EX.
ONE PROCESS CAN USE PRINTER AT A TIME. NO INTERLEAVING SCHEME.
Hold and Wait: A process must be holding at least one resource and
waiting to acquire additional resources that are currently held by other
processes.
No Preemption: Resources cannot be forcibly taken away from a process;
they must be released voluntarily by the process holding them.
Circular Wait: A set of waiting processes must exist such that each process
is waiting for a resource held by the next process in the set, creating a
circular chain of dependencies.
02/25/2024 19
Various Methods to handle Deadlock[IPAD]
• Deadlock Prevention
02/25/2024 20
DEADLOCK IGNORANCE
• It is also known as the OSTRICH METHOD.
• There is no particular strategy to handle the deadlock.
• No mechanism to handle the deadlock.
• We ignore the problem all together and pretend that deadlock never
occur in the system.
• Most of the desktop computer uses the deadlock ignorance including
UNIX.
• Solution: Simply Restart the system.
02/25/2024 21
DEADLOCK IGNORANCE
• Deadlock Ignorance is the most widely used approach among all the
mechanism. This is being used by many operating systems mainly for end
user uses.
• In this approach, the Operating system assumes that deadlock never
occurs. It simply ignores deadlock. This approach is best suitable for a
single end user system where User uses the system only for browsing
and all other normal stuff.
• In these types of systems, the user has to simply restart the computer
in the case of deadlock.
• Windows and Linux are mainly using this approach.
02/25/2024 23
Deadlock Prevention
02/25/2024 25
1. Mutual Exclusion
• Some resources are inherently un shareable, for example, Printers. For un
shareable resources, processes require exclusive control of the resources.
• A mutual exclusion means that un shareable resources cannot be accessed
simultaneously by processes.
• Shared resources do not cause deadlock but some resources can't be shared
among processes, leading to a deadlock.
• For Example: read operation on a file can be done simultaneously by multiple
processes, but write operation cannot. Write operation requires sequential
access, so, some processes have to wait while another process is doing a
write operation.
02/25/2024 26
• It is not possible to eliminate mutual exclusion, as some resources are
inherently non-shareable,
• For Example Tape drive, as only one process can access data from a
Tape drive at a time.
• For other resources like printers, we can use a technique called
Spooling.
• Spooling: It stands for Simultaneous Peripheral Operations online.
• A Printer has associated memory which can be used as a spooler
directory (memory that is used to store files that are to be printed
next).
02/25/2024 27
Hold and Wait
• Hold and wait is a condition in which a process is holding one
resource while simultaneously waiting for another resource that is
being held by another process. The process cannot continue till it gets
all the required resources.
• Resource 1 is allocated to Process 2
• Resource 2 is allocated to Process 1
• Resource 3 is allocated to Process 1
• Process 1 is waiting for Resource 1 and holding Resource 2 and
Resource 3
• Process 2 is waiting for Resource 2 and holding Resource 1
02/25/2024 28
There are two ways to eliminate hold and wait:-
• By eliminating wait:
• The process specifies the resources it requires in advance so that it does not
have to wait for allocation after execution starts.
• For Example: Process1 declares in advance that it requires both Resource1
and Resource2
• By eliminating hold:
• The process has to release all resources it is currently holding before making a
new request.
• For Example: Process1 has to release Resource2 and Resource3 before making
request for Resource1
02/25/2024 29
02/25/2024 30
No preemption
• Preemption is temporarily interrupting an executing task and later
resuming it.
• For example, if process P1 is using a resource and a high-priority
process P2 requests for the resource, process P1 is stopped and the
resources are allocated to P2.
• There are two ways to eliminate this condition by preemption:
1. If a process is holding some resources and waiting for other
resources, then it should release all previously held resources and put a
new request for the required resources again. The process can resume
once it has all the required resources.
02/25/2024 31
• For example: If a process has resources R1, R2, and R3 and it is
waiting for resource R4, then it has to release R1, R2, and R3 and put
a new request of all resources again.
02/25/2024 32
Challenges:
• These approaches are problematic as the process might be actively
using these resources and halting the process by preempting can cause
inconsistency.
• For example: If a process is writing to a file and its access is revoked
for the process before it completely updates the file, the file will
remain unusable and in an inconsistent state.
• Putting requests for all resources again is inefficient and time-
consuming.
02/25/2024 33
Circular Wait
02/25/2024 34
• To eliminate circular wait, we assign a priority to each resource. A
process can only request resources in increasing order of priority.
• In the example above, process P3 is requesting resource R1, which
has a number lower than resource R3 which is already allocated to
process P3. So this request is invalid and cannot be made, as R1 is
already allocated to process P1.
Challenges:
• It is difficult to assign a relative priority to resources, as one resource
can be prioritized differently by different processes.
• For Example: A media player will give a lesser priority to a printer
while a document processor might give it a higher priority. The
priority of resources is different according to the situation and use
case.
02/25/2024 35
Feasibility of Deadlock Prevention
02/25/2024 36
02/25/2024 37
Deadlock Avoidance
• A deadlock avoidance policy grants a resource request only if it can establish
that granting the request cannot lead to a deadlock either immediately or in the
future.
• The kernal lacks detailed knowledge about future behavior of processes, so it
cannot accurately predict deadlocks. To facilitate deadlock avoidance under
these conditions, it uses the following conservative approach: Each process
declares the maximum number of resource units of each class that it may
require.
• The kernal permits a process to request these resource units in stages- i.e. a few
resource units at a time- subject to the maximum number declared by it and
uses a worst case analysis technique to check for the possibility of future
deadlocks.
• A request is granted only if there is no possibility of deadlocks; otherwise, it
remains pending until it can be granted. This approach is conservative because a
process may complete its operation without requiring the maximum number of
units declared by it.
02/25/2024 38
Resource Allocation Graph
• The resource allocation graph (RAG) is used to visualize the system’s
current state as a graph. The Graph includes all processes, the
resources that are assigned to them, as well as the resources that
each Process requests.
• Sometimes, if there are fewer processes, we can quickly spot a
deadlock in the system by looking at the graph rather than the tables
we use in Banker’s algorithm.
• Deadlock avoidance can also be done with Banker’s Algorithm.
02/25/2024 39
Banker’s Algorithm/Deadlock
Avoidance/Deadlock Detection
• Bankers’s Algorithm is a resource allocation and deadlock avoidance
algorithm which test all the request made by processes for resources,
it checks for the safe state, and after granting a request system
remains in the safe state it allows the request, and if there is no safe
state it doesn’t allow the request made by the process.
Inputs to Banker’s Algorithm
• Max needs of resources by each process.
• Currently, allocated resources by each process.
• Max free available resources in the system.
02/25/2024 40
The request will only be granted under the below condition
• If the request made by the process is less than equal to the max
needed for that process.
• If the request made by the process is less than equal to the freely
available resource in the system.
• Timeouts: To avoid deadlocks caused by indefinite waiting, a timeout
mechanism can be used to limit the amount of time a process can
wait for a resource. If the help is unavailable within the timeout
period, the process can be forced to release its current resources and
try again later.
• Need = Maximum Resources Requirement – Currently Allocated
Resources.
• AVAILABE=TOTAL-ALLOCATED
02/25/2024 41
• The Banker's algorithm is a deadlock avoidance algorithm used in
operating systems to allocate resources to processes in a safe manner,
ensuring that deadlock cannot occur. It was developed by Edsger W.
Dijkstra in 1965. The algorithm is primarily used in systems where
resources are allocated in advance and can be safely released when no
longer needed.
• Resources: These are the various types of resources available in the
system, such as printers, memory blocks, etc.
• Processes: These are the tasks or programs running in the system that
require resources to execute.
• Available: This is a vector indicating the number of available instances
of each resource type.
• Allocation: This is a matrix indicating the number of resources of each
type currently allocated to each process.
• Maximum: This is a matrix indicating the maximum number of
resources of each type that each process may request.
02/25/2024 42
02/25/2024 43
02/25/2024 44
Banker’s Algorithm History
• It is a banker algorithm used to avoid deadlock and allocate resources safely
to each process in the computer system.
• The 'S-State' examines all possible tests or activities before deciding whether
the allocation should be allowed to each process.
• It also helps the operating system to successfully share the resources
between all the processes.
• The banker's algorithm is named because it checks whether a person should
be sanctioned a loan amount or not to help the bank system safely simulate
allocation resources.
• In this section, we will learn the Banker's Algorithm in detail. Also, we will
solve problems based on the Banker's Algorithm. To understand the Banker's
Algorithm first we will see a real word example of it.
02/25/2024 45
• Suppose the number of account holders in a particular bank is 'n', and
the total money in a bank is 'T’.
• If an account holder applies for a loan; first, the bank subtracts the
loan amount from full cash and then estimates the cash difference is
greater than T to approve the loan amount.
• These steps are taken because if another person applies for a loan or
withdraws some amount from the bank, it helps the bank manage
and operate all things without any restriction in the functionality of
the banking system.
02/25/2024 46
• Similarly, it works in an operating system. When a new process is
created in a computer system, the process must provide all types of
information to the operating system like upcoming processes,
requests for their resources, counting them, and delays.
• Based on these criteria, the operating system decides which process
sequence should be executed or waited so that no deadlock occurs in
a system.
• Therefore, it is also known as deadlock avoidance algorithm or
deadlock detection in the operating system.
02/25/2024 47
Advantages
• It contains various resources that meet the requirements of each
process.
• Each process should provide information to the operating system for
upcoming resource requests, the number of resources, and how long
the resources will be held.
• It helps the operating system manage and control process requests
for each type of resource in the computer system.
• The algorithm has a Max resource attribute that represents indicates
each process can hold the maximum number of resources in a system.
02/25/2024 48
• Consider a system that contains five process P1, P2, P3, P4, P5 and the
three resources type A,B and C. Following are the resources types: A
has 10, B has 5 and the resource type C has 7 Instance.
PROCESS ALLOCATION MAX NEED AVAILABE REMAINING NEED
A B C A B C A B C A B C
P1 0 1 0 7 5 3
P2 2 0 2 3 2 2
P3 3 0 2 9 0 2
P4 2 1 1 4 2 2
P5 0 0 2 5 3 3
02/25/2024 49
• Deadlock Avoidance algorithm/Also used for Deadlock
Detection(Deadlock Occur in future or not)
• Five Process request the resources( A, B, C)
• A=CPU, B=MEMORY, C=PRINTER LET SAY.
• A=10 CPU/INSTANCES, B=5 MEMORY/INSTANCES, C=5
PRINTER/INSTANCE
• ALLOCATION tells how many resources system has already given to a
particular process.
• Maximum need means which process needs how many resources?
• If there is a deadlock then simply you can say that this is unsafe.
• Safe Means deadlock will not occur.
• And unsafe means deadlock will occur.
02/25/2024 50
If deadlock does not occur then its sequence comes we call safe
sequence. Safe sequence means how do we sequence resources and
process so that deadlock does not occur.
Available means how much is the total availability of resources we
have. Total we have 10, 5 and 7.
02/25/2024 51
PROCESS ALLOCATION MAX NEED AVAILABE REMAINING NEED
A B C A B C A B C A B C
P1 0 1 0 7 5 3 10-7=3 5-2=3 7-5=2
P2 2 0 0 3 2 2
P3 3 0 2 9 0 2
P4 2 1 1 4 2 2
P5 0 0 2 5 3 3
Total 7 2 5
02/25/2024 52
Current Availability=(3,3,2)
Step 2: Remaining Need=Max. Need-Allocation
Remaining Need of P1= (7-0)=0, (3-2)=1,
PROCESS ALLOCATION MAX NEED AVAILABE REMAINING NEED
A B C A B C A B C A B C
P1 0 1 0 7 5 3 10-7=3 5-2=3 7-5=2 7-0=7 5-1=4 3-0=3
P2 2 0 0 3 2 2 3-2=1 2-0=2 2-0=2
P3 3 0 2 9 0 2 9-3=6 0-0=0 2-2=0
P4 2 1 1 4 2 2 4-2=2 2-2=1 2-1=1
P5 0 0 2 5 3 3 5-0=5 3-0=3 3-2=1
Total 7 2 5
02/25/2024 53
PROCESS ALLOCATION MAX NEED AVAILABE REMAINING NEED
A B C A B C A B C A B C
P1 0 1 0 7 5 3 3 3 2 7 4 3
P2 2 0 0 3 2 2 1 2 2
P3 3 0 2 9 0 2 6 0 0
P4 2 1 1 4 2 2 2 1 1
P5 0 0 2 5 3 3 5 3 1
Total 7 2 5
02/25/2024 54
For Process P1:
Need <= Available
7, 4, 3 <= 3, 3, 2 condition is false.[The remaining need to P1 can not be
fulfilled, thus we cannot keep P1 in sequence]
For Process P2:
Need<= Available
1,2,2<=3,3,2 [ True, The remaining need of P2 can be fulfilled]
[All three resources are to be provided at once not individually]
P2 is successfully executed and terminated.
Now, New Available= allocation of P2+Current Available
=(2,0,0)+(3,3,2)=(2+3,0+3,0+2)=(5,3,2)
02/25/2024 55
PROCESS ALLOCATION MAX NEED AVAILABE REMAINING NEED
A B C A B C A B C A B C
P1 0 1 0 7 5 3 3 3 2 7 4 3
P2 2 0 0 3 2 2 5 3 2 1 2 2
P3 3 0 2 9 0 2 6 0 0
P4 2 1 1 4 2 2 2 1 1
P5 0 0 2 5 3 3 5 3 1
Total 7 2 5
02/25/2024 56
• The remaining need of P2 is full filled thus it will be removed from the
system.
02/25/2024 57
PROCESS ALLOCATION MAX NEED AVAILABE REMAINING NEED
A B C A B C A B C A B C
P1 0 1 0 7 5 3 3 3 2 7 4 3
P2 2 0 0 3 2 2 5 3 2
P3 3 0 2 9 0 2 6 0 0
P4 2 1 1 4 2 2 2 1 1
P5 0 0 2 5 3 3 5 3 1
Total 7 2 5
02/25/2024 58
Similarly, we examine another process P3.For Process P3,
Step 3: For Process P3:
P3 Need <= Available
6, 0, 0 < = 5, 3, 2 condition is false.
Similarly, we examine another process, P4.
Step 4: For Process P4:
P4 Need <= Available
2, 1, 1 <= 5, 3, 2 condition is true
New Available resource = Available + Allocation
5, 3, 2 + 2, 1, 1 => 7, 4, 3
Process P4 is successfully executed. P4 will be successfully terminated and
removed from system.
02/25/2024 59
PROCESS ALLOCATION MAX NEED AVAILABE REMAINING NEED
A B C A B C A B C A B C
P1 0 1 0 7 5 3 3 3 2 7 4 3
P2 2 0 0 3 2 2 5 3 2
P3 3 0 2 9 0 2 7 4 3 6 0 0
P4 2 1 1 4 2 2
P5 0 0 2 5 3 3 5 3 1
Total 7 2 5
02/25/2024 60
Similarly, we examine another process P5.
Step 5: For Process P5:
• P5 Need <= Available
• 5, 3, 1 <= 7, 4, 3 condition is true
• New available resource = Available + Allocation
• (7, 4, 3 + 0, 0, 2) => 7, 4, 5
• Process P5 is successfully executed. P5 will be successfully terminated and
removed from system.
02/25/2024 61
PROCESS ALLOCATION MAX NEED AVAILABE REMAINING NEED
A B C A B C A B C A B C
P1 0 1 0 7 5 3 3 3 2 7 4 3
P2 2 0 0 3 2 2 5 3 2
P3 3 0 2 9 0 2 7 4 3 6 0 0
P4 2 1 1 4 2 2 7 4 5
P5 0 0 2 5 3 3
Total 7 2 5
02/25/2024 62
• Step 6: For Process P1:
• 7, 4, 5 + 0, 1, 0 => 7, 5, 5
• P1 is successfully executed and removed it from system.
02/25/2024 63
PROCESS ALLOCATION MAX NEED AVAILABE REMAINING NEED
A B C A B C A B C A B C
P1 0 1 0 7 5 3 3 3 2
P2 2 0 0 3 2 2 5 3 2
P3 3 0 2 9 0 2 7 4 3 6 0 0
P4 2 1 1 4 2 2 7 4 5
P5 0 0 2 5 3 3 7 5 5
Total 7 2 5
02/25/2024 64
• Step 7: For Process P3:
• P3 Need <= Available
• 6, 0, 0 <= 7, 5, 5 condition is true
• New Available Resource = Available + Allocation
• 7, 5, 5 + 3, 0, 2 => 10, 5, 7
• Hence, we execute the banker's algorithm to find the safe state and
the safe sequence like P2, P4, P5, P1 and P3.
02/25/2024 65
PROCESS ALLOCATION MAX NEED AVAILABE REMAINING NEED
A B C A B C A B C A B C
P1 0 1 0 7 5 3 3 3 2
P2 2 0 0 3 2 2 5 3 2
P3 3 0 2 9 0 2 7 4 3 6 0 0
P4 2 1 1 4 2 2 7 4 5
P5 0 0 2 5 3 3 7 5 5
Total 7 2 5
02/25/2024 66
References:
• https://www.slideshare.net/sonalichauhan/operating-system-deadloc
k-galvin
02/25/2024 67