2. CONTENTS
What is concurrency control?
Lock based protocol
Two phase locking protocol
deadlock
3. What is concurrency control?
The technique is used to protect data when multiple users
are accessing same data concurrently(same time) is called
concurrency control.
4. Lock Based Protocol
o Lock is a mechanism to control concurrent access to data
item
o Data items can be locked in two modes:
a. Exclusive(X)Mode: Data item can be both read as well
as written. X-lock is request using lock-X instruction.
b. Shared (S) mode: data item can only be read,S-lock is
request using lock-s instruction.
o Lock request are made to concurrency-control manger.
o Transaction can proceed only after request s grated.
5. Cont……..
Lock-compatibility matrix:
A transaction may be granted a lock on
an item if the requested lock is
compatible with locks already held on
the item by other transaction.
Any number of transaction can hold
shared locks on an item , but if any
transaction holds an exclusive on the
item no other transaction may hold any
lock on the item.
s x
s true
fals
e
x fals
e
fals
e
6. Cont……..
A transaction may be granted a lock on an item if the
requested lock is compatible with locks already held
on the item by other transaction.
Any number of transaction can hold shared locks on
an item , but if any transaction holds an exclusive on
the item no other transaction may hold any lock on
the item.
7. Cont…….
If a lock cannot be granted , the requesting transaction is
made to wait till all incompatible locks held by other
transaction have been released. The lock is then granted.
Example:
T1:lock-S(A); //Grant-S(A.T1)
read(A);
Unlock(A);
Lock-S(B); //Grant-S(B.T1)
Read(B);
Unlock(B);
Display(A+B)
8. Pitfalls of lock based protocol
Locking as above is not sufficient to guarantee
serialializability- if A and B get in between the read of A and B
,the displayed sum would be wrong.
A locking protocol is a set of rules followed by all transactions
while requesting and releasing locks . Locking protocols
restrict the set of possible schedules.
T3 T4
Lock-x(B)
Read(B)
B:=B – 50
Write(B)
Lock-X(A)
Lock-S(A)
Read(A)
Lock-S(B)
9. Cont….
Neither T₃ nor T₄ can make progress –executing lock-S(B) causes T₄ to
wait for T₃ to release its lock on B , while executing lock-X(A) causes T₃
to wait for T₄ to relase its lock on A.
To handle a deadlock one of T₃ or T₄ must be rolled back
and its locks relased.
Starvation is also possible if concurrency control manager
is badly designed. For example:
I. A transaction may be waiting for an X –lock on an item
,while a sequence of other transactions request and are
granted an S-lock on the same item.
II. The same transaction is repeatedly rolled back due to
deadlocks.
Concurrency control manager can be designed to prevent
starvation.
10. Two phase locking protocol
This is a protocol which ensures conflict-serializable
schedules.
Phase 1:Growing Phase
-transaction may obtain locks
-transaction may not release locks
Phase 2: Shrinking Phase
-transaction may release locks
-transaction may not obtain locks
The protocol assures serializability. It can be proved that
the transaction can be serialized in the order of their lock
points.
(i.e, the point where a transaction acquired its final lock).
11. Cont….
Two –phase locking does not ensure from deadlocks.
Cascading roll-back is possible under two-phase locking.
To avoid this, follow a modifed protocol called strict two-
phase locking. Here a transaction must hold all its
exclusive locks till it commits/aborts.
Rigorous two-phase locking is even stricter: here all
locks are held till commit/abort. In this protocol
transaction can be serialized in the order in which they
commit.
12. DEADLOCK
A set of process is deadlock if each process in the set is
waiting for an event that only another process in the set can
cause.
13. Four conditions for deadlock
Mutual exclusive condition
-each resource assigned to 1 process or is available
Hold and wait condition
-process holding resources can request additional
No preeption condition
-previously granted resoures cannot forcibly taken away
Circular wait condition
-must be a circular chain of 2 or more process
-each is waiting for resource held by next member of the
chain
14. Strategies of deadlock handling
Deadlock prevention:
prevents deadlocks by restraining requests made to ensure
that at least one of the deadlock conditions cannot occur.
• Deadlock avoidance:
Dynamically grants a resource to a process if the resulting
state is safe. A state is safe if there is at least one execution
sequence that allows all process to run to completion.
• Deadlock detection and recovery:
That allows dealocks to form; then finds and breaks them.
15. Deadlock avoidance
WAIT- DIE RULE:
if Tᵢ request a lock on a data
item which is already locked
by Tʲ, then Tᵢ is permitted to
wait if ts (Tᵢ)<ts(Tʲ). If ts
(Tᵢ)>ts(Tʲ), then Tᵢ is aborted
and restarted with the same time
stamp.
-if ts (Tᵢ)<ts(Tʲ) then Tᵢ waits
else Tᵢ dies
-non-preempting: Tᵢ never
preempts Tʲ
-prefers younger transactions
WOUND-WAIT RULE:
If Tᵢ request a lock on a data
item which is already locked by
Tʲ, then Tᵢ is permitted to wait if
ts (Tᵢ)>ts(Tʲ). If ts (Tᵢ)<ts(Tʲ),
then Tʲ is aborted and the lock is
granted to Tᵢ.
-if ts (Tᵢ)<ts(Tʲ) then Tʲ is
wounded else Tᵢ waits
- preemptive : Tᵢ preempts Tʲ if it
is younger
-prefers older transactions
16. Deadlock detection
For deadlock detection, the system must provide:
An algorithm that examines the state of the system to detect
whether a deadlock has occurred
And an algorithm to recover from the deadlock
A detection-and –recovery scheme require various kinds of
overhead
Runtime –time costs of maintaining necessary information and
executing the detection algorithm.