Module 3
Module 3
Deadlock
Definition:
• Deadlock is a situation in a multiprogramming or multi-threading
environment where a set of processes or threads become stuck in a
state of indefinite waiting because each is waiting for a resource that
another process holds. As a result, none of the processes can
proceed, leading to a standstill.
Conditions for Deadlock: For a deadlock to occur, the following four
conditions must be present simultaneously (known as the Coffman
conditions):
1. Mutual Exclusion:
•Definition: At least one resource must be held in a non-
shareable mode. That is, only one process can use the
resource at any given time.
• Example: A printer can only be used by one process at a time.
2. Hold and Wait:
• Definition: A process holding at least one resource is waiting to
acquire additional resources currently held by other processes.
• Example: A process that has a printer but is waiting for access
to a scanner held by another process.
3. No Preemption:
• Definition: Resources cannot be forcibly taken from a process
holding them; they must be released voluntarily.
• Example: A process cannot be preempted from holding a
resource like memory until it finishes using it.
4. Circular Wait:
• Definition: There exists a set of processes such that each
process is waiting for a resource held by the next process in the
set, forming a circular chain.
• Example: Process A waits for a resource held by Process B,
Process B waits for a resource held by Process C, and Process
C waits for a resource held by Process A.
Detection of Deadlock:
• Deadlock Detection Algorithm:
• Definition: Algorithms are used to detect the presence of
deadlock in a system. One common method is to use a
Resource Allocation Graph where nodes represent processes
and resources, and edges represent requests and allocations.
• Technique: A cycle in the resource allocation graph indicates
the presence of a deadlock.
Prevention of Deadlock:
• Eliminate One Condition: Prevent deadlock by ensuring that at
least one of the Coffman conditions cannot occur.
• Mutual Exclusion: Make resources shareable if possible,
though this might not always be feasible.
• Hold and Wait: Require processes to request all resources at
once, which might increase resource consumption but prevents
hold and wait.
• No Preemption: Allow resources to be forcibly taken from
processes, which might involve rolling back processes to a safe
state.
• Circular Wait: Impose an ordering on resource requests and
require processes to request resources in a specific sequence.
Avoidance of Deadlock:
• Banker’s Algorithm:
• Definition: A resource allocation algorithm that determines
whether resource requests can be safely granted without
leading to a deadlock.
• Technique: It uses information about available resources,
maximum claims, and current allocations to ensure that
resource allocation will not leave the system in an unsafe state.
Recovery from Deadlock:
• Process Termination: Kill one or more processes involved in the
deadlock to break the cycle.
• Technique: Choose processes to terminate based on criteria
such as priority or the amount of work lost.
• Resource Preemption: Take resources away from some processes
and allocate them to others to resolve the deadlock.
• Technique: Rollback processes to a safe state and reallocate
resources.
Applications:
• Database Systems: Managing concurrent transactions and
ensuring that database locks do not lead to deadlock.
• Operating Systems: Handling resource allocation and process
scheduling to avoid deadlock situations.
Challenges:
• Complexity: Implementing deadlock prevention, avoidance, or
recovery mechanisms can be complex and may impact system
performance.
• Overhead: The overhead associated with deadlock detection and
resolution can affect system efficiency and resource utilization.
Comparison:
• Detection vs. Prevention: Detection involves identifying deadlocks
after they occur and taking corrective actions, while prevention aims
to avoid the conditions that lead to deadlock in the first place.
• Avoidance vs. Recovery: Avoidance focuses on dynamically
managing resources to ensure safe allocation, while recovery deals
with handling deadlocks after they have occurred.