Deadlock in Operating Systems
A deadlock in an operating system is a situation where two or more processes are unable to
proceed because each is waiting for the other to release resources. In simpler terms, it's a
standstill situation in which processes cannot continue their execution due to mutual blocking.
Understanding deadlocks is crucial for system stability and resource management.
Concept of Deadlock
1. Processes and Resources:
o Processes: Active programs that require resources to execute (e.g., CPU time,
memory, I/O devices).
o Resources: Any hardware or software components that processes use (e.g.,
printers, memory, files).
For example, if you're printing a document, your process (the program sending the
print job) needs the printer (which is the resource) to complete that task.
2. Coffman Conditions: For a deadlock to occur, four conditions, known as the Coffman
conditions, must hold:
o Mutual Exclusion: At least one resource must be held in a non-shareable mode.
o Hold and Wait: Processes holding resources are waiting for additional resources.
o No Preemption: Resources cannot be forcibly taken from processes.
o Circular Wait: There exists a set of processes, each of which is waiting for a
resource held by the next process in the cycle.
How Deadlock Works
1. Resource Allocation:
o Processes request resources and hold them while waiting for others. For example,
Process A holds Resource 1 and waits for Resource 2, while Process B holds
Resource 2 and waits for Resource 1.
2. Circular Waiting:
o This creates a circular wait condition where neither process can proceed. Since
each is waiting for the other to release a resource, a deadlock occurs.
Here’s why Deadlocks happens:
1. Resource Management:
o In multi-tasking environments, processes need to share resources efficiently.
Deadlocks are an inherent risk when multiple processes compete for limited
resources.
2. System Stability:
o Understanding deadlocks allows operating systems to manage resource allocation
effectively, preventing performance degradation and system crashes.
3. Concurrency Control:
o In systems where multiple processes run simultaneously, recognizing and
handling deadlocks ensures that processes do not block each other indefinitely,
maintaining system responsiveness.
Where Deadlocks commonly happens:
1. Operating Systems:
o Deadlocks are prevalent in operating systems that manage multiple processes and
resources, especially those with shared resources like printers, disk drives, and
network connections.
2. Database Management Systems (DBMS):
o In multi-user environments, deadlocks can occur when transactions wait for
resources held by each other. DBMS implement deadlock detection and recovery
mechanisms to manage this.
3. Distributed Systems:
o In systems where processes run on different machines but share resources,
deadlocks can arise due to network communication delays and resource sharing.
4. Real-Time Systems:
o Deadlocks can occur in real-time systems, where processes need to complete tasks
within specific time constraints and share resources to do so.
System Model in Deadlock (Operating System)
The system model in the context of deadlocks describes how processes interact with resources
and the operating system (OS) manages these interactions. It provides a framework to understand
how deadlocks occur and what conditions must be satisfied for a deadlock to happen.
The system model consists of three key components:
1. Processes
A process is a program in execution that needs resources (e.g., CPU, memory, I/O
devices) to run. Processes may request, hold, or release resources at different points
during their execution.
Processes in a deadlock are usually waiting for resources held by other processes, causing
a circular wait where no process can proceed.
2. Resources
Resources in the system can be classified into two types:
Preemptible Resources: These resources can be taken away from a process without
causing issues (e.g., CPU, memory). Preemption allows the OS to take back resources
from a process if needed.
Non-preemptible Resources: These resources cannot be forcibly taken away from a
process without causing problems (e.g., printers, disk space). Once allocated, they must
be released voluntarily by the process.
3. Resource Allocation
In a system, multiple resources are available for use, and processes can request, hold, or release
resources. The system's resource allocation model explains how these resources are distributed
among processes. The OS manages resource allocation to avoid deadlocks. This model is
represented by:
Request: When a process requires resources, it makes a request. The process will be
blocked if the requested resource is not available.
Allocation: When a resource becomes available, the OS allocates it to the requesting
process, allowing it to proceed.
Release: After the process finishes using a resource, it releases it so that other processes
can use it.
Representation of Resource Allocation in a System Model
The Resource Allocation Graph (RAG) is used to visually represent the relationship between
processes and resources in a system. This graph helps identify potential deadlocks.
Nodes: Represent processes and resources.
o A process node is depicted as a circle.
o A resource node is depicted as a square, with smaller squares inside representing
individual resource instances.
Edges:
o A request edge (from process to resource) indicates that the process has
requested the resource.
o An assignment edge (from resource to process) shows that the resource has been
allocated to the process.
If the graph contains a cycle, it means that a deadlock may exist in the system (for single-
resource instances per resource type, a cycle always indicates a deadlock).
4. Conditions for Deadlock
For a deadlock to occur, the following four Coffman conditions must hold simultaneously in the
system model:
1. Mutual Exclusion: At least one resource must be held by a process in a non-shareable
mode (i.e., only one process can use the resource at a time).
2. Hold and Wait: A process holding at least one resource is waiting to acquire additional
resources that are currently being held by other processes.
3. No Preemption: Resources cannot be forcibly taken away from a process; the process
must release resources voluntarily.
4. Circular Wait: There exists a circular chain of processes, where each process is waiting
for a resource that is being held by the next process in the chain.
Example
Process A requests Resource 1, and the OS allocates it.
Process B requests Resource 2, and the OS allocates it.
Process A now requests Resource 2 (held by Process B), and Process B requests
Resource 1 (held by Process A).
Both processes are now waiting on each other, creating a circular wait that leads to a deadlock.
Deadlock Characterization
Deadlock characterization describes the conditions under which a deadlock can occur in an
operating system. A deadlock is a situation where two or more processes are blocked
indefinitely because each process is waiting for a resource held by another process. The system is
unable to resolve this situation without intervention, leading to a permanent standstill for those
processes.
Four Necessary Conditions for Deadlock (Coffman Conditions)
For a deadlock to occur, four necessary conditions must be met simultaneously. These
conditions are known as the Coffman conditions, and they are:
1. Mutual Exclusion
o At least one resource involved in the deadlock must be held in a non-shareable
mode, meaning that only one process can use the resource at any given time.
o Example: A printer can be used by only one process at a time. If another process
requests the printer while it's in use, it must wait.
2. Hold and Wait
o A process is holding at least one resource and is waiting to acquire additional
resources that are currently being held by other processes.
o Example: Process A holds a file and is waiting for a printer, while Process B
holds the printer and is waiting for the file.
3. No Preemption
o Resources cannot be forcibly removed from processes holding them. The process
must release the resource voluntarily.
o Example: If Process A holds a resource, the OS cannot take it away. It must wait
for Process A to release it.
4. Circular Wait
o A circular chain of processes exists where each process is waiting for a resource
that is held by the next process in the chain.
o Example: Process A is waiting for Resource 1, held by Process B; Process B is
waiting for Resource 2, held by Process C; and Process C is waiting for Resource
1, held by Process A. This forms a cycle.
If all four conditions are true, a deadlock can occur. The system is unable to proceed with the
processes involved in the deadlock until one or more of the conditions are broken.
Resource Allocation Graph (RAG) for Deadlock Characterization
The Resource Allocation Graph (RAG) is a tool used to visually represent and characterize
deadlocks in a system. It consists of the following elements:
Nodes: Represent processes (circles) and resources (rectangles).
Edges:
o A request edge from a process to a resource indicates that the process is
requesting the resource.
o An assignment edge from a resource to a process indicates that the resource has
been allocated to the process.
Example of RAG:
If a graph contains a cycle and there is only one instance of each resource type, a
deadlock has occurred.
If no cycles exist, no deadlock exists.
Handling Deadlock Characterization
Understanding the characterization of deadlock helps the operating system and system designers
to implement strategies to prevent or manage deadlocks. These strategies include:
Deadlock Prevention: Breaking one of the Coffman conditions to prevent deadlocks
from occurring.
Deadlock Avoidance: Dynamically analyzing requests and ensuring the system remains
in a safe state (e.g., Banker's Algorithm).
Deadlock Detection and Recovery: Allowing deadlocks to occur but detecting and
recovering from them once they are detected.
Methods for Handling Deadlocks
Deadlocks in an operating system can severely impact system performance, so it’s essential to
have strategies for handling them. There are four primary methods for dealing with deadlocks:
deadlock prevention, deadlock avoidance, deadlock detection and recovery, and ignoring
the deadlock problem (also called the ostrich algorithm). Each method has its own advantages,
disadvantages, and use cases.
1. Deadlock Prevention
Deadlock prevention aims to eliminate one or more of the four Coffman conditions (necessary
conditions for deadlock: mutual exclusion, hold and wait, no preemption, and circular wait). By
ensuring that one of these conditions cannot occur, deadlocks can be entirely prevented.
Techniques for Deadlock Prevention:
Mutual Exclusion: Some resources must inherently be non-shareable (e.g., printers, tape
drives). However, if a resource can be shared (e.g., read-only files), (PREVENTION-the
system should allow multiple processes to access it simultaneously to avoid mutual
exclusion.)
Hold and Wait: Prevent processes from holding resources while waiting for others. Two
ways to do this:
यह प्रक्रिया सभी आवश्यक Resources को baar-baar nahi baalki - एक ही बार में
मांगने की आवश्यकता रखती है। यह सुनिश्चित करता है कि process- Resources को
तब तक न रोकें जब तक उन्हें नए Resources का इंतजार न हो।
processes को मजबूर किया जाता है कि अगर उन्हें नए Resources की आवश्यकता है,
तो वे पहले से रखे गए Resources को छोड़ दें और बाद में उन्हें फिर से
प्राप्त करें।
o Require at once. This ensures that processes don’t hold resources while waiting.
o Force processes to release held resources if they need new ones and reacquire
them later.
No Preemption: If a process is holding some resources and requests another one that
cannot be immediately allocated, preempt (take away) the resources the process is
holding. The process can then be restarted when all of its requested resources are
available.
अगर कोई प्रक्रिया कुछ resources इस्तेमाल कर रही है और एक और resource
मांगती है जो तुरंत नहीं दिया जा सकता, तो उसके पास जो resource हैं, उन्हें
वापस ले लिया जाए। फिर जब उसके सभी मांगे गए संसाधन मिल जाएं, तब उस
प्रक्रिया को फिर से शुरू किया जा सकता है।
Circular Wait: Impose a total ordering of resource types. Require processes to request
resources in an increasing order of resource numbers. This eliminates circular waiting by
ensuring that no process can wait for a resource held by a lower-priority process.
संसाधन प्रकारों की एक कुल व्यवस्था (total ordering) लागू करें। प्रक्रियाओं
को संसाधनों को एक बढ़ते हुए क्रम में मांगने की शर्त रखें। यह सर्कुलर
वेटिंग को खत्म कर देता है, क्योंकि इससे यह सुनिश्चित होता है कि कोई भी
प्रक्रिया कम-प्राथमिकता वाली प्रक्रिया द्वारा धारण किए गए संसाधन के लिए
इंतजार नहीं कर सकती।
Pros:
Prevents deadlocks entirely.
Cons:
Decreases system efficiency (e.g., processes must
request all resources upfront or repeatedly release and
reacquire resources)
.
2. Deadlock Avoidance
Deadlock avoidance requires the operating system to make decisions dynamically about resource
allocation based on the current state of the system. It analyzes whether granting a particular
resource request will lead the system into an unsafe state (where a deadlock might occur). The
Banker's Algorithm is a popular deadlock avoidance technique.
Techniques for Deadlock Avoidance:
Banker’s Algorithm: Named after the way a banker evaluates loans, this algorithm
checks whether the system has enough resources to safely allocate them to processes
without entering a deadlock.
o Safe State: The system is in a safe state if there is a sequence of processes such
that every process can finish and release its resources, ensuring that all other
processes can eventually get the resources they need.
o Unsafe State: If no such sequence exists, the system is in an unsafe state and a
deadlock could potentially occur.
Pros:
It allows more resource requests to be granted than deadlock prevention.
Avoids deadlocks by only granting resource requests if the system remains in a safe state.
Cons:
Requires information about future resource requests (which may be difficult to predict).
Adds overhead to track resource states and check for safe states.
3. Deadlock Detection and Recovery
In this method, the system allows deadlocks to occur but has mechanisms to detect deadlocks
and recover from them. This method is useful in environments where deadlocks are infrequent,
and it is more practical to detect and fix them when they occur, rather than prevent them upfront.
Techniques for Deadlock Detection:
Resource Allocation Graph (RAG): The system maintains a graph that tracks which
processes are holding and requesting which resources. If a cycle is found in the graph, it
indicates a deadlock.
Wait-For Graph: A simplified version of the RAG that only tracks processes waiting for
resources held by other processes. Detecting a cycle in this graph signifies a deadlock.
Recovery Methods:
Terminate processes:
o Abort all deadlocked processes: Simple but extreme solution, leads to wasted
work.
o Abort one process at a time until the deadlock is resolved: Choose processes to
terminate based on criteria such as priority, execution time, or resources held.
Resource preemption:
o Preempt resources: Take resources away from one process and assign them to
another. The system should select processes and resources to preempt based on
factors such as process priority, execution time, and resource importance.
Pros:
Allows maximum flexibility in resource allocation since the system doesn’t prevent
deadlocks upfront.
Deadlock detection algorithms can be run periodically to check for deadlocks.
Cons:
May lead to performance degradation as detecting deadlocks and recovering from them
consumes system resources.
Requires careful handling of recovery to avoid further complications.
4. Ignoring the Problem (Ostrich Algorithm)
Some operating systems choose to ignore the possibility of deadlocks entirely. This method is
often used in environments where deadlocks are very rare, and the overhead of preventing or
detecting them is more costly than the deadlocks themselves.
Pros:
Minimal system overhead, as no additional code or algorithms are needed to handle
deadlocks.
Cons:
Deadlocks may occur, leading to processes getting stuck indefinitely without a resolution.
System administrators might need to manually intervene to fix deadlocks.
Summary of Methods for Handling Deadlocks
Method Strategy Pros Cons
Deadlock Ensure that at least one Prevents deadlocks Limits system efficiency
Prevention of the Coffman completely. and resource utilization.
conditions cannot hold.
Deadlock Dynamically analyze Allows for more Requires detailed
Avoidance resource allocation flexibility thanknowledge of resource
requests to avoid unsafe prevention, avoidsneeds, can add significant
states. deadlocks. overhead.
Deadlock Allow deadlocks to Can fully utilize Performance impact from
Detection and occur, then detect and resources without detection and recovery;
Recovery resolve them. upfront restrictions. may require process
termination.
Ignoring Do nothing, assuming Minimal overhead Deadlocks may occur,
Deadlocks deadlocks are rare. and complexity. requiring manual
intervention.
In practice, operating systems choose one or a combination of these strategies depending on the
nature of the system and the likelihood of deadlocks occurring.