25.9 KB · commit 40bb182
← Previous: T11.3: Strict 2PL, Rigorous 2PL, and Drawbacks of 2PL | 🏠 Index | Next: T11.5: Wait-Die vs. Wound-Wait Deadlock Prevention →
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 handling is one of the highest-yield concurrency control topics in ECE 2215, appearing in 4 out of 7 past final exams for a total of 17 marks. Because Two-Phase Locking (2PL) forces transactions to hold onto existing locks while waiting for new ones, two or more transactions can easily trap each other in a permanent stalemate. Examiners regularly test three angles of this topic: (1) the four methods for preventing deadlocks before they happen, (2) when prevention is cheaper than detection, and (3) tracing wait-for graphs to spot deadlock cycles and pick a victim to roll back.
Deadlock: A state where two or more transactions wait forever because each holds a lock that another transaction in the group needs. None of the transactions in this circular wait can move forward unless the system rolls one of them back.
Wait-For Graph: A directed graph G=(V,E) used to detect deadlocks. The vertices V are active transactions, and a directed edge Ti→Tj means Ti is waiting for Tj to release a lock. The system is in a deadlock state if and only if the wait-for graph contains a directed cycle.
Deadlock Prevention: A set of locking rules that stops circular waits from ever forming so the database never enters a deadlock state. Common prevention methods include locking all items upfront, locking items in a fixed order, using timestamps (
wait-dieorwound-wait), and using lock timeouts.
Imagine two drivers meeting head-on in a single-lane alley where neither car will back up. In a database, a deadlock can happen if and only if four conditions hold at the exact same time:
Every deadlock prevention method works by breaking at least one of these four conditions.
Database systems deal with deadlocks using one of two main strategies:
| Strategy | Core Idea | Best Used When | Main Cost / Trade-off |
|---|---|---|---|
| Deadlock Prevention | Set strict lock rules so a circular wait can never form in the first place. | Lock conflicts are frequent and transactions are long or expensive to undo. | Lower concurrency, extra upfront rules, or unnecessary precautionary rollbacks. |
| Deadlock Detection & Recovery | Let transactions request locks freely, check a wait-for graph periodically for cycles, and abort one victim if a deadlock forms. | Lock conflicts are rare and transactions are short. | CPU overhead to maintain and scan the wait-for graph, plus lost work when a victim rolls back. |
Deadlock prevention stops circular waits before they can ever form using four classic techniques (Lecture Deck 08, Slides 22–23):
Wait-Die and Wound-Wait)Wait-Die or Wound-Wait, it restarts with its original timestamp so it gradually becomes the oldest transaction in the system and cannot starve. (Full trace details in T11.05.)When a database does not use prevention, it lets transactions request locks freely and tracks who is waiting on whom using a wait-for graph.
Figure 11.4.1: Two-transaction interleaved schedule and resulting circular wait-for graph (Source: Course archive).
Think of each active transaction as a dot (vertex), and draw an arrow Ti→Tj whenever "Ti is waiting for Tj to unlock something." As soon as Tj unlocks the item and Ti gets the lock, you erase the arrow. If the arrows ever form a closed loop (a cycle), every transaction in that loop is waiting on the next one in a circle, which means they are deadlocked!
Theorem: A database system is in a deadlock state if and only if its wait-for graph G=(V,E) contains at least one directed cycle.
Warning (Cycle Nodes vs. Blocked Nodes):
Only the transactions inside the cycle (for example, T2→T4→T5→T2) are actually deadlocked. If another transaction T1 has an arrow pointing into the cycle (T1→T2) but no arrow pointing back to T1, then T1 is simply blocked waiting for the deadlock to clear, and T1 is not part of the deadlock cycle itself!
Once the cycle-detection algorithm spots a loop in the wait-for graph, the recovery manager breaks the deadlock in three steps:
The system picks one transaction from the cycle (the victim) to roll back. To waste as little work as possible, the lock manager weighs four cost factors:
If the cost formula always picks the youngest or smallest transaction as the victim, the same unlucky transaction could get aborted over and over again every time it restarts. To prevent this starvation, the system includes the number of times a transaction has already been rolled back in the victim-selection formula, making repeat victims protected after a few rollbacks.
A classic Silberschatz question (Exercise 18.20, tested verbatim in 2018 Q.5(b)) asks: When is it cheaper to prevent/avoid deadlocks upfront rather than letting them happen and detecting them?
| Year & Question | Marks | Scenario / Data Given | Key Result / Answer Summary |
|---|---|---|---|
| 2017 Q.7(b) | 4 | General theory on deadlock definition and prevention | Defined deadlock; explained pre-declaration, ordering, timestamps (wait-die/wound-wait), and timeouts |
| 2018 Q.5(b) | 4 | Trade-off question from Textbook Exercise 18.20 | Defined deadlock; analyzed contention, transaction duration, and detection overhead trade-offs |
| 2020 Q.7(a) | 4 | Deadlock definition + "time-shared locking" misprint | Clarified misprint for 2PL; defined 2PL growing/shrinking phases, lock point, and example schedule |
| 2021 Q.8(a) | 5 | Wait-for graph with 5 vertices (T1 to T5) | Detected directed cycle T4→T5→T2→T4; identified deadlocked transactions and victim rollback |
Appeared: 2017 Q.7(b) — (04 marks)
Question:
Define deadlock. Explain how deadlock is prevented?
Full Answer:
A deadlock is a state in which a set of two or more concurrent transactions are blocked indefinitely because each transaction in the set holds a lock on a data item that another transaction in the set needs, creating a circular wait where none of the transactions can proceed.
Deadlock prevention protocols ensure that the database never enters a circular wait state using four main methods:
Pre-declaration (Pre-claiming All Locks): Every transaction must request and acquire all the locks it will need before it begins executing, or else wait without holding any locks.
Lock Ordering (Graph-Based / Total Ordering): All data items are assigned a strict global order (d1<d2<⋯<dn), and transactions may only request locks in ascending order. Because requests always move forward along the order, a cycle can never form.
Timestamp-Based Preemption Schemes (Wait-Die and Wound-Wait):
Each transaction Ti receives a startup timestamp TS(Ti) upon arrival. When Ti requests a lock currently held by Tj:
| Scheme | Older Ti requests item held by Younger Tj | Younger Ti requests item held by Older Tj | Mechanism |
|---|---|---|---|
| Wait-Die | Older waits | Younger dies (Ti rolls back) | Non-preemptive |
| Wound-Wait | Older wounds younger (Tj rolls back) | Younger waits | Preemptive |
Timeout-Based Scheme: A transaction waits for a requested lock only up to a fixed timeout limit. If the lock is not granted within that time, the transaction assumes a potential deadlock and rolls itself back.
Appeared: 2018 Q.5(b) — (04 marks)
Question:
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?
Full Answer:
In transaction management, a deadlock is a stalemate across a set of active transactions {T0,T1,…,Tn} where each transaction holds a lock needed by the next transaction in the set, forming a closed cycle in the wait-for graph:
T0→T1→⋯→Tn→T0Because two-phase locking forbids releasing locks early during the growing phase, none of the transactions in the cycle can advance unless the database intervenes and rolls back a victim transaction.
Avoiding or preventing deadlocks upfront is cheaper than allowing and detecting them under three conditions:
(Conversely, detection and recovery is cheaper when deadlocks are rare, transactions are short, and data contention is low.)
Appeared: 2020 Q.7(a) — (04 marks)
Question:
What is deadlock? Describe the time-shared locking protocol with example.
(Sorting Note: Second part asks for "time-shared locking protocol" [misprint for 2PL, cross-ref T11.2])
Full Answer:
Note: "Time-shared locking protocol" on the 2020 question paper is a printing typo for the Two-Phase Locking (2PL) protocol.
A deadlock is a system state in which two or more concurrent transactions are blocked waiting for each other to release locks in a circular chain, so that none of the transactions can proceed without external rollback intervention.
The Two-Phase Locking protocol guarantees conflict serializability by requiring every transaction to acquire and release locks in two sequential phases:
lock-S) or exclusive (lock-X) locks, or upgrade an existing lock (S → X).unlock), or downgrade an existing lock (X → S).Lock Point: The exact point where a transaction acquires its final lock (marking the end of the growing phase) is called its lock point. Transactions can be serialized in the order of their lock points.
Consider transaction T1 reading account A and updating account B:
| Step | Operation | Phase |
|---|---|---|
| 1 | lock-S(A); read(A) | Growing Phase |
| 2 | lock-X(B); read(B); write(B) | Growing Phase (Lock Point) |
| 3 | unlock(A) | Shrinking Phase begins |
| 4 | unlock(B) | Shrinking Phase |
Because all lock acquisitions (Steps 1–2) occur before the first unlock (Step 3), T1 obeys 2PL.
Appeared: 2021 Q.8(a) — (05 marks)
Question:
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.
(Refer to the Wait-for-Graph Diagram and Detailed Text Representation below)
Wait-for-Graph Details (
2021 Q.8(a)): Vertices V={T1,T2,T3,T4,T5}; Directed edges E={(T1,T4),(T1,T2),(T1,T3),(T4,T5),(T5,T2),(T2,T4)}; Cycle T4→T5→T2→T4⟹ Deadlock.
Full Answer:
A deadlock is an execution state in which two or more transactions are waiting indefinitely for locks held by one another in a circular chain, preventing any of them from making progress.
From the given diagram, the wait-for graph G=(V,E) has:
Tracing the directed edges starting from T4:
This forms a closed directed cycle of length 3:
T4→T5→T2→T4Yes, the schedule contains a deadlock because the wait-for graph contains a directed cycle.
To break the deadlock, the recovery manager selects one victim from {T2,T4,T5} with the lowest rollback cost and rolls it back (totally or partially), removing its edges from the cycle so the remaining transactions can finish.
Source: Silberschatz 7e · Chapter 18 (Exercise 18.21)
Exam Relevance: Prime Conceptual Viva and Theoretical Exam Question
Key Tested Concept: Starvation hazards in deadlock-avoidance schemes and countermeasure protocols.
Problem Statement:
If deadlock is avoided by deadlock-avoidance schemes, is starvation still possible? Explain your answer with concrete scenarios and identify how DBMSs prevent starvation.
Model Exam-Style Solution:
Direct Verdict: Yes, starvation is still possible under deadlock-avoidance schemes unless specific anti-starvation mechanisms (such as timestamp ordering or priority aging) are enforced.
Scenario 1: Resource-Ordering Deadlock Prevention
Scenario 2: Rollback-Based Avoidance without Timestamp Retention
DBMS Countermeasures to Prevent Starvation:
💡 RUET Exam Takeaway: Deadlock avoidance guarantees that the system as a whole never freezes in a cycle, but it does not automatically guarantee individual transaction progress. Always mention FIFO lock queues and timestamp seniority retention as the solutions to starvation.
2021 Q.8(a), always write the full loop (T4→T5→T2→T4) Including the repeated start/end node so the grader sees the closed cycle immediately.2021 Q.8(a), many students mistakenly list T1 as deadlocked because T1 points to T2 and T4. Pointing into a cycle makes T1 temporarily blocked, not part of the circular deadlock!T10.03a) means the schedule is not conflict serializable. A cycle in a Wait-For Graph (T11.04) means the system is currently in a deadlock.← Previous: T11.3: Strict 2PL, Rigorous 2PL, and Drawbacks of 2PL | 🏠 Index | Next: T11.5: Wait-Die vs. Wound-Wait Deadlock Prevention →