Skip to the page

Operating systems · Concept

Deadlock and its four conditions

The interviewer asks

What is a deadlock? Give me a real-life example, then tell me the four conditions that must all be true for it to happen.

Then

Follow-ups the interviewer asks next

  1. Which of the four conditions is easiest to break in a real program, and how?
  2. What's the difference between deadlock prevention, avoidance and detection?
  3. What does the Banker's algorithm do, in one or two sentences?
  4. What do Windows and Linux actually do about deadlocks?
  5. How is deadlock different from starvation?

The answer

What a strong answer covers

  • Deadlock: a set of processes each waiting for a resource held by another in the set, so none can ever continue.
  • Example: two friends, one pen and one notebook. A holds the pen and waits for the notebook; B holds the notebook and waits for the pen.
  • The four Coffman conditions: mutual exclusion (a resource can be used by one at a time), hold and wait (holding one while waiting for another), no preemption (you can't take a resource away), circular wait (a cycle of waiting).
  • Prevention: break one condition. The usual one in code is circular wait: always take locks in the same global order. Others: ask for everything at once, or let a request time out and release what you hold.
  • Avoidance: check each request and grant it only if the system stays in a safe state (the Banker's algorithm). It needs to know maximum needs in advance, so it's rare outside textbooks.
  • Detection and recovery: find cycles in the wait-for graph, then kill or roll back a process. Databases do this: they detect a deadlock and abort one transaction.
  • General-purpose OSs mostly ignore deadlocks in user programs (the "ostrich approach") and leave it to programmers.
  • Starvation: a process waits a very long time because others keep getting picked; it isn't stuck in a cycle.

The red pen

Common traps

Avoid:listing only two or three conditions; "just restart the system" as the only answer; mixing up deadlock with starvation or with a slow program.

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview