A deadlock is a state in a system where two or more processes are unable to proceed because each is waiting for a resource held by another. This results in a permanent blocking of the involved processes.
Four Necessary Conditions for Deadlock
Mutual Exclusion: At least one resource must be non-sharable (used by one process at a time).
Hold and Wait: Processes hold resources while waiting for others.
No Preemption: Resources cannot be forcibly taken from a process.
Circular Wait: A cycle of processes exists where each waits for a resource held by the next.
Example of Deadlock
Process P1 holds Resource R1 and requests Resource R2.
Process P2 holds Resource R2 and requests Resource R1.
Neither can proceed, resulting in a deadlock.
2. Resource Allocation Graphs (RAGs)
Purpose of RAGs
A RAG is a directed graph used to model resource allocation states and detect deadlocks. It visualizes:
Processes (circles)
Resources (rectangles with dots for instances)
Edges:
Request Edge: Process → Resource (P1 → R2: P1 is waiting for R2).
Assignment Edge: Resource → Process (R1 → P1: R1 is allocated to P1).
RAG Components
Component
Symbol
Description
Process
○ (e.g., P1)
A running entity.
Resource
□ (e.g., R1‧‧‧)
Rectangles with dots for instances.
Request Edge
→ (P1 → R2)
Process is waiting for a resource.
Assignment Edge
→ (R1 → P1)
Resource is allocated to a process.
Deadlock Detection in RAGs
Single-Instance Resources: A cycle in the RAG = Deadlock.
Multi-Instance Resources: A cycle does not always imply deadlock (depends on availability).
Example 1: Deadlock (Cycle in RAG)
Processes: P1, P2Resources: R1 (1 instance), R2 (1 instance)Edges:- P1 → R1 (assignment: R1 is held by P1)- P1 → R2 (request: P1 is waiting for R2)- P2 → R2 (assignment: R2 is held by P2)- P2 → R1 (request: P2 is waiting for R1)Cycle: P1 → R1 → P2 → R2 → P1 → ...