Professional Documents
Culture Documents
21.concurreny Controllocks Deadlock
21.concurreny Controllocks Deadlock
21.concurreny Controllocks Deadlock
•Binary locks
•Shared/Exclusive locks
.......... helps solve concurrency problem.
A. locking
B. transaction monitor
C. transaction serializability
D. two phase commit
Binary Locks
• A binary lock can have 2 States or values
• Locked (or 1) and Unlocked (or 0)
Operations used with Binary Locking
• lock_item : A transaction request access to an item by first
issuing a lock_item(X) operation.
– If LOCK(X) =1 or L(X) : the other transaction is forced to
wait.
– If LOCK(X) = 0 or U(x) : it is set to 0 and the transaction is
allowed to access item X.
Binary Locks
unlock_item : After using the data item the transaction issues an
operation unlock(X), which sets the operation LOCK(X) to 0 i.e.
unlocks the data item so that X may be accessed by another
transactions.
Transaction Rules for Binary Locks
Every transaction must obey the following rules :
•A transaction T must issue the lock(X) operation before
any read(X) or write(X) operations in T.
•A transaction T must issue the unlock(X) operation after all
read(X) and write(X) operations in T.
•If a transaction T will not issue a lock(X) operation if it already
holds the lock on item A.
•If a transaction does not hold the lock on item X, then T will not
issue an unlock(X) operation.
EXAMPLE
Problem here is:
If both T1 and T2 wants to issue read(A).
As read-read is not a conflict, all
transactions can read same data item.
But
If T1 will issue lock(A) then T2 cant
read(A) until T1 release lock i.e.
unlock(A)
Shared and Exclusive Locks or Read/Write
Locks
• The binary lock is too restrictive for data items because at most
one transaction can hold on a given item whether the
transaction is reading or writing.
• To improve it we have shared and exclusive locks in which
more than one transaction can access the same item for
reading purposes .i.e. the read operations on the same item by
different transactions are not conflicting.
In this types of lock, system supports two kinds of lock :
• Exclusive(or Write) Locks and
• Shared(or Read) Locks.
1. Shared Locks
•If a transaction T1 has locked the data item A in shared mode,
then a request from another transaction T2 on A for :
a) Write operation on A : Denied. Transaction T2 has to wait
until transaction T1 unlock A.
b) Read operation on A : Allowed.
2. Exclusive Locks
•If a transaction T1 has locked a data item a in exclusive mode
then request from some another transaction T2 for-
•Read operation on A : Denied
•Write operation on A : Denied
Compatibility table
Request
S X
Grant S Yes No
X No No
• Operations Used with Shared and Exclusive Locks
• Read_lock(A) or s(A)
• Write_lock(A) or X(A)
• Unlock(X) or U(A)
Locks
• If a transaction acquires a shared lock, then it can
perform .......... operation.
• A. read
• B. write
• C. read and write
• D. update
• If a transaction obtains a shared lock on a row, it
means that the transaction wants to ..... that row.
• A. write
• B. insert
• C. execute
• D. read
The Two-Phase Locking Protocol
The Two-Phase Locking Protocol
• Need of 2 Phase Locking
• Binary locks or shared and exclusive locks in
transaction does not guarantee serializability of
schedules on its own.
• For example, consider a banking transaction where
the read write locking rules are used, but we get a
non serializable schedule which give incorrect
result.
• In this scheme, each transaction makes lock and unlock request
in 2 phases :
• A Growing Phase( or An Expanding Phase or First Phase)
: In this phase, new locks on the desired data item can be
acquired but none can be released.
• A Shrinking Phase (or Second Phase) : In this phase, existing
locks can be released but no new locks can be acquired.
• A 2 phase locking always results serializable schedule but it
does not permit all possible serializable schedules i.e. some
serializable schedules will be prohibited by the protocol.
EXAMPLE
• Cascading rollback may occur in Two phase locking so
in order to avoid it following variations on two
phase locking is used:
• Rules
T1 T2 T3
R(A)
R(B)
W(C)
R(B)
R(C)
W(B)
W(A)
A B C
RTS 0 0 0
WTS 0 0 0
example
example
Deadlock Handling
DEADLOCK
Deadlock in Transaction
• Deadlock occurs when each transaction T in a set of
2 or more transactions is waiting for some item that
is locked by some other transaction T’ in the set.
• Hence, each transaction in the set is on a waiting
queue, waiting for one of the other transaction in
the set to release the lock on an item.
EXAMPLE
• System is deadlocked if there is a set of transactions such
that every transaction in the set is waiting for another
transaction in the set.