1. Definition
A deadlock is a situation where a set of processes are permanently blocked, each waiting for a resource held by another process in the set.
2. Necessary Conditions (Coffman Conditions - Very Important)
All four must hold simultaneously for deadlock:
- Mutual Exclusion: At least one resource must be non-sharable
- Hold and Wait: Process holds ≥1 resource and waits for more
- No Preemption: Resources cannot be forcibly taken - only voluntarily released
- Circular Wait: P1 waits for P2, P2 waits for P3, ..., Pn waits for P1
3. Resource Allocation Graph (RAG)
- Request edge: Process → Resource (process wants resource)
- Assignment edge: Resource → Process (resource assigned to process)
- Single instance: Cycle in RAG → deadlock
- Multiple instances: Cycle in RAG → may or may not be deadlock
4. Deadlock Handling Strategies
Prevention: Ensure at least one Coffman condition never holds
- Eliminate circular wait: impose ordering on resource types, request in order
- Eliminate hold & wait: request all resources at once before starting
- Allow preemption: if resource not available, preempt from holder
Avoidance: Dynamically check if allocation leads to unsafe state
- Requires advance knowledge of maximum resource needs
- Banker's Algorithm (Very Important)
Detection & Recovery:
- Allow deadlock to occur, detect using RAG or detection algorithm, then recover
- Recovery: process termination (abort all/one at a time) or resource preemption
Ignorance (Ostrich Algorithm): Ignore deadlock - used by most OS (Linux, Windows) for rare deadlocks
5. Banker's Algorithm (Very Important)
Data structures for n processes, m resource types:
- Available[m]: available instances of each resource
- Max[n][m]: maximum demand of each process
- Allocation[n][m]: currently allocated
- Need[n][m] = Max − Allocation
Safety Algorithm:
- Work = Available; Finish[i] = false for all i
- Find i where Finish[i]=false AND Need[i] ≤ Work
- Work = Work + Allocation[i]; Finish[i] = true
- Repeat until no such i found
- If all Finish[i] = true → Safe state
Resource Request Algorithm:
- If Request[i] ≤ Need[i]: proceed, else error
- If Request[i] ≤ Available: proceed, else wait
- Tentatively allocate, run safety algorithm
- If safe → allocate; else → rollback and wait
6. GATE Trick
Banker's algorithm: always compute Need = Max − Allocation first. Safe sequence may not be unique - any valid sequence counts. For deadlock detection with multiple instances: use same algorithm as Banker's but with Allocation replacing Max. Cycle in single-instance RAG = deadlock (no further check needed).