A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Deadlock: Conditions and Modeling

College Depth 105 in the knowledge graph ☐ I know this ☆ Set as goal
5topics build on this
412prerequisites beneath it
See this on the map →
Mutual Exclusion and LocksSemaphores→→Deadlock Conditions and Resource GraphsDeadlocks in Databases
deadlock Coffman-conditions resource-allocation-graph circular-wait

Core Idea

A deadlock is a state where a set of processes are each waiting for a resource held by another process in the set, and none can proceed. Coffman et al. identified four necessary conditions that must all hold simultaneously for deadlock to occur: mutual exclusion (resources are non-shareable), hold and wait (a process holds resources while waiting for more), no preemption (resources cannot be forcibly taken), and circular wait (a circular chain of processes exists, each waiting for the next). Resource-Allocation Graphs (RAGs) are the standard formal tool for detecting deadlocks: a cycle in the RAG is a necessary condition for deadlock, and sufficient when each resource type has exactly one instance.

How It's Best Learned

Draw the dining philosophers problem as a resource-allocation graph with five philosophers and five forks. Show how a cycle forms if all five pick up their left fork simultaneously.

Common Misconceptions

Explainer

From your work on mutexes and locks, you know that threads must acquire exclusive access to shared resources to avoid race conditions. Deadlock is the dark side of that protection: when multiple threads each hold a resource and wait for another resource held by a different thread, the system can reach a state where nobody can make progress. Imagine two people in a narrow hallway, each refusing to step aside — neither can pass, and both wait forever.

The four Coffman conditions provide a precise framework for understanding when deadlocks can occur. All four must hold simultaneously. Mutual exclusion means at least one resource is non-shareable — only one thread can use it at a time (this is the nature of a mutex). Hold and wait means a thread holds at least one resource while waiting to acquire another. No preemption means resources cannot be forcibly taken from a thread — the thread must release them voluntarily. Circular wait means there is a cycle of threads, each waiting for a resource held by the next thread in the chain. The classic illustration is the dining philosophers problem: five philosophers sit at a round table, each needing two forks to eat. If all five pick up their left fork simultaneously, each holds one fork and waits for the right fork held by their neighbor — all four conditions hold, and nobody eats.

The Resource-Allocation Graph (RAG) is the formal tool for modeling and detecting deadlocks. It is a directed graph with two types of nodes: processes (circles) and resource types (rectangles with dots representing instances). A request edge goes from a process to a resource it is waiting for. An assignment edge goes from a resource instance to the process holding it. If you draw the RAG for a system and find a cycle, that indicates a potential deadlock. For single-instance resource types (one printer, one database lock), a cycle is both necessary and sufficient for deadlock — the cycle *is* the deadlock. For multi-instance resource types (three identical printers), a cycle is necessary but not sufficient: the cycle might resolve if one thread finishes and releases an instance, allowing another thread in the cycle to proceed.

The practical power of the Coffman conditions is that eliminating any one of the four prevents deadlock entirely. Most real systems attack circular wait by imposing a total ordering on resources: all threads must acquire resources in the same global order (e.g., always lock A before lock B). If every thread follows this order, a circular chain cannot form. Other strategies include disallowing hold-and-wait (a thread must request all resources upfront), allowing preemption (if a thread cannot get what it needs, it releases what it holds), or avoiding mutual exclusion where possible (using read-write locks instead of exclusive locks). Each strategy has tradeoffs in complexity and performance, but the key insight is structural: deadlock is not random bad luck — it requires all four conditions, and you can engineer systems to break at least one.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Binary Counters: Design and Analysis → Binary Arithmetic → Fixed-Point Number Representation → Two's Complement Representation → Overflow and Underflow Detection → Binary Adders: Half-Adders and Full-Adders → Full Adder and Carry Propagation → Carry Lookahead Adder Design → Half Adder Circuit Design → Multiplication Circuit Design → Sequential Circuit Design → Registers and Register Files → Instruction Set Architecture (ISA) → Kernel Architecture and OS Structure → System Calls and User/Kernel Mode → Processes and the Process Control Block → Process Creation: fork() and exec() → Process Termination and Resource Cleanup → Process States and State Transitions → Process Model Formalization → Context Switching and CPU Dispatch → CPU Scheduling Fundamentals → Round-Robin (RR) Scheduling → First-Come-First-Served (FCFS) Scheduling → Scheduling Fairness and Starvation Prevention → Thread Scheduling and Coordination → Semaphores → Deadlock: Conditions and Modeling

Longest path: 106 steps · 412 total prerequisite topics

Prerequisites (2)

Leads To (2)