0% found this document useful (0 votes)
137 views3 pages

Deadlock

A deadlock is a situation in computing where two or more processes cannot proceed because each is waiting for the other to release a resource, leading to an indefinite waiting state. Deadlocks occur under four conditions: mutual exclusion, hold and wait, no preemption, and circular wait. Techniques to handle deadlocks include prevention, avoidance, and detection, which help ensure smooth system operation.

Uploaded by

Hassan ali Sayed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views3 pages

Deadlock

A deadlock is a situation in computing where two or more processes cannot proceed because each is waiting for the other to release a resource, leading to an indefinite waiting state. Deadlocks occur under four conditions: mutual exclusion, hold and wait, no preemption, and circular wait. Techniques to handle deadlocks include prevention, avoidance, and detection, which help ensure smooth system operation.

Uploaded by

Hassan ali Sayed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Deadlock:

Definition
A deadlock is a situation in computing where two or more processes are unable to proceed
because each is waiting for the other to release a resource. This results in an indefinite
waiting state, preventing the system from progressing. Deadlocks commonly occur in multi-
threaded and distributed systems where multiple processes or threads need access to
shared resources.

Conditions for Deadlock


A deadlock can occur if the following four necessary conditions, known as Coffman’s
Conditions, hold simultaneously:
1. Mutual Exclusion:
o A resource is held by one process at a time and cannot be used by another
process until it is released.
o Example: A printer is assigned to one process, preventing another process from
using it until it is freed.
2. Hold and Wait:
o A process holding at least one resource is waiting for additional resources held
by other processes.
o Example: Process A is holding a printer and waiting for a scanner, while Process
B is holding a scanner and waiting for a printer.
3. No Preemption:
o A resource cannot be forcibly taken from a process. It must be released
voluntarily.
o Example: If a process acquires a lock on a database table, the system cannot
revoke the lock—it must wait for the process to release it.
4. Circular Wait:
o A set of processes exist where each process is waiting for a resource held by
the next process in the chain.
o Example:
 Process A → needs a resource held by Process B
 Process B → needs a resource held by Process C
 Process C → needs a resource held by Process A
Examples of Deadlock
1. Deadlock in Operating Systems
Imagine two processes (P1 and P2) that require two resources (R1 and R2):
 P1 acquires R1 and then requests R2, which is held by P2.
 P2, in turn, requests R1, which is held by P1.
 Neither process can proceed, resulting in a deadlock.
2. Deadlock in Database Management Systems (DBMS)
Consider two transactions:
 Transaction T1 locks Table A and requests Table B, which is locked by T2.
 Transaction T2 locks Table B and requests Table A, causing deadlock.
3. Deadlock in Multithreading
In Java, two threads can be blocked indefinitely due to synchronized methods:
class DeadlockExample {
static final Object resource1 = new Object();
static final Object resource2 = new Object();
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
synchronized (resource1) {
System.out.println("Thread 1: Locked resource 1");
try { Thread.sleep(100); } catch (Exception e) {}
synchronized (resource2) {
System.out.println("Thread 1: Locked resource 2");
}
}
});
Thread t2 = new Thread(() -> {
synchronized (resource2) {
System.out.println("Thread 2: Locked resource 2");
try { Thread.sleep(100); } catch (Exception e) {}
synchronized (resource1) {
System.out.println("Thread 2: Locked resource 1");
}
}
});
t1.start();
t2.start();
}
}
Here, both threads acquire one lock and wait indefinitely for the other, causing a deadlock.
Deadlock Handling Techniques
1. Deadlock Prevention (Eliminating at least one Coffman condition)
o Break mutual exclusion: Use resources in a shared manner if possible.
o Avoid hold and wait: Require processes to request all needed resources at
once.
o Allow preemption: Permit resource preemption to avoid indefinite waiting.
o Avoid circular wait: Impose an ordering on resource requests.
2. Deadlock Avoidance
o Use algorithms like Banker's Algorithm to determine if granting resources
would lead to deadlock.
3. Deadlock Detection and Recovery
o Detect cycles in a wait-for graph.
o If deadlock is detected, terminate one or more processes or forcibly reclaim
resources.

Deadlocks occur when processes compete for shared resources without a proper handling
mechanism. Understanding Coffman’s conditions helps in designing systems that either
prevent or recover from deadlocks. Various strategies, such as deadlock prevention,
avoidance, and detection, are used to ensure smooth system operation.

You might also like