Deadlocks
A deadlock is a situation in which a set of processes are blocked because each process is holding
a resource and waiting for another resource acquired by some other process in the set.
1. System Model
A system consists of a finite set of resources distributed among processes. A resource may have
multiple instances and can be classified into:
 Preemptible Resources: Can be taken away from a process without side effects (e.g.,
CPU).
 Non-Preemptible Resources: Cannot be taken away until the process releases them
(e.g., printers, database locks).
Each process follows this sequence:
1. Request: A process requests a resource.
2. Use: The process uses the resource.
3. Release: The process releases the resource after use.
2. Deadlock Characterization
A deadlock occurs if the following four conditions hold simultaneously (Coffman’s Conditions):
1. Mutual Exclusion: At least one resource must be held in a non-shareable mode.
2. Hold and Wait: A process holding at least one resource is waiting to acquire additional
resources held by other processes.
3. No Preemption: A resource cannot be forcibly taken from a process; it must be released
voluntarily.
4. Circular Wait: A circular chain of processes exists where each process holds at least one
resource needed by the next process in the cycle.
3. Methods for Handling Deadlock
There are three general approaches:
1. Ignore the problem (Most common in operating systems like Windows and Linux).
2. Ensure the system never enters a deadlock state (Deadlock Prevention or Avoidance).
3. Allow deadlocks to occur, detect, and then recover (Deadlock Detection and
Recovery).
4. Deadlock Prevention
Deadlock prevention ensures that at least one of the four necessary conditions never holds:
1. Mutual Exclusion: Not possible for non-shareable resources, but for some resources,
virtualization can allow sharing.
2. Hold and Wait: A process must request all resources at once (increasing resource
contention).
3. No Preemption: If a process is holding a resource and requests another one that is not
available, it must release all held resources.
4. Circular Wait: Impose a total ordering of all resource types and require each process to
request resources in increasing order.
5. Deadlock Avoidance
Deadlock avoidance requires the system to dynamically decide whether a resource request
should be granted. This is done using algorithms like:
1. Resource Allocation Graph (RAG) Algorithm
o If a graph with request and allocation edges forms a cycle, deadlock might occur.
o Adding a claim edge (showing future requests) helps in avoidance.
2. Banker's Algorithm (Dijkstra's Algorithm)
o Used for multiple instances of resources.
o Ensures that after allocation, the system remains in a safe state (a state where at
least one sequence of execution is possible without leading to deadlock).
6. Deadlock Detection
If deadlock prevention or avoidance is not used, the system must detect deadlocks.
1. Single Instance of Each Resource Type
o A wait-for graph is constructed by removing resource nodes from the Resource
Allocation Graph.
o If a cycle is detected, deadlock exists.
2. Multiple Instances of Resources
o A modified Banker’s algorithm checks for circular wait conditions.
7. Recovery from Deadlock
Once a deadlock is detected, the system must recover using one of the following methods:
1. Process Termination
o Abort all deadlocked processes.
o Abort one process at a time until deadlock is resolved.
2. Resource Preemption
o Select a victim process, roll it back, and preempt its resources.
o Requires careful selection to avoid starvation.

Deadlocks with example in Operating System.docx

  • 1.
    Deadlocks A deadlock isa situation in which a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process in the set. 1. System Model A system consists of a finite set of resources distributed among processes. A resource may have multiple instances and can be classified into:  Preemptible Resources: Can be taken away from a process without side effects (e.g., CPU).  Non-Preemptible Resources: Cannot be taken away until the process releases them (e.g., printers, database locks). Each process follows this sequence: 1. Request: A process requests a resource. 2. Use: The process uses the resource. 3. Release: The process releases the resource after use. 2. Deadlock Characterization A deadlock occurs if the following four conditions hold simultaneously (Coffman’s Conditions): 1. Mutual Exclusion: At least one resource must be held in a non-shareable mode. 2. Hold and Wait: A process holding at least one resource is waiting to acquire additional resources held by other processes.
  • 2.
    3. No Preemption:A resource cannot be forcibly taken from a process; it must be released voluntarily. 4. Circular Wait: A circular chain of processes exists where each process holds at least one resource needed by the next process in the cycle. 3. Methods for Handling Deadlock There are three general approaches: 1. Ignore the problem (Most common in operating systems like Windows and Linux). 2. Ensure the system never enters a deadlock state (Deadlock Prevention or Avoidance). 3. Allow deadlocks to occur, detect, and then recover (Deadlock Detection and Recovery). 4. Deadlock Prevention Deadlock prevention ensures that at least one of the four necessary conditions never holds: 1. Mutual Exclusion: Not possible for non-shareable resources, but for some resources, virtualization can allow sharing. 2. Hold and Wait: A process must request all resources at once (increasing resource contention). 3. No Preemption: If a process is holding a resource and requests another one that is not available, it must release all held resources. 4. Circular Wait: Impose a total ordering of all resource types and require each process to request resources in increasing order.
  • 3.
    5. Deadlock Avoidance Deadlockavoidance requires the system to dynamically decide whether a resource request should be granted. This is done using algorithms like: 1. Resource Allocation Graph (RAG) Algorithm o If a graph with request and allocation edges forms a cycle, deadlock might occur. o Adding a claim edge (showing future requests) helps in avoidance. 2. Banker's Algorithm (Dijkstra's Algorithm) o Used for multiple instances of resources. o Ensures that after allocation, the system remains in a safe state (a state where at least one sequence of execution is possible without leading to deadlock). 6. Deadlock Detection If deadlock prevention or avoidance is not used, the system must detect deadlocks. 1. Single Instance of Each Resource Type o A wait-for graph is constructed by removing resource nodes from the Resource Allocation Graph. o If a cycle is detected, deadlock exists. 2. Multiple Instances of Resources o A modified Banker’s algorithm checks for circular wait conditions. 7. Recovery from Deadlock Once a deadlock is detected, the system must recover using one of the following methods: 1. Process Termination
  • 4.
    o Abort alldeadlocked processes. o Abort one process at a time until deadlock is resolved. 2. Resource Preemption o Select a victim process, roll it back, and preempt its resources. o Requires careful selection to avoid starvation.