What is deadlock in OS?

A deadlock in an operating system is a situation where two or more processes are unable to proceed because each is waiting for a resource held by another process. It’s like a traffic jam at an intersection where no car can move because each is blocking the other.

Real-World Example

Imagine two friends at a restaurant who both want to borrow each other’s fork and knife to eat, but neither lets go of what they’re holding. Both stay stuck, unable to eat or move forward.

Necessary Conditions for Deadlock

A deadlock occurs when these four conditions hold simultaneously:

  1. Mutual Exclusion: At least one resource cannot be shared and can only be used by one process at a time.
  2. Hold and Wait: A process holding at least one resource is waiting to acquire 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 circular chain of processes exists, where each process waits for a resource held by the next process in the chain.

How Deadlock Happens

  1. Process A locks Resource X and requests Resource Y.
  2. Process B locks Resource Y and requests Resource X.
  3. Neither process can proceed, causing a deadlock.

Handling Deadlocks

1. Deadlock Prevention

  • Break one of the four necessary conditions.
    • For example, avoid circular wait by assigning a specific order for resource acquisition.

2. Deadlock Avoidance

  • Use algorithms like the Banker's Algorithm to ensure the system can allocate resources without entering a deadlock state.

3. Deadlock Detection

  • Allow deadlocks to occur but periodically check for them using a detection algorithm.
  • Once detected, resolve them by terminating processes or forcibly preempting resources.

4. Deadlock Recovery

  • Recover from a detected deadlock by:
    • Killing one or more processes.
    • Rolling back processes to a safe state.

Why Understanding Deadlocks Matters

Deadlocks can cause system freezes, unresponsive applications, or degraded performance. Effective deadlock management is crucial for building reliable and efficient systems.

For more insights into advanced OS concepts like deadlocks, explore Grokking Multithreading and Concurrency for Coding Interviews or System Design Primer The Ultimate Guide.

TAGS
Coding Interview
System Design Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What does Palantir do?
Why do I want to work for Spotify?
What is Service Mesh?
What are the three most essential skills of a system designer?
Why is LeetCode so difficult?
What are the tips for pair programming exercises in interviews?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.8
(1,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.