Deadlocks in Computing

A deadlock is a situation in which two or more processes become stuck, each waiting for resources held by the other, preventing further progress. Without external intervention, these processes remain indefinitely blocked.


1. Conditions for Deadlock

A deadlock occurs when the following four conditions hold simultaneously (Coffman’s Conditions):


1. Mutual Exclusion – Some resources can only be used by one process at a time.

2. Hold and Wait – A process holding a resource is waiting for additional resources held by other processes.

3. No Preemption – Resources cannot be forcibly taken from a process; they must be released voluntarily.

4. Circular Wait – A set of processes is waiting in a circular chain, where each process holds a resource needed by the next process in the cycle.


2. Preventing Deadlocks

To prevent deadlocks, at least one of the four conditions must be eliminated:


- Avoid Mutual Exclusion: Where possible, allow multiple processes to share resources (e.g., read-only files).

- Eliminate Hold and Wait: Require processes to request all needed resources at once before execution begins.

- Enable Preemption: Allow resources to be forcibly taken from processes when necessary.

- Break Circular Wait: Impose a strict ordering on resource allocation, preventing circular dependencies.


3. Deadlock Avoidance

Rather than outright prevention, deadlock avoidance ensures that resource allocation does not lead to a deadlock.


- Banker’s Algorithm: A process is allowed to proceed only if granting its requested resources leaves the system in a "safe state" (i.e., a state where all processes can eventually complete).

- Resource Allocation Graph (RAG): Tracks resource dependencies and prevents circular waits by blocking unsafe allocations.


4. Detecting and Resolving Deadlocks

When prevention and avoidance are not feasible, the system must detect and recover from deadlocks.


- Detection Techniques:


Periodically examine resource allocation graphs to identify circular waits.

Check available and allocated resources to determine if any processes are indefinitely waiting.

- Recovery Strategies:


Terminate Processes: Kill one or more processes to break the deadlock.

Preempt Resources: Temporarily take resources from certain processes and reallocate them to allow execution to continue.


Comments

Popular posts from this blog

Absolute and relative path in HTML pages

Errors

goto PHP operator