OSC Lecture 10-11
OSC Lecture 10-11
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.
• 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
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).
• 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.
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:
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
• 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