LOCK
Definition : Lock is a
variable associated with data item which gives the status whether the possible
operations can be applied on it or not.
Two-Phase Locking Techniques:
Binary locks: Locked/unlocked
The simplest kind of
lock is a binary on/off lock. This can be created by storing a lock bit with
each database item. If the lock bit is on (e.g. = 1) then the item
cannot be accessed by
any transaction either for reading or writing, if it is off (e.g. = 0) then the
item is available. Enforces mutual exclusion
Binary locks are:
Simple
but are restrictive.
Transactions
must lock every data item that is read or written
No
data item can be accessed concurrently
Locking is an operation which secures
(a) permission
to Read
(b) permission
to Write a data item for a transaction.
Example:
Lock (X). Data item X is locked in
behalf of the requesting transaction.
Unlocking is an operation which removes these
permissions from the data item.
Example:Unlock
(X): Data item X is made available to all other transactions.
•
Lock and Unlock are Atomic operations.
•
Lock Manager:
Managing
locks on data items.
•
Lock table:
•
Lock manager uses it to store the
identify of transaction locking a data item, the data item, lock mode . One
simple way to implement a lock table is through linked list.
< locking_transaction
,data item, LOCK >
The following code performs the lock operation:
B:
if LOCK (X) = 0 (*item is unlocked*)
then
LOCK (X) 1 (*lock the item*)
else
begin
wait
(until lock (X) = 0) and
the
lock manager wakes up the transaction);
goto
B
end;
The following code performs the unlock operation:
LOCK
(X) < - 0(*unlock the item*)
if
any transactions are waiting then
wake
up one of the waiting the transactions;
Multiple-mode locks: Read/write
– a.k.a.
Shared/Exclusive
•
Three operations
–
read_lock(X)
–
write_lock(X)
– unlock(X)
•
Each data item can be in one of three
lock states
– Three locks
modes:
• (a) shared (read) (b) exclusive (write) (c) unlock(release)
– Shared mode: shared lock (X)
More than one transaction can apply
share lock on X for reading its value but no write lock can be applied on X by
any other transaction.
– Exclusive mode: Write lock (X)
Only one write lock on X can exist at
any time and no shared lock can be applied by any other transaction on X.
_ Unlock
mode: Unlock(X)
After reading or writing the
corresponding transaction releases by issuing this.
The rules for multiple-mode locking schemes are a
transaction T:
Issue
a read_lock(X) or a write_lock(X) before read(X)
Issue
a write_lock(X) before write(X)
Issue
an unlock(X) after all read(X) and write(X) are finished
The transaction T
Will
not issue read_lock (X) or write_lock(X) if it already holds a
lock on X
Will
not issue unlock(X) unless it already holds a lock on X
Lock table:
Lock manager uses it to store the identify of
transaction locking a data item, the data item, lock mode and no of
transactions that are currently reading the data item . It looks like as below
<data
item,read_ LOCK,nooftransactions,transaction id >
This protocol isn’t
enough to guarantee seri create problems. This usually happens when a lock is
released before another lock is acquired.
The following code performs the read operation:
B: if
LOCK (X) =
“unlocked” then
begin
LOCK (X) “read-locked”;
no_of_reads
(X) 1;
end
else
if LOCK (X) “read-locked” then no_of_reads (X) no_of_reads (X) +1
else begin
wait (until LOCK
(X) = “u
the
lock manager wakes up the transaction);
go
to B
end;
The following code performs the write lock
operation:
B:
if
LOCK (X) =
“unlocked” then
LOCK
(X)< - “write-locked”;
else begin wait (untild”and LOCK (X) the
lock manager wakes up the transaction);
go to B end;
The following code performs the unlock operation:
if LOCK (X) = “write-locked” then
begin LOCK (X) “unlocked”;
wakes
up one of the transactions, if any
end
else if LOCK (X) < - “read-locked”
then begin
no_of_reads (X) < - no_of_reads (X) -1
if no_of_reads (X) = 0 then
begin
LOCK (X) = “unlocked”; wake up one of
the transactions, if any
end end;
Lock
conversion:
Lock
upgrade: existing read lock to write lock
if Ti has a read-lock (X) and Tj has no read-lock
(X) (i!= j) then convert read-lock (X) to write-lock (X)
else
force
Ti to wait until Tj unlocks X
Lock
downgrade: existing write lock to read lock
Ti
has a write-lock (X) (*no transaction
can have any lock on X*)
convert
write-lock (X) to read-lock (X)
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.