Concurrency Controlالتحكم في التزامن

الشريحة 1 من 12

مقدمة الفصل

تفاصيل المصدرالفصل 7 · الشريحة ⁦2⁩
المصدر
الفصل 7 · الشريحة ⁦2⁩

Lock-based protocolsالأقفال

Locks and lock modes

Lock
A mechanism to control concurrent access to a data item. Requests go to the concurrency-control manager; the transaction proceeds only after the request is granted.

Exclusive (X) modeحصري

  • Data item can be both read and written
  • Requested with lock-X

Shared (S) modeمشترك

  • Data item can only be read
  • Requested with lock-S
Lock compatibility (derived from slide 4's rule)
Requested \ heldS heldX held
S requestedcompatible (granted)not compatible (wait)
X requestednot compatible (wait)not compatible (wait)
  • Any number of transactions can hold shared locks on an item; if any transaction holds an exclusive lock, no other transaction may hold any lock on it.
  • If a lock cannot be granted, the requester waits till all incompatible locks have been released.

T1 holds a shared lock on item Q. Which request can be granted immediately?

تفاصيل المصدرالفصل 7 · الشرائح ⁦3–4⁩
المصدر
الفصل 7 · الشرائح ⁦3–4⁩

Why a locking protocol?لماذا بروتوكول القفل؟

Locks and lock modes

من الشرائح

  • T2: lock-S(A); read(A); unlock(A); lock-S(B); read(B); unlock(B); display(A+B)
  • Locking as above is not sufficient to guarantee serializability: if A and B get updated in-between the read of A and B, the displayed sum would be wrong.
Locking protocol
A set of rules followed by all transactions while requesting and releasing locks; locking protocols restrict the set of possible schedules.
تفاصيل المصدرالفصل 7 · الشريحة ⁦5⁩
المصدر
الفصل 7 · الشريحة ⁦5⁩

Two-phase lockingالقفل ثنائي المرحلة

Two-phase locking protocol (slide 6)

Phase 1: growing

  • May obtain locks
  • May not release locks
  • With conversions: lock-S, lock-X, upgrade S → X

Phase 2: shrinking

  • May release locks
  • May not obtain locks
  • With conversions: release S/X, downgrade X → S
  • Ensures conflict-serializable schedules: transactions can be serialized in the order of their lock points (where each acquired its final lock).
  • Still relies on the programmer to insert the locking instructions.

Under two-phase locking, a transaction has released its lock on A. Which action is now forbidden?

تفاصيل المصدرالفصل 7 · الشرائح ⁦6–7⁩
المصدر
الفصل 7 · الشرائح ⁦6–7⁩

Deadlocksالجمود

Partial schedule (slide 8)
T3T4
lock-X(B)
read(B)
B := B − 50
write(B)
lock-S(A)
read(A)
lock-S(B)
lock-X(A)
  • T4's lock-S(B) waits for T3 to release B; T3's lock-X(A) waits for T4 to release A — neither can make progress: a deadlock.
  • To handle it, one of T3 or T4 must be rolled back and its locks released.
  • Two-phase locking does not ensure freedom from deadlocks.

T3 holds lock-X(B) and requests lock-X(A); T4 holds lock-S(A) and requests lock-S(B). What happens?

تفاصيل المصدرالفصل 7 · الشرائح ⁦8–9⁩
المصدر
الفصل 7 · الشرائح ⁦8–9⁩

Starvationالتجويع

Starvation
Occurs if the concurrency control manager is badly designed — e.g., a transaction waits for an X-lock while other transactions keep being granted S-locks on the same item, or the same transaction is repeatedly rolled back due to deadlocks.

Reasons for starvation (slide 10)

  • Unfair waiting scheme (priority queue)
  • Victim selection: same victim repeatedly
  • Resource leak
  • Denial-of-service attack

T1 waits for an X-lock on Q, but new transactions keep receiving S-locks on Q, so T1 never proceeds. What is this?

تفاصيل المصدرالفصل 7 · الشرائح ⁦9–10⁩
المصدر
الفصل 7 · الشرائح ⁦9–10⁩

Lock manager and lock tableمدير الأقفال وجدولها

  • The lock manager can be a separate process: it replies to a lock request with a lock grant message (or a message asking the transaction to roll back, in case of a deadlock); the requester waits for the answer.
  • The lock table records granted locks and pending requests (and the lock type); usually an in-memory hash table indexed on the name of the data item.

Lock table rules (slide 12)

  1. 1

    A new request is added to the end of the queue for the data item; it is granted if it is compatible with all earlier locks.

  2. 2

    An unlock deletes the request; later requests are checked to see if they can now be granted.

  3. 3

    If a transaction aborts, all its waiting or granted requests are deleted.

How is the lock table usually implemented?

تفاصيل المصدرالفصل 7 · الشرائح ⁦11–12⁩
المصدر
الفصل 7 · الشرائح ⁦11–12⁩

Deadlock detectionكشف الجمود

Wait-for graph G = (V, E)
V = all the transactions; an edge Ti → Tj means Ti is waiting for Tj to release a data item. It is inserted when Ti requests an item held by Tj and removed when Tj no longer holds an item needed by Ti.
Deadlock test
The system is in a deadlock state if and only if the wait-for graph has a cycle. A deadlock-detection algorithm must be invoked periodically.

A wait-for graph has edges T1 → T2 and T2 → T3 only. Is the system deadlocked?

تفاصيل المصدرالفصل 7 · الشرائح ⁦13–14⁩
المصدر
الفصل 7 · الشرائح ⁦13–14⁩

Deadlock preventionمنع الجمود

Deadlocked system
There is a set of transactions such that every transaction in the set is waiting for another transaction in the set.

Prevention protocols ensure the system never enters a deadlock state (slide 15)

  • Pre-declaration: each transaction locks all its data items before it begins execution.
  • Partial ordering of all data items: a transaction locks items only in that order.
Timeout-based schemes (slide 21)
A transaction waits for a lock only for a specified time, then is rolled back and restarted — deadlocks are not possible. Simple to implement, but starvation is possible, and a good timeout value is difficult to determine.
تفاصيل المصدرالفصل 7 · الشرائح ⁦15, 21⁩
المصدر
الفصل 7 · الشرائح ⁦15, 21⁩

Wait-die and wound-waitانتظر-مت وجرح-انتظر

Deadlock prevention: wait-die, wound-wait and timeouts

Timestamp-based prevention (smaller timestamp = older)
T1 requests an item held by T2Wait-die (non-preemptive)Wound-wait (preemptive)
T1 older: TS(T1) < TS(T2)T1 waitsT1 wounds T2: T2 is rolled back
T1 younger: TS(T1) > TS(T2)T1 dies (rolled back)T1 waits
Do older transactions wait?Yes, for younger onesNever wait for younger ones
Aborts and rollbacksHigherLesser

حدد ما يحدث قبل الكشف.

  1. Wait-die

    TS(T1) = 5, TS(T2) = 10. T2 requests an item locked by T1.

  2. Wound-wait

    Same timestamps. T1 requests an item locked by T2.

  • In both schemes a rolled-back transaction keeps its original timestamp: older transactions gain precedence and starvation is avoided.

Wound-wait: TS(T1) = 3 and TS(T2) = 8. T1 requests an item held by T2. What happens?

تفاصيل المصدرالفصل 7 · الشرائح ⁦16–21⁩
المصدر
الفصل 7 · الشرائح ⁦16–21⁩

Deadlock recoveryالتعافي من الجمود

  1. Victim

    Roll back some transaction (a victim): choose the one that will incur minimum cost.

  2. How far

    Total rollback (abort and restart), or — more effective — roll back only as far as necessary to break the deadlock.

  3. Starvation

    Happens if the same transaction is always the victim: include the number of rollbacks in the cost factor.

When a deadlock is detected, which transaction should be chosen as the victim?

تفاصيل المصدرالفصل 7 · الشريحة ⁦22⁩
المصدر
الفصل 7 · الشريحة ⁦22⁩

الخلاصة

تفاصيل المصدر
المصدر
الفصل 7

خريطة الفصل

    1. 1مقدمة الفصل
  1. Locks and lock modesالأقفال وأنماطها

    1. 2Lock-based protocolsالأقفال
    2. 3Why a locking protocol?لماذا بروتوكول القفل؟
  2. Two-phase locking and lock conversionsالقفل ثنائي المرحلة وتحويل الأقفال

    1. 4Two-phase lockingالقفل ثنائي المرحلة
  3. Deadlocks and starvationالجمود والتجويع

    1. 5Deadlocksالجمود
    2. 6Starvationالتجويع
  4. Implementation of locking: lock manager and lock tableتنفيذ القفل: مدير الأقفال وجدول الأقفال

    1. 7Lock manager and lock tableمدير الأقفال وجدولها
  5. Deadlock detection: the wait-for graphكشف الجمود: مخطط الانتظار

    1. 8Deadlock detectionكشف الجمود
  6. Deadlock prevention: wait-die, wound-wait and timeoutsمنع الجمود: انتظر-مت، وجرح-انتظر، والمهلة

    1. 9Deadlock preventionمنع الجمود
    2. 10Wait-die and wound-waitانتظر-مت وجرح-انتظر
    1. 11Deadlock recoveryالتعافي من الجمود
    1. 12الخلاصة