LearnViz CompSci
Home / Learn / Operating Systems / Deadlock

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

  1. Thread 1 holds lock A.
  2. Thread 2 holds lock B.
  3. Thread 1 now requests B.
  4. Thread 2 requests A.
  5. 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

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Operating Systems quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Deadlock?
Cycle of tasks each waiting for resources held by another. It belongs to Operating Systems and is an intermediate-level topic.
Where do people use Deadlock?
Understanding deadlock matters in operating systems, multithreaded software, databases with transactions and any system using locks.
What is the most common mistake with Deadlock?
Typical causes are inconsistent lock ordering, holding a lock while calling unknown code and waiting on resources inside locks.
What should I learn before this topic?
Start with Monitors, the topic just before it in Operating Systems.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Operating Systems quiz.
Is Deadlock hard to learn?
It is an Intermediate topic. Be comfortable with the Beginner topics in this area first, then read the common pitfall carefully, since it is where most mistakes happen.
How long does it take to learn Deadlock?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Deadlock?
Monitors, Deadlock Prevention and Avoidance, Semaphores are closely connected. The full list is in the related topics section of this page.
What should I study after Deadlock?
Continue with Deadlock Prevention and Avoidance, the next topic in Operating Systems.

Related topics