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

OSC Lecture 10-11

The document discusses deadlock in operating systems, defining it as a situation where processes are blocked due to resource holding and waiting. It outlines the four necessary conditions for deadlock (mutual exclusion, hold and wait, no preemption, and circular wait) and methods for deadlock management, including prevention, avoidance, detection, and recovery. The Banker's Algorithm is highlighted as a strategy for resource allocation that ensures the system remains in a safe state to prevent deadlock.

Uploaded by

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

OSC Lecture 10-11

The document discusses deadlock in operating systems, defining it as a situation where processes are blocked due to resource holding and waiting. It outlines the four necessary conditions for deadlock (mutual exclusion, hold and wait, no preemption, and circular wait) and methods for deadlock management, including prevention, avoidance, detection, and recovery. The Banker's Algorithm is highlighted as a strategy for resource allocation that ensures the system remains in a safe state to prevent deadlock.

Uploaded by

legendapex166
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Operating System Concepts

Lecture 10-11
by
Kalimullah Khan
Assistant Professor
• Lecture Outlines

• Deadlock
• Conditions of Dead Lock
• Methods of Dead Lock Removing
• Prevention
• Avoidance
• Detection
• Recovery
Deadlock
• A deadlock is a situation where a set of processes is blocked because each process is holding a
resource and waiting for another resource acquired by some other process.
• Deadlock is a situation in computing where two or more processes are unable to proceed because
each is waiting for the other to release resources.

• 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. This is a
practical example of deadlock.
Characteristics of Deadlock
• Mutual Exclusion: At least one resource must be held in a non-shareable mode, i.e., only one
process can use the resource at a time.

• Hold and Wait: A process that is holding at least one resource is waiting for additional resources
held by other processes.

• No Preemption: Resources cannot be forcibly removed from the processes holding them; they
must be released voluntarily.

• Circular Wait: A set of processes {P1, P2, ..., Pn} exists such that each process Pi is waiting for a
resource held by process Pi+1, and process Pn is waiting for a resource held by process P1, forming
a cycle.
How does deadlock occur in the operating system
Before going into detail about how deadlock occurs in the Operating System, let’s first discuss how
the Operating System uses the resources present. A process in an operating system uses resources in
the following way.

• Requests a resource
• Use the resource
• Releases the resource

A 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.
Conditions of Deadlock
• The four necessary conditions for a deadlock to occur are known as the Coffman conditions. All
four of these conditions must be present simultaneously for a deadlock to take place in a system.

• These conditions are:


1. Mutual Exclusion
Definition: A resource is in a non-shareable mode, meaning only one process can use the resource
at any given time.
Example: If a printer is assigned to a process, no other process can use it until the first process
releases it.
Impact on Deadlock: If multiple processes are contending for resources that cannot be shared, this
condition makes deadlock possible.
2. Hold and Wait
• Definition: A process is holding at least one resource and is waiting for additional resources that
are currently held by other processes.

• Example: Process A is holding Resource 1 and is waiting for Resource 2, while Process B is holding
Resource 2 and waiting for Resource 1.

• Impact on Deadlock: If processes hold resources while waiting for others, it can create a cycle of
dependencies, which is crucial for deadlock to occur.
3. No Preemption

• Definition: Resources cannot be forcibly taken from processes. Once a process holds a resource, it
cannot be preempted (taken away) until the process voluntarily releases it.

• Example: If Process A holds Resource 1 and is waiting for Resource 2, the system cannot force
Process A to release Resource 1 to satisfy other processes.

• Impact on Deadlock: The inability to preempt resources means that if a process is holding a
resource and waiting indefinitely for others, it cannot be interrupted or helped by the system,
allowing deadlock to persist.
4. Circular Wait
• Definition: A set of processes {P1, P2, ..., Pn} exists such that each process Pi is waiting for a
resource held by process Pi+1, and the last process Pn is waiting for a resource held by P1, forming
a cycle.

• Example: Process A is waiting for Resource 2 (held by Process B), Process B is waiting for Resource
3 (held by Process C), and Process C is waiting for Resource 1 (held by Process A). This forms a
circular wait.

• Impact on Deadlock: The circular wait condition is the key feature of a deadlock. It creates a cycle
in which no process can proceed because each one is waiting on another.
Methods of deadlock removing
• There are several methods that operating systems use to remove or avoid deadlocks. These
methods fall into four main categories:
1. Prevention
2. Avoidance
3. Detection
4. Recovery

Lets discuss each method in detail with the help of examples.


1. Prevention
• Deadlock prevention aims to ensure that at least one of the Coffman conditions (the four
necessary conditions for deadlock) is never met.
• By breaking one of these conditions, we can prevent deadlock from occurring.

Methods to prevent deadlock:


1. Eliminate Mutual Exclusion: If resources could be shared among processes (i.e., allow multiple
processes to access the same resource simultaneously), this would prevent mutual exclusion.
However, this is not always feasible, especially for resources like printers or files, which cannot
be shared.
• Example: In the case of printers, we cannot eliminate mutual exclusion because only one process
can use a printer at a time.
2. Eliminate Hold and Wait: This condition can be avoided by ensuring that a process requests all
the resources it needs at once. If it cannot get all the resources it needs, it will not hold any
resource and will instead wait until it can acquire all the resources simultaneously.
• Example: If Process A needs Resources 1 and 2, it must request both resources at once. If it cannot
obtain both, it will not hold Resource 1 and wait for Resource 2. It must release Resource 1 and
wait for both to be available together.

3. Eliminate no Preemption: This condition can be avoided by allowing the system to preempt
resources from a process if it needs them. If a process is holding a resource and waiting for
others, the system can force it to release its resource and allow another process to use it.
• Example: If Process A is holding a resource but needs to wait for another, the system might
preempt the resource from Process A, allowing another process to use it. This would prevent the
process from holding resources while waiting for others.
4. Eliminate Circular Wait: Circular wait can be avoided by imposing an ordering of resource types.
Each process can request resources in an increasing order, so a cycle cannot form.

• Example: If resources are numbered (say, Resource 1, Resource 2, etc.), processes must always
request lower-numbered resources first. For instance, Process A must always request Resource 1
before Resource 2. This prevents a cycle from occurring.
2. Deadlock Avoidance
Deadlock avoidance is a more flexible approach than prevention. Instead of enforcing strict rules,
deadlock avoidance dynamically checks the system’s state to ensure that allocating resources will
not lead to deadlock.

One of the most common algorithms for deadlock avoidance is the Banker’s Algorithm (used for
allocating resources to multiple processes safely).

Example of Deadlock Avoidance: Banker's Algorithm


The Banker’s Algorithm works by evaluating whether resource allocation would lead to a safe state
(where deadlock is not possible). It checks the current allocation of resources and the maximum
demands of processes to ensure that resources can be allocated without creating a deadlock.
• Example:
• Process A requests Resource 1. The system checks if allocating Resource 1 will leave enough
resources for other processes to finish (i.e., it checks if it can still allocate resources to other
processes in a safe order). If it can, the resource is allocated. If not, the process is forced to wait.

• If any process cannot proceed because its resource request might lead to a potential deadlock, the
system denies the request, ensuring the system stays in a safe state.
3. Deadlock Detection
In deadlock detection, the system allows deadlock to occur but has mechanisms to detect it when it
happens. Once deadlock is detected, the system can take action to handle it.

Steps involved in deadlock detection:


The system periodically checks the state of resources and processes to see if there is any circular
wait or other deadlock condition.

This can be done using a Resource Allocation Graph (RAG) or by maintaining a Wait-for Graph, which
shows the relationships between processes and resources. A cycle in these graphs indicates a
deadlock.
• Example of Deadlock Detection:
• Resource Allocation Graph (RAG):
• Each process and resource is represented as a node in the graph. Directed edges are used to show
which process is holding which resource and which process is waiting for which resource.
• If there is a cycle in the graph, it indicates a deadlock.

• For example, if Process A is holding Resource 1 and waiting for Resource 2, and Process B is holding
Resource 2 and waiting for Resource 1, a cycle is formed (A → 1 → B → 2 → A), indicating
deadlock.
4. Deadlock Recovery
Once deadlock is detected, the system must take action to recover from the deadlock. There are two
primary strategies for recovery: process termination and resource preemption.
1. Process Termination
The system can abort one or more processes involved in the deadlock. This can be done in two ways:
Abort all deadlocked processes: This ensures that all deadlocked processes are terminated, but it
may be inefficient.
Abort one process at a time: The system can terminate processes one by one until the deadlock is
broken. The process that is aborted can be selected based on criteria such as the process with the
least resources or the one that has been running the longest.
Example: If Process A and Process B are deadlocked, the system may choose to terminate Process A,
thus breaking the cycle and freeing its resources for other processes.
2. Resource Preemption
The system can take resources away from processes that hold them, preempting the resources and
giving them to other processes. This can be done by forcing processes to release resources they are
holding so that other processes can proceed.

Example: If Process A is holding Resource 1 and waiting for Resource 2, but Process B needs
Resource 1, the system may preempt Resource 1 from Process A and allocate it to Process B. This can
break the deadlock, but the preempted process may need to be restarted later.
Banker's Algorithm
• The Banker's Algorithm is used to allocate resources in a way that ensures a system doesn't enter a
deadlock or an unsafe state.
• It works by checking if a resource request can be granted without leaving the system in an unsafe
state.
• If granting the request leads to a safe state, it is allowed; otherwise, the request is denied until it is
safe.
• Key Concepts in Banker's Algorithm:

• Available: The number of available instances of each resource type.


• Max: The maximum number of instances of each resource type that each process might need.
• Allocation: The number of instances of each resource type currently allocated to each process.
• Need: The remaining resources that each process needs to complete its execution. Calculated as:

Need = Max−Allocation
• Example Scenario
• Let's consider a system with 3 processes (P1, P2, P3) and 3 types of resources (A, B, C).
• Initial Data:
• Available Resources:
• A = 3, B = 3, C = 2
Max Matrix (Maximum resources each process might need):
Allocation Matrix (Currently allocated resources):
Need Matrix (Remaining resources needed by each process):
Need = Max−Allocation
• Step-by-Step Banker's Algorithm

• Step 1: Can P1 Finish?


• P1's Need = (1, 1, 1)
• Available Resources = (3, 3, 2)
• Since P1's Need (1, 1, 1) is less than or equal to Available (3, 3, 2), P1 can finish. When P1 finishes,
it releases the resources it holds:
• P1 releases (2, 2, 1), which is its current allocation.

• Step 2: Update Available Resources


• After P1 finishes and releases its resources:
• New Available = (3, 3, 2) + (2, 2,1) = (5, 5, 3)
• Step 3: Can P2 Finish?
• P2's Need = (1, 0, 1)
• Available Resources = (5, 5, 3)
• Since P2's Need (1, 0, 1) is less than or equal to Available (5, 5, 3), P2 can finish. When P2 finishes,
it releases the resources it holds:
• P2 releases (4, 5, 2).

• Step 4: Update Available Resources


• After P2 finishes and releases its resources:

• New Available = (5, 5, 3) + (4, 5,2) = (9, 10, 5)


• Step 5: Can P3 Finish?
• P3's Need = (1, 2, 1)
• Available Resources = (9, 10, 5)
• Since P3's Need (1, 2, 1) is less than or equal to Available (9, 10, 5), P3 can finish. When P3
finishes, it releases the resources it holds:
• P3 releases (8, 8, 4).

• Step 6: Final Available Resources


• After P3 finishes and releases its resources:
• New Available = (9, 10, 5) + (8, 8, 4) = (17, 18, 9)
• Conclusion:
• Since all processes can finish one by one, the system is in a safe state. The process sequence is: P1
→ P2 → P3.
Tasks for the Students Home Work

1. Scenario: Simple Resource Allocation


• In a small computer lab, there are three types of resources: printers, computers, and desks. The lab
has four students: Student A, Student B, Student C, and Student D. Each student needs some of
these resources to complete their tasks.
• The goal is to ensure that resources are allocated in a way that no student is left waiting
indefinitely, and that no student is blocked from completing their task. You need to determine if it's
safe to allocate resources to the students using the Banker's Algorithm.
• Available Resources:
• Printers: 3
• Computers: 5
• Desks: 4
• Task for the Students:
• Calculate the Available Resources:

• Total resources minus the resources allocated to each student.


• Check if the system is in a safe state:

• Apply the Banker's Algorithm to check if there is a sequence of students that can finish their tasks
and release resources, leaving enough resources for the next student in line.
Thank You

You might also like