Intermediate Topic 23 of 51 Operating Systems
Deadlock
Cycle of tasks each waiting for resources held by another.
How it works
Deadlock is a state in which a set of processes are each waiting for a resource held by another in the set, so none can proceed. Four conditions must hold together: mutual exclusion, hold and wait, no preemption and circular wait. Resource-allocation graphs help detect cycles. Distributed systems and databases can deadlock across machines too.
Step-by-step walkthrough
- Thread 1 holds lock A.
- Thread 2 holds lock B.
- Thread 1 now requests B.
- Thread 2 requests A.
- Each waits forever for the other, a circular wait.
Where it is used
Understanding deadlock matters in operating systems, multithreaded software, databases with transactions and any system using locks. Databases detect deadlocks and abort a victim transaction.
Common pitfall
Typical causes are inconsistent lock ordering, holding a lock while calling unknown code and waiting on resources inside locks. Deadlocks make a program hang silently and are hard to reproduce. Use consistent lock order, timeouts, try-lock and avoid nested locks; thread dumps reveal the cycle. Distinguish deadlock from livelock and starvation.
Where it fits in the course
Deadlock is topic 23 of 51 in Operating Systems, rated Intermediate. It follows Monitors. The next topic is Deadlock Prevention and Avoidance.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Operating Systems quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Operating Systems quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Deadlock?
Where do people use Deadlock?
What is the most common mistake with Deadlock?
What should I learn before this topic?
How can I practice it?
Is Deadlock hard to learn?
How long does it take to learn Deadlock?
Which topics are related to Deadlock?
What should I study after Deadlock?
Related topics
- Monitors (Operating Systems)
- Deadlock Prevention and Avoidance (Operating Systems)
- Semaphores (Operating Systems)
- Dining Philosophers (Operating Systems)