15.2 KB · commit 40bb182
| title | T11.04 · Deadlock Handling |
|---|
← Previous: T11.03 Strict 2PL, Rigorous 2PL, and Drawbacks of 2PL | 🏠 Index | Next: T11.05 Multiple Granularity Locking Protocol →
Topic: T11 · Concurrency Control Protocols | Priority: 🟠 | Exam Frequency: 4/7 finals (17 marks) Sources: Silberschatz 7e Ch. 18 (§18.2, pp. 849–853) · Lecture Deck 08 (Slides 20–25) · Raidah Notes (pp. 28–29)
Deadlock: A state where two or more transactions are waiting indefinitely for locks held by each other in a circular chain. None of the transactions can continue without external intervention.
A deadlock can occur if and only if four conditions hold simultaneously:
Deadlock prevention works by breaking at least one of these four conditions.
Deadlock Prevention: A set of locking rules that ensures the system never enters a deadlock state. Four standard schemes exist (Lecture Deck 08, Slides 22–23):
Wait-Die and Wound-Wait)| Scheme | When Ti requests item held by Tj | Type |
|---|---|---|
| Wait-Die | If Ti is older (TS(Ti)<TS(Tj)), Ti waits. If Ti is younger (TS(Ti)>TS(Tj)), Ti dies (rolls back). | Non-preemptive |
| Wound-Wait | If Ti is older (TS(Ti)<TS(Tj)), Ti wounds Tj (Tj rolls back). If Ti is younger, Ti waits. | Preemptive |
When prevention is not used, the DBMS allows locks freely and monitors dependencies using a wait-for graph.
Wait-For Graph: A directed graph G=(V,E) where vertices V represent active transactions, and directed edges E represent lock wait dependencies:
Cycle Detection Theorem: The database system is in a deadlock state if and only if the wait-for graph contains a directed cycle.

Figure: Wait-for graph with a cycle indicating deadlock (Source: Course Archive)
Once a cycle is detected, the DBMS breaks the deadlock in three steps:
Deadlock prevention and deadlock detection offer different engineering trade-offs (Textbook Exercise 18.20, tested in 2018 Q.5(b)):
Section B · Asked in: 2017 · L1 (asked once)
Question (2017 Q.7(b)). Define deadlock. Explain how deadlock is prevented?
Answer.
1. Deadlock Definition:
A deadlock is a condition in which two or more transactions are waiting indefinitely for locks held by each other. None of the transactions can continue without external intervention.
2. Deadlock Prevention Methods:
Deadlock prevention protocols ensure that the system never enters a deadlock state. Four standard schemes are:
Pre-declaration (Pre-claiming All Locks):
Each transaction must lock all required data items at once before execution begins. If any item is unavailable, no locks are granted and the transaction waits.
Drawbacks: Difficult to predict needed items beforehand. Data item usage is low.
Lock Ordering:
Impose a global ordering on all data items (d1<d2<⋯<dn). A transaction can only request a lock on item dj if it already holds locks on items that precede dj. Circular waits cannot form.
Timestamp-Based Schemes (Preemption and Rollback):
Each transaction Ti is assigned a unique timestamp TS(Ti) when it begins. The system compares timestamps to decide whether to wait or roll back:
| Scheme | When Ti requests item held by Tj | Type |
|---|---|---|
| Wait-Die | If Ti is older (TS(Ti)<TS(Tj)), Ti waits. If Ti is younger (TS(Ti)>TS(Tj)), Ti dies (rolls back). | Non-preemptive |
| Wound-Wait | If Ti is older (TS(Ti)<TS(Tj)), Ti wounds Tj (Tj rolls back). If Ti is younger, Ti waits. | Preemptive |
Timeout-Based Scheme:
A transaction waits for a lock for only a set amount of time. If the lock is not granted within the interval, the transaction times out and rolls back.
Section B · Asked in: 2018 · L1 (asked once)
Question (2018 Q.5(b)). What do you know about deadlock in transaction management? Under what conditions it is less expensive to avoid deadlock than to allow deadlocks to occur and then to detect them?
Answer.
Deadlock in Transaction Management:
A deadlock is an execution state where a set of transactions {T1,T2,…,Tn} cannot proceed because each transaction waits for a lock held by another transaction in the set. A circular wait forms in the wait-for graph. The DBMS must intervene, select a victim transaction, and roll it back to break the cycle.
When Avoidance / Prevention is Less Expensive Than Detection:
Deadlock avoidance or prevention is less expensive than detection and recovery under the following conditions:
When Detection is Less Expensive:
When deadlocks are rare (short transactions, low concurrency, mostly disjoint data items), detection is cheaper. Prevention mechanisms reduce concurrency or cause false-positive aborts. Detection allows transactions to run freely and resolves only the rare deadlock that occurs.
Section B · Asked in: 2020 · L1 (asked once)
Question (2020 Q.7(a)). What is deadlock? Describe the time-shared locking protocol with example.
Answer.
Assumption: "Time-shared locking protocol" is an exam misprint for the Two-Phase Locking (2PL) protocol.
Deadlock:
A deadlock is a situation where two or more transactions are in a mutual wait condition, each waiting for a data lock held by the other. Neither transaction can make progress.
Two-Phase Locking (2PL) Protocol:
The Two-Phase Locking protocol ensures conflict serializability by requiring that each transaction acquires and releases locks in two distinct phases:
Lock Point:
The point where a transaction acquires its final lock is its lock point. Serializability is guaranteed in the order of transaction lock points.
Example:
Consider transaction T1 modifying accounts A and B:
| Step | Action | Phase |
|---|---|---|
| 1 | lock-S(A); read(A) | Growing |
| 2 | lock-X(B); read(B); write(B) | Growing (Lock Point) |
| 3 | unlock(A) | Shrinking begins |
| 4 | unlock(B) | Shrinking |
T1 obtains all locks before releasing any lock. This satisfies 2PL and guarantees serializability.
Section B · CLO1 · Asked in: 2021 · L1 (asked once)
Question (2021 Q.8(a)). What is deadlock? Consider the following wait-for-graph:

Figure 2021_Q8.a: Wait-for-graph for deadlock detection (Source: Page 2, Q.8(a))
Determine whether the schedule contains any deadlock or not.
Answer.
Deadlock:
A deadlock is a state where each transaction in a set is waiting for a resource or data lock held by another transaction in that set. None of the transactions can continue execution.
Analysis of the Wait-For Graph:
The given wait-for graph has:
A directed edge Ti→Tj means transaction Ti is waiting for transaction Tj to release a required lock.
Cycle Detection:
We trace the directed edges among transactions T2,T4, and T5:
This forms a closed directed cycle.
Verdict:
Yes, the schedule contains a deadlock.
Transactions in Deadlock:
Transactions T2,T4, and T5 are deadlocked. (Note: T1 is waiting on T2,T3,T4, but is not part of the cycle. T3 has no outgoing edges and is not blocked).
Breaking the Deadlock:
The DBMS must select one transaction in the cycle (T2,T4, or T5) as a victim based on minimum abort cost, roll it back, and release its locks. This breaks the cycle and allows the remaining transactions to complete.
T10) means the schedule is not conflict serializable. A cycle in a Wait-For Graph (T11) means active transactions are in a deadlock.← Previous: T11.03 Strict 2PL, Rigorous 2PL, and Drawbacks of 2PL | 🏠 Index | Next: T11.05 Multiple Granularity Locking Protocol →