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

OS Unit 4

A deadlock in an operating system occurs when a set of processes are blocked because each process holds a resource and waits for another resource held by another process. The four necessary conditions for a deadlock are: mutual exclusion, hold and wait, no preemption, and circular wait. A resource allocation graph models processes and resources and can be used to detect deadlocks.

Uploaded by

BHAKTI AYAREKAR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

OS Unit 4

A deadlock in an operating system occurs when a set of processes are blocked because each process holds a resource and waits for another resource held by another process. The four necessary conditions for a deadlock are: mutual exclusion, hold and wait, no preemption, and circular wait. A resource allocation graph models processes and resources and can be used to detect deadlocks.

Uploaded by

BHAKTI AYAREKAR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

OS Unit 4

Q 1. What is a deadlock in an operating system?

• 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.
• In this situation, none of the process gets executed since the
resource it needs, is held by some other process which is also
waiting for some other resource to be released.
• Consider an example when two trains are coming toward each
other on the same track and there is only one track, none of
the trains can move once they are in front of each other.
• A similar situation occurs in operating systems when there are
two or more processes that hold some resources and wait for
resources held by other(s).
• For example, in the below diagram, Process 1 is holding
Resource 1 and waiting for resource 2 which is acquired by
process 2, and process 2 is waiting for resource 1.

Examples Of Deadlock
1. The system has 2 tape drives. P0 and P1 each hold one tape
drive and each needs another one.
2. Semaphores A and B, initialized to 1, P0, and P1 are in
deadlock as follows:
• P0 executes wait(A) and preempts.
• P1 executes wait(B).
• Now P0 and P1 enter in deadlock.

P0 P1

wait(A); wait(B)

wait(B); wait(A)

3. Assume the space is available for allocation of 200K bytes, and


the following sequence of events occurs.

P0 P1

Request 80KB; Request 70KB;

Request 60KB; Request 80KB;

Deadlock occurs if both processes progress to their second


request.
Deadlock can arise if the following four conditions hold
simultaneously (Necessary Conditions)
Mutual Exclusion: Two or more resources are non-shareable
(Only one process can use at a time)
Hold and Wait: A process is holding at least one resource and
waiting for resources.
No Preemption: A resource cannot be taken from a process
unless the process releases the resource.
Circular Wait: A set of processes waiting for each other in circular
form.
Q 2. What are the four conditions that must be met for a
deadlock to occur?
How a Deadlock Can Occur?
Let’s see that both processes begin execution at the same time.
1. Process A obtains Resource 1.
2. Process B obtains Resource 2.
Process A requires Resource 2 to continue execution but is unable to
do so because Process B is currently holding Resource 2.
Similarly, Process B requires Resource 1 to continue execution but is
unable to do so because Process A is currently holding Resource 1.
Both processes are now stuck in a loop:
1. Process A is awaiting Resource 2 from Process B.
2. Process B is awaiting Resource 1 from Process A.
We have a deadlock because neither process can release the
resource it is holding until it completes its task, and neither can
proceed without the resource the other process is holding. Both
processes are effectively “deadlocked,” unable to move forward.
To break the deadlock and free up resources for other processes in
this situation, an external intervention, such as the operating
system killing one or both processes, would be required. Deadlocks
are undesirable in operating systems because they waste resources
and have a negative impact on overall system performance and
responsiveness. To prevent deadlocks, various resource allocation
and process scheduling algorithms, such as deadlock detection and
avoidance, are employed.
Necessary Conditions for the Occurrence of a Deadlock
Let’s explain all four conditions related to deadlock in the context of
the scenario with two processes and two resources:
Mutual Exclusion
This condition requires that at least one resource be held in a non-
shareable mode, which means that only one process can use the
resource at any given time. Both Resource 1 and Resource 2 are
non-shareable in our scenario, and only one process can have
exclusive access to each resource at any given time.
As an example:
• Process A obtains Resource 1.
• Process B acquires Resource 2.
Hold and Wait
The hold and wait condition specifies that a process must be
holding at least one resource while waiting for other processes to
release resources that are currently held by other processes. In our
example,
• Process A has Resource 1 and is awaiting Resource 2.
• Process B currently has Resource 2 and is awaiting Resource 1.
• Both processes hold one resource while waiting for the other,
satisfying the hold and wait condition.
No Preemption
Preemption is the act of taking a resource from a process before it
has finished its task. According to the no preemption condition,
resources cannot be taken forcibly from a process a process can only
release resources voluntarily after completing its task. In our
scenario, neither Process A nor Process B can be forced to release
the resources in their possession. The processes can only release
resources voluntarily.
Circular Wait
This condition implies that circular processes must exist, with each
process waiting for a resource held by the next process in the chain.
In our scenario, Process A is waiting for Resource 2, which is being
held by Process B.
Process B is awaiting Resource 1 from Process A.
This circular chain of dependencies causes a deadlock because
neither process can proceed, resulting in a system shutdown.
To summarise, in the context of two processes and two resources,
all four conditions (mutual exclusion, hold and wait, no preemption,
and circular wait) must be met at the same time. Deadlocks are a
major concern in operating systems, and various techniques are
used to avoid them to prevent, detect, and recover from them.
Q 3. What is a resource allocation graph in an operating
system?
Resource Allocation Graph (RAG)
• A resource allocation graphs shows which resource is held by
which process and which process is waiting for a resource of a
specific kind. It is amazing and straight – forward tool to
outline how interacting processes can deadlock.
• Therefore, resource allocation graph describe what the
condition of the system as far as process and resources are
concern like what number of resources are allocated and what
is the request of each process.
• Everything can be represented in terms of graph. One of the
benefit of having a graph is, sometimes it is conveivable to see
a deadlock straight forward by utilizing RAG and however you
probably won’t realize that by taking a glance at the table.
Types of Vertices in RAG
So RAG also contains vertices and edges. In RAG vertices are two
types
1. Process Vertex: Every process will be represented as a process
vertex. Generally, the process will be represented with a circle.
2. Resource Vertex: Every resource will be represented as a
resource vertex. It is also two types:
• Single instance type resource: It represents as a box, inside
the box, there will be one dot. So the number of dots indicate
how many instances are present of each resource type.
• Multi-resource instance type resource: It also represents as a
box, inside the box, there will be many dots present.

How many Types of Edges are there in RAG?


Now coming to the edges of RAG.There are two types of edges in
RAG –
• Assign Edge: If you already assign a resource to a process
then it is called Assign edge.
• Request Edge: It means in future the process might want
some resource to complete the execution, that is called request
edge.
So, if a process is using a resource, an arrow is drawn from the
resource node to the process node. If a process is requesting a
resource, an arrow is drawn from the process node to the resource
node.
Example 1 (Single instances RAG)

If there is a cycle in the Resource Allocation Graph and each


resource in the cycle provides only one instance, then the processes
will be in deadlock. For example, if process P1 holds resource R1,
process P2 holds resource R2 and process P1 is waiting for R2 and
process P2 is waiting for R1, then process P1 and process P2 will be
in deadlock.

Here’s another example, that shows Processes P1 and P2 acquiring


resources R1 and R2 while process P3 is waiting to acquire both
resources. In this example, there is no deadlock because there is no
circular dependency. So cycle in single-instance resource type is the
sufficient condition for deadlock.

Q 4. What is the difference between a system-level


deadlock and a user-level deadlock?
A system-level deadlock and a user-level deadlock refer to different
contexts in which deadlocks can occur in computer systems:
1. **System-Level Deadlock**:
- This occurs at the operating system level, involving system
resources such as memory, CPU, and I/O devices.
- System-level deadlocks are typically managed by the operating
system itself.
- Examples of system-level deadlocks include:
- Deadlock between processes competing for access to shared
resources like files, semaphores, or sockets.
- Deadlock in memory allocation where processes are waiting for
memory that is held by others and cannot proceed.
- Deadlock in device allocation when processes are waiting for
I/O resources like printers or disk drives.

2. **User-Level Deadlock**:
- This occurs within a specific application or program, involving
resources managed by the application.
- User-level deadlocks are handled by the application or
programming language rather than the operating system.
- Examples of user-level deadlocks include:
- Deadlock in multithreaded programs where threads are waiting
for resources held by other threads in the same program.
- Deadlock in database systems where transactions are waiting
for data held by other transactions.
- Deadlock in distributed systems where processes or nodes are
waiting for resources held by other processes or nodes.

In summary, system-level deadlocks occur at the OS level involving


system resources, while user-level deadlocks occur within
applications involving resources managed by the application itself.
Q 5. What is deadlock prevention in an operating system?
Deadlock Prevention
• Deadlock prevention is a technique used in operating systems
to ensure that deadlocks, which are situations where two or
more processes are unable to proceed because each is waiting
for the other to release a resource, do not occur.
• It involves designing the system in such a way that at least one
of the necessary conditions for deadlock cannot hold.
• This typically requires careful resource allocation and
tracking, as well as enforcing certain rules and protocols to
prevent deadlock formation.
We can prevent a Deadlock by eliminating any of the above four
conditions.
Eliminate Mutual Exclusion: It is not possible to dis-satisfy
the mutual exclusion because some resources, such as the tape drive
and printer, are inherently non-shareable.
Eliminate Hold and wait: Allocate all required resources to the
process before the start of its execution, this way hold and wait
condition is eliminated but it will lead to low device utilization. for
example, if a process requires a printer at a later time and we have
allocated a printer before the start of its execution printer will
remain blocked till it has completed its execution. The process will
make a new request for resources after releasing the current set of
resources. This solution may lead to starvation.

Eliminate No Preemption : Preempt resources from the process


when resources are required by other high-priority processes.
Eliminate Circular Wait : Each resource will be assigned a
numerical number. A process can request the resources to
increase/decrease. order of numbering. For Example, if the P1
process is allocated R5 resources, now next time if P1 asks for R4,
R3 lesser than R5 such a request will not be granted, only a request
for resources more than R5 will be granted.
Detection and Recovery: Another approach to dealing with
deadlocks is to detect and recover from them when they occur. This
can involve killing one or more of the processes involved in the
deadlock or releasing some of the resources they hold.
Q6. What is deadlock avoidance in an operating system?
• 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.
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.
Banker’s Algorithm
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
1. Max needs of resources by each process.
2. Currently, allocated resources by each process.
3. Max free available resources in the system.
The request will only be granted under the below
condition
1. If the request made by the process is less than equal to the
max needed for that process.
2. 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.
Example:
Total resources in system:
ABCD
6576

The total number of resources are


Available system resources are:
ABCD
3112
Available resources are
Processes (currently allocated resources):
ABCD
P1 1 2 2 1
P2 1 0 3 3
P3 1 2 1 0

Maximum resources we have for a process


Processes (maximum resources):
ABCD
P1 3 3 2 2
P2 1 2 3 4
P3 1 3 5 0

Need = Maximum Resources Requirement – Currently


Allocated Resources.
Need = maximum resources - currently allocated
resources.
Processes (need resources):
ABCD
P1 2 1 0 1
P2 0 2 0 1
P3 0 1 4 0

Q 7. What is deadlock detection in an operating system?

Deadlock is a situation that occurs in a computer system when two


or more processes are blocked and waiting for each other to release
resources, resulting in a stalemate.
• It's a serious issue in operating systems as it can cause the
entire system to freeze or crash. Therefore, detecting and
resolving deadlock is crucial for the smooth operation of any
computer system.
• Deadlock detection algorithms are used to identify the
presence of deadlocks in computer systems.
• These algorithms examine the system's processes and
resources to determine if there is a circular wait situation that
could lead to a deadlock.
• If a deadlock is detected, the algorithm can take steps to
resolve it and prevent it from occurring again in the future.
• Deadlock detection and recovery is an important aspect of
operating system design and management, as it affects the
stability and performance of the system.
• The choice of deadlock detection and recovery approach
depends on the specific requirements of the system and the
trade-offs between performance, complexity, and risk
tolerance.
• The operating system must balance these factors to ensure
that deadlocks are effectively detected and resolved.

Deadlock Detection:
1. If resources have a single instance –
In this case for Deadlock detection, we can run an algorithm to
check for the cycle in the Resource Allocation Graph. The presence
of a cycle in the graph is a sufficient condition for deadlock. In
single instanced resource types, if a cycle is being formed in the
system then there will definitely be a deadlock.

In the above diagram, resource 1 and resource 2 have single


instances. There is a cycle R1 → P1 → R2 → P2. So, Deadlock is
Confirmed.

2. If there are multiple instances of resources –


On the other hand, in multiple instanced resource type graph,
detecting a cycle is not just enough. We have to apply the safety
algorithm on the system by converting the resource allocation graph
into the allocation matrix and request matrix.Detection of the cycle
is necessary but not a sufficient condition for deadlock detection, in
this case, the system may or may not be in deadlock varies
according to different situations.

3. Wait-For Graph Algorithm –


The Wait-For Graph Algorithm is a deadlock detection algorithm
used to detect deadlocks in a system where resources can have
multiple instances. The algorithm works by constructing a Wait-For
Graph, which is a directed graph that represents the dependencies
between processes and resources.

Q 8. What is deadlock recovery in an operating system?


• “Recovery from Deadlock in Operating Systems” refers to the set of
techniques and algorithms designed to detect, resolve, or mitigate deadlock
situations. These methods ensure that the system can continue processing
tasks efficiently without being trapped in an eternal standstill.
• When a Deadlock Detection Algorithm determines that a deadlock has
occurred in the system, the system must recover from that deadlock.
Approaches To Breaking a Deadlock
1.Process Termination
To eliminate the deadlock, we can simply kill one or more processes. For this, we use
two methods:
1. Abort all the Deadlocked Processes: Aborting all the processes will certainly
break the deadlock but at a great expense. The deadlocked processes may
have been computed for a long time, and the result of those partial
computations must be discarded and there is a probability of recalculating
them later.

2. Abort one process at a time until the deadlock is eliminated: Abort one
deadlocked process at a time, until the deadlock cycle is eliminated from the
system. Due to this method, there may be considerable overhead, because,
after aborting each process, we have to run a deadlock detection algorithm to
check whether any processes are still deadlocked.
2.Resource Preemption
To eliminate deadlocks using resource preemption, we preempt some resources from
processes and give those resources to other processes. This method will raise three
issues –
1. Selecting a victim: We must determine which resources and which processes
are to be preempted and also in order to minimize the cost.

2. Rollback: We must determine what should be done with the process from
which resources are preempted. One simple idea is total rollback. That means
aborting the process and restarting it.

3. Starvation: In a system, it may happen that the same process is always picked
as a victim. As a result, that process will never complete its designated task.
This situation is called Starvation and must be avoided. One solution is that a
process must be picked as a victim only a finite number of times.
3.Priority Inversion
A technique for breaking deadlocks in real-time systems is called priority inversion.
This approach alters the order of the processes to prevent stalemates. A higher
priority is given to the process that already has the needed resources, and a lower
priority is given to the process that is still awaiting them. The inversion of priorities
that can result from this approach can impair system performance and cause
performance issues. Additionally, because higher-priority processes may continue to
take precedence over lower-priority processes, this approach may starve lower-
priority processes of resources.
4.RollBack
In database systems, rolling back is a common technique for breaking deadlocks.
When using this technique, the system reverses the transactions of the involved
processes to a time before the deadlock. The system must keep a log of all
transactions and the system’s condition at various points in time in order to use this
method. The transactions can then be rolled back to the initial state and executed
again by the system. This approach may result in significant delays in the
transactions’ execution and data loss.

Q 9. What is the difference between a conservative


algorithm and an optimistic algorithm in deadlock
avoidance?
In deadlock avoidance, conservative and optimistic algorithms are
two approaches used to prevent deadlock situations in concurrent
systems. Here's the difference between them:

**Conservative Algorithm:**

- **Definition:** Conservative algorithms take a cautious approach


to resource allocation, avoiding situations that could potentially
lead to deadlock.

- **Resource Allocation:** In conservative algorithms, resources are


allocated only if it can be guaranteed that the allocation will not lead
to deadlock.

- **Safety Guarantee:** Conservative algorithms ensure safety by


analyzing the system's current state and potential future resource
requests before granting a resource request.

- **Example:** The Banker's Algorithm is a classic example of a


conservative algorithm. It checks if allocating resources to a process
would leave enough resources to satisfy the future resource requests
of all processes, ensuring no deadlock will occur.

**Optimistic Algorithm:**

- **Definition:** Optimistic algorithms take a more lenient


approach to resource allocation, allowing resource requests to
proceed optimistically and resolving any potential deadlocks as they
arise.

- **Resource Allocation:** In optimistic algorithms, resources are


allocated without checking for potential deadlock. Processes are
allowed to proceed with their resource requests optimistically.

- **Safety Guarantee:** Safety is guaranteed by periodically


checking for deadlock conditions and then taking corrective actions
to resolve them if they occur.

- **Example:** The Wound-Wait and Wait-Die algorithms are


examples of optimistic deadlock avoidance. They allow processes to
proceed with their resource requests optimistically, but if a process
is found to be waiting too long for a resource, it may be "wounded"
or "killed" to prevent deadlock.

**Difference:**

1. **Approach to Resource Allocation:**


- Conservative algorithms: They check resource allocation before
granting it, ensuring that it won't lead to deadlock.
- Optimistic algorithms: They allocate resources optimistically
without immediate deadlock checks, resolving potential deadlocks
as they arise.

2. **Safety Guarantee:**
- Conservative algorithms: They provide a safety guarantee by
ensuring that the system state is always deadlock-free before
making resource allocations.
- Optimistic algorithms: They provide safety by detecting and
resolving deadlocks dynamically when they occur.

3. **Resource Utilization:**
- Conservative algorithms: They may lead to lower resource
utilization as resources are allocated cautiously.
- Optimistic algorithms: They can lead to higher resource
utilization but may have to deal with occasional deadlocks.

In summary, conservative algorithms take a cautious approach by


ensuring that resource allocations won't lead to deadlock, while
optimistic algorithms are more lenient, allowing resource allocation
to proceed optimistically and dealing with deadlocks as they arise.

Q 10. What is the difference between a deadlock and a


livelock in an operating system?

Deadlock and livelock are both undesirable situations in operating


systems, but they differ in their characteristics and how they occur.
**Deadlock:**
- **Definition:** Deadlock occurs when two or more processes are
indefinitely blocked because each is waiting for a resource held by
another process in the set.
- **Cause:** Deadlock typically arises in a system when each
process holds at least one resource and is waiting to acquire
additional resources held by other processes. As a result, none of
the processes can proceed.
- **State of Processes:** In a deadlock, processes are blocked and
unable to proceed further. They are stuck in a waiting state
indefinitely.
- **Resource Usage:** Resources involved in deadlock are held by
processes and are not released until the deadlock is broken.
- **Example:** Consider a scenario where Process A holds Resource
X and requests Resource Y, while Process B holds Resource Y and
requests Resource X. Both processes are waiting for a resource held
by the other, resulting in a deadlock.
**Livelock:**
- **Definition:** Livelock occurs when processes continuously
change their states in response to the actions of other processes
without making progress. In a livelock, processes are not blocked
but are stuck in a loop of states, preventing any meaningful work
from being accomplished.
- **Cause:** Livelock often happens when multiple processes are
trying to resolve a resource conflict by repeatedly changing their
states, but none of them can make progress.
- **State of Processes:** In a livelock, processes are active but are
not making any real progress due to repeated state changes.
- **Resource Usage:** Resources in a livelock are actively used and
released, but the processes involved are unable to achieve their
intended goals.
- **Example:** Imagine two people walking towards each other in a
narrow hallway. Each person tries to move to the side to let the
other pass, but they end up continuously moving in the same
direction, blocking each other's path. This results in a situation
where neither can pass, even though they are not strictly blocked.
**Difference:**
1. **State of Processes:**
- **Deadlock:** Processes are blocked and cannot proceed further.
- **Livelock:** Processes are not blocked but are stuck in a loop of
states, preventing progress.
2. **Resource Usage:**
- **Deadlock:** Resources involved in deadlock are held by
processes and are not released until the deadlock is resolved.
- **Livelock:** Resources are actively used and released, but
processes are unable to make progress.
3. **Nature of Condition:**
- **Deadlock:** It's a condition where processes are stuck waiting
for resources.
- **Livelock:** It's a condition where processes are continually
changing states without making progress.

In essence, deadlock is a situation where processes are stuck waiting


for each other to release resources, while livelock is a situation
where processes are continuously changing states but not making
any progress. Both are undesirable in an operating system but
require different strategies for resolution.

You might also like