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

Deadlock os lecture notes

Deadlock in operating systems occurs when multiple processes are blocked as each holds a resource and waits for another, leading to an infinite wait. Four necessary conditions for deadlock include mutual exclusion, hold and wait, no preemption, and circular wait. Strategies for handling deadlock include prevention, avoidance, detection and recovery, and ignorance, with various methods to ensure systems remain deadlock-free.

Uploaded by

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

Deadlock os lecture notes

Deadlock in operating systems occurs when multiple processes are blocked as each holds a resource and waits for another, leading to an infinite wait. Four necessary conditions for deadlock include mutual exclusion, hold and wait, no preemption, and circular wait. Strategies for handling deadlock include prevention, avoidance, detection and recovery, and ignorance, with various methods to ensure systems remain deadlock-free.

Uploaded by

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

Deadlock in OS

Deadlock is a situation where-


• The execution of two or more processes is blocked because each process holds
some resource and waits for another resource held by some other process.
. Deadlock is a common problem in multi-processing where several processes
share a specific type of mutually exclusive resource known as a soft lock or
software.
Example 1

Here
• Process P1 holds resource R1 and waits for resource R2 which is held by
process P2.
• Process P2 holds resource R2 and waits for resource R1 which is held by
process P1.
• None of the two processes can complete and release their resource.
• Thus, both the processes keep waiting infinitely.

Example 2
• A real-world example would be traffic, which is going only in one direction.
• Here, a bridge is considered a resource.
• So, when Deadlock happens, it can be easily resolved if one car backs up
(Preempt resources and rollback).
• Several cars may have to be backed up if a deadlock situation occurs.
• So starvation is possible.

Conditions For Deadlock


There are following 4 necessary conditions for the occurrence of deadlock-
1. Mutual Exclusion
2. Hold and Wait
3. No preemption
4. Circular wait

1. Mutual Exclusion.
By this condition,
• There must exist at least one resource in the system which can be used by only
one process at a time.
• If there exists no such resource, then deadlock will never occur.
• Printer is an example of a resource that can be used by only one process at a
time.
2. Hold and Wait.
By this condition,
• There must exist a process which holds some resource and waits for another
resource held by some other process.

3. No Preemption-
By this condition,
• Once the resource has been allocated to the process, it can not be preempted.
• It means resource can not be snatched forcefully from one process and given
to the other process.
• The process must release the resource voluntarily by itself.

4. Circular Wait-
By this condition,
• All the processes must wait for the resource in a cyclic manner where the last
process waits for the resource held by the first process.

Here,
• Process P1 waits for a resource held by process P2.
• Process P2 waits for a resource held by process P3.
• Process P3 waits for a resource held by process P4.
• Process P4 waits for a resource held by process P1.

Important Note
• All these 4 conditions must hold simultaneously for the occurrence of
deadlock.
• If any of these conditions fail, then the system can be ensured deadlock free.

Deadlock Detection | Deadlock Prevention


Deadlock Handling
The various strategies for handling deadlock are-

1. Deadlock Prevention
2. Deadlock Avoidance
3. Deadlock Detection and Recovery
4. Deadlock Ignorance

Deadlock Prevention-
• This strategy involves designing a system that violates one of the four
necessary conditions required for the occurrence of deadlock.
• This ensures that the system remains free from the deadlock.
The various conditions of deadlock occurrence may be violated as-
1. Mutual Exclusion-
• To violate this condition, all the system resources must be such that they can
be used in a shareable mode.
• In a system, there are always some resources which are mutually exclusive by
nature.
• So, this condition cannot be violated.
2. Hold and Wait
This condition can be violated in the following ways-
Approach-01:
In this approach,
• A process has to first request for all the resources it requires for execution.
• Once it has acquired all the resources, only then it can start its execution.
• This approach ensures that the process does not hold some resources and wait
for other resources.
Drawbacks-
The drawbacks of this approach are-
• It is less efficient.
• It is not implementable since it is not possible to predict in advance which
resources will be required during execution.

Approach-02:
In this approach,
• A process is allowed to acquire the resources it desires at the current moment.
• After acquiring the resources, it start its execution.
• Now before making any new request, it has to compulsorily release all the
resources that it holds currently.
• This approach is efficient and implementable.
Approach-03:
In this approach,
• A timer is set after the process acquires any resource.
• After the timer expires, a process has to compulsorily release the resource.

3. No Preemption
• This condition can by violated by forceful preemption.
• Consider a process is holding some resources and request other resources that
can not be immediately allocated to it.
• Then, by forcefully preempting the currently held resources, the condition can
be violated.
A process is allowed to forcefully preempt the resources possessed by some other
process only if-
• It is a high priority process or a system process.
• The victim process is in the waiting state.

4. Circular Wait-
• This condition can be violated by not allowing the processes to wait for
resources in a cyclic manner.
• To violate this condition, the following approach is followed-

Approach-
• A natural number is assigned to every resource.
• Each process is allowed to request for the resources either in only increasing
or only decreasing order of the resource number.
• In case increasing order is followed, if a process requires a lesser number
resource, then it must release all the resources having larger number and vice
versa.
• This approach is the most practical approach and implementable.
• However, this approach may cause starvation but will never lead to deadlock.

Deadlock Avoidance-
• This strategy involves maintaining a set of data using which a decision is
made whether to entertain the new request or not.
• If entertaining the new request causes the system to move in an unsafe state,
then it is discarded.
• This strategy requires that every process declares its maximum requirement of
each resource type in the beginning.
• The main challenge with this approach is predicting the requirement of the
processes before execution.
• Banker’s Algorithm is an example of a deadlock avoidance strategy.

Deadlock Detection and Recovery-


• This strategy involves waiting until a deadlock occurs.
• After deadlock occurs, the system state is recovered.
• The main challenge with this approach is detecting the deadlock.
Deadlock Ignorance-
• This strategy involves ignoring the concept of deadlock and assuming as if it
does not exist.
• This strategy helps to avoid the extra overhead of handling deadlock.
• Windows and Linux use this strategy and it is the most widely used method.
• It is also called as Ostrich approach.

Difference Between Starvation and Deadlock

Here, are some important differences between Deadlock and starvation :

Deadlock Starvation

The deadlock situation occurs when one Starvation is a situation where all the
of the processes got blocked. low priority processes got blocked, and
the high priority processes execute.

Deadlock is an infinite process. Starvation is a long waiting but not an


infinite process.

Every Deadlock always has starvation. Every starvation does n't necessarily
have a deadlock.

Deadlock happens then Mutual It happens due to uncontrolled priority


exclusion, hold and wait. Here, and resource management.
preemption and circular wait do not
occur simultaneously.

Advantages of Deadlock
Here, are pros/benefits of using Deadlock method

• This situation works well for processes which perform a single burst of
activity
• No preemption needed for Deadlock.
• Convenient method when applied to resources whose state can be saved
and restored easily
• Feasible to enforce via compile-time checks
• Needs no run-time computation since the problem is solved in system
design
Disadvantages of Deadlock method
Here, are cons/ drawback of using deadlock method

• Delays process initiation


• Processes must know future resource need
• Pre-empts more often than necessary
• Dis-allows incremental resource requests
• Inherent preemption losses.

Deadlock Problems | Questions


We have discussed-
• In a deadlock state, the execution of multiple processes is blocked.
• There are 4 necessary conditions for the occurrence of deadlock.
• Several Deadlock Handling Strategies exist to deal with the deadlock.
In this article, we will discuss practice problems based on deadlock.
Important Concept-

Consider there are n processes in the system P 1, P2, P3, …… , Pn where-


• Process P1 requires x1 units of resource R
• Process P2 requires x2 units of resource R
• Process P3 requires x3 units of resource R and so on.

In worst case,

The number of units that each process holds = One less than its maximum demand

So,
• Process P1 holds x1 – 1 units of resource R
• Process P2 holds x2 – 1 units of resource R
• Process P3 holds x3 – 1 units of resource R and so on.
Now,
• Had there been one more unit of resource R in the system, system could be
ensured deadlock free.
• This is because that unit would be allocated to one of the processes and it
would get execute and then release its units.
From here, we have-

Maximum Number Of Units That Ensures Deadlock-

Maximum number of units of resource R that ensures deadlock


= (x1-1) + (x2-1) + (x3-1) + …. + (xn-1)
= ( x1 + x2 + x3 + …. + xn ) – n
= ∑xi – n
= Sum of max needs of all n processes – n

Minimum Number Of Units That Ensures No Deadlock-

Minimum number of units of resource R that ensures no deadlock


= One more than maximum number of units of resource R that ensures deadlock
= (∑xi – n) + 1

PRACTICE PROBLEMS BASED ON DEADLOCK IN OS-


Problem-01:
A system is having 3 user processes each requiring 2 units of resource R. The
minimum number of units of R such that no deadlock will occur-
1. 3
2. 5
3. 4
4. 6

Solution-
In worst case,
The number of units that each process holds = One less than its maximum demand
So,
• Process P1 holds 1 unit of resource R
• Process P2 holds 1 unit of resource R
• Process P3 holds 1 unit of resource R
Thus,
• Maximum number of units of resource R that ensures deadlock = 1 + 1 + 1 = 3
• Minimum number of units of resource R that ensures no deadlock = 3 + 1 = 4

Problem-02:
A system is having 10 user processes each requiring 3 units of resource R. The
minimum number of units of R such that no deadlock will occur _____?
Solution-
In worst case,
The number of units that each process holds = One less than its maximum demand

So,
• Process P1 holds 2 units of resource R
• Process P2 holds 2 units of resource R
• Process P3 holds 2 units of resource R and so on.
• Process P10 holds 2 units of resource R
Thus,
• Maximum number of units of resource R that ensures deadlock = 10 x 2 = 20
• Minimum number of units of resource R that ensures no deadlock = 20 + 1 =
21

Problem-03:
A system is having 3 user processes P1, P2 and P3 where P1 requires 2 units of
resource R, P2 requires 3 units of resource R, P3 requires 4 units of resource R.
The minimum number of units of R that ensures no deadlock is _____?
Problem 4:
If there are 6 units of resource R in the system and each process in the system
requires 2 units of resource R, then how many processes can be present at
maximum so that no deadlock will occur?
Problem 5:
A system is having 3 user processes P1, P2 and P3 where P1 requires 21 units of
resource R, P2 requires 31 units of resource R, P3 requires 41 units of resource
R. The minimum number of units of R that ensures no deadlock is _
Problem-06:
If there are 6 units of resource R in the system and each process in the system
requires 3 units of resource R, then how many processes can be present at
maximum so that no deadlock will occur?
Solution-
In worst case,
The number of units that each process holds = One less than its maximum demand

So,
• Process P1 holds 2 units of resource R
• Process P2 holds 2 units of resource R
• Process P3 holds 2 units of resource R

Thus,
• Minimum number of processes that ensures deadlock = 3
• Maximum number of processes that ensures no deadlock = 3 – 1 = 2

Problem-07:
If there are 100 units of resource R in the system and each process in the system
requires 2 units of resource R, then how many processes can be present at
maximum so that no deadlock will occur?
Solution-
In worst case,
The number of units that each process holds = One less than its maximum demand

So,
• Process P1 holds 1 unit of resource R
• Process P2 holds 1 unit of resource R
• Process P3 holds 1 unit of resource R and so on.
• Process P100 holds 1 unit of resource R

Thus,
• Minimum number of processes that ensures deadlock = 100
• Maximum number of processes that ensures no deadlock = 100 – 1 = 99
Problem-08:
If there are 100 units of resource R in the system and each process in the system
requires 4 units of resource R, then how many processes can be present at
maximum so that no deadlock will occur?
Solution-
In worst case,
The number of units that each process holds = One less than its maximum demand

So,
• Process P1 holds 3 units of resource R
• Process P2 holds 3 units of resource R
• Process P3 holds 3 units of resource R and so on.
• Process P33 holds 3 units of resource R
• Process P34 holds 1 unit of resource R

Thus,
• Minimum number of processes that ensures deadlock = 34
• Maximum number of processes that ensures no deadlock = 34 – 1 = 33
Problem-09:

If there are 100 units of resource R in the system and each process in the system
requires 5 units of resource R, then how many processes can be present at
maximum so that no deadlock will occur?
Solution-
In worst case,
The number of units that each process holds = One less than its maximum demand

So,
• Process P1 holds 4 units of resource R
• Process P2 holds 4 units of resource R
• Process P3 holds 4 units of resource R and so on.
• Process P25 holds 4 units of resource R
Thus,
• Minimum number of processes that ensures deadlock = 25
• Maximum number of processes that ensures no deadlock = 25 – 1 = 24

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy