# GATE2007-IT-66

6.2k views

Consider the following two transactions : T1 and T2.

 T1 : read (A); T2 : read (B); read (B); read (A); if A = 0 then B ← B + 1; if B ≠ 0 then A ← A - 1; write (B); write (A);

Which of the following schemes, using shared and exclusive locks, satisfy the requirements for strict two phase locking for the above transactions?

1.  S1 : lock S(A); S2 : lock S(B); read (A); read (B); lock S(B); lock S(A); read (B); read (A); if A = 0 if B ≠ 0 then B ← B + 1; then A ← A - 1; write (B); write (A); commit; commit; unlock (A); unlock (B); unlock (B); unlock (A);
2.  S1 : lock X(A); S2 : lock X(B); read (A); read (B); lock X(B); lock X(A); read (B); read (A); if A = 0 if B ≠ 0 then B ← B + 1; then A ← A - 1; write (B); write (A); unlock (A); unlock (A); commit; commit; unlock (B); unlock (A);
3.  S1 : lock S(A); S2 : lock S(B); read (A); read (B); lock X(B); lock X(A); read (B); read (A); if A = 0 if B ≠ 0 then B ← B + 1; then A ← A - 1; write (B); write (A); unlock (A); unlock (B); commit; commit; unlock (B); unlock (A);
4.  S1 : lock S(A); S2 : lock S(B); read (A); read (B); lock X(B); lock X(A); read (B); read (A); if A = 0 if B ≠ 0 then B ← B + 1; then A ← A - 1; write (B); write (A); unlock (A); unlock (A); unlock (B); unlock (B); commit; commit;

edited
2
Please explain me someone what's wrong in option a if they mention exclusive lock on b in option a
Thin this option is best suitable for strict 2 pl as it is regorous 2 pl
0

Strict 2 PL ensures  conflict serializable serial schedule based on Lock Point but above mentioned scheduled not conflict serializable. And 2 PL says other transaction can not R/W uncommitted values but it is happening here.

So how is this  2 P L ?

ping @Krish__, @rahul sharma 5,  @Red_devil, @Shivam Chauhan, @Tuhin Dutta, @Anu007,  @Ashwin Kulkarni @reena_kandari  and @srestha ji.

6

The above question just asks if the modified transactions can lead to a strict 2PL schedule. The schedule itself hasn't been given and we can only check if:

1. Exclusive locks should be released after the commit for each transaction.

2. No Locking of the same data item done after the first Unlock and vice versa.

3. The transactions have proper shrinking and growing phase.

0
@krish_ first of all, are the two schedules S1 and S2 independent?
Secondly, why can't option (a) be strict 2PL? It satisfies the condition for both Strict as well as Rigorous 2PL.
0
Also, is it necessary that a schedule has to have an exclusive lock for it to follow strict 2PL? As maybe the case with option (a).....kinda stupid doubt but it exists!
0
same doubt @jerry
3
in option (a) since we have just acquired shared lock  and hence we can only read not write. for writing we need exclusive locks.
0

In strict 2PL all the exclusive locks must be unlocked after the commit operation.

Option (a) write operation in both schedules need exclusive locks,thus it is wrong option because it does not follows locking protocol

option(b) In S1 shared lock is unlocked after commit operation. So,wrong.

option (c) correct. Since, all exclusive locks in both S1 and S2 are unlocked after commit operation

option (d) exclusive locks are not unlocked after commit operation. So wrong.
0
suppose if any schedule does not have write operation and hence it does not need to acquire an exclusive lock, so in this case, will the schedule be in Strict 2pl. I mean to ask that a schedule with no exclusive lock is in strict 2pl or not?
0
For Strict 2PL, All X_LOCKS() are held till commit in addition to to locks being 2PL.
0

@akku_126 Yes, It will be in Strict 2PL if there are no X_LOCKS() provided it follows 2PL since condition for a schedule being Strict 2PL is on X_Locks().

Many of you would point a DEADLOCK and I won't deny But see Question just asks for requirement to follow Strict 2PL. Requirement are

1. Exclusive locks should be released after the commit.
2. No Locking can be done after the first Unlock and vice versa.

In 2Pl deadlock may occur BUT it may be that it doesn't occur at all.

Consider that in option (C) if both execute in serial order without concurrency. Then that is perfectly valid and YES it follows Strict 2PL.

edited
1
but in c) we have unlocked write lock on A before commit in S2....then how can it be in 2PL?
3
@dhingrak :I think that is a printing mistake !! See they have unlocked the X_lock(A) two times . First one must have been Unlock(B)

If it is exactly same as given NO option matches in that case !!
4
What's wrong with option A?
0
If t2 has shared lock on A,then can T1 can get that lock as an exclusive lock?
2
Why not option A ?
1
In Option C there is also cycle in precedance graph. If it is in 2PL then there shouldn't be any cycles  because it wil be in CS
0
In option A, in S2 there has to be an Exclusive lock on A.
0
20
In strict 2PL all the exclusive locks must be unlocked after the commit operation.

Option (a) write operation in both schedules need exclusive locks,thus it is wrong option because it does not follows locking protocol

option(b) In S1 shared lock is unlocked after commit operation. So,wrong.

option (c) correct. Since, all exclusive locks in both S1 and S2 are unlocked after commit operation

option (d) exclusive locks are not unlocked after commit operation. So wrong.
1
No locking can be done after the first unlock and vice versa .

is this point checking necessary ?

becoz i feel exclusive lock should be release after the commit is enough.
2

"No locking can be done after the first unlock and vice versa" .

This is the basic requirement of 2PL.

0
Option (A) is wrong because only shared locks are used in that case. Since we are also using write operation in that case, so shared lock isn't sufficient. Am i right?
0
It must have to aquire exclusive lock
0

2 Phase Locking requires locking and unlocking in two phases:

1. Growing Phase: A transaction can obtain locks but can't release locks.
2. Shrinking Phase: A transaction can release the locks but can't obtain locks.

For Strict 2PL, All X_LOCKS() are held till commit in addition to to locks being 2PL.

A) is eliminated because write (B) is done in S1 without acquiring Exclusive lock on B.

B) is eliminated because unlock (A) should happen only after commit in a strict 2pL.

D) is also eliminated. The reason is exactly same as B)

•  It can be clearly seen that it is a 2 pL because all acquired locks are released only after locking_point.
•  It is also clear that it is also a strict 2 pL as all Exclusive locks acquired by a transaction are released only after commit operation.
0

3) Shared lock can be unlocked anytime Exclusive only after commit
4) is 2pl
2) is Wrong Exclusive cannot take other's Exclusive Lock.

1) To write X you need Exclusive Lock So WRONG
Correct If Wrong.

edited
0
How option 4 ? 4 has commit after release of xclusive locks, it shud commit before releasing....

Moreover the option 2 and 3 i guess incorrectly given... both didnt hav release of B for A
0
@Abhinav

can u explain

2) is Wrong Exclusive cannot take other's Exclusive Lock
0
@cse23 ,  " if schedule in 2PL then it is CS " This is correct , right ?

If not CS then not 2PL is this also correct ??
Here in option some mistake is there in original paper.

The requirements for strict two phase locking for the above transactions  = strict two phase locking says unlock exclusive lock i.e. unlock X(); after commit or rollback.

Shared lock used for Read , exclusive lock used for  Write data.

Releasing exclusive lock after commit restrict the deadlock condition. i.e. we cannot get lock on item were exclusive lock already taken.
0
Thanks
0
But strict 2PL can have deadlock...
2
whats wrong with, option (a), it is rigorous so implying strict 2PL

Edit: only shared locks no exclusive so wrong
0
You need exclusive lock to write data.

No exclusive locks are present in A , yet write was performed
0
@Dulqar

we know p-->q is equivalent to ~q-->~p

if 2PL implies CSS

hence If schedule is not CSS then It is not 2PL also
0
@cse23 in option C there is cycle in precedence graph so it is not CSS and hence not 2PL
correct me if im wrong
0
the exact interleaving of operations is not even shown in the diagram, how are u drawing the precedence cycle??? Moreover if every transaction in a schedule individually follows 2PL then the schedule is always serializable. Hence option C
0
Then 2PL is conflict serializable?

For option a,there is a shared lock on  B, so how it can write B, therefore wrong option.

For option b, exclusive lock on A is unlocked before commit , therefore wrong option.

For option c, all is correct as exclusive lock on B is removed after commit .

For option d, exclusive lock on B is removed before commit, therfore wrong option.

## Related questions

1
3.6k views
In the Spiral model of software development, the primary determinant in selecting activities in each iteration is Iteration size Cost Adopted process such as Rational Unified Process or Extreme Programming Risk
Consider the $B^{+}$ tree in the adjoining figure, where each node has at most two keys and three links. Now the key $K50$ is deleted from the $B^+$ tree resulting after the two insertions made earlier. Consider the following statements about the $B^+$ tree resulting after ... (i) and (ii) are true Statements (ii) and (iii) are true Statements (iii) and (i) are true All the statements are false
Consider the $B^+$ tree in the adjoining figure, where each node has at most two keys and three links. Keys $K15$ and then $K25$ are inserted into this tree in that order. Exactly how many of the following nodes (disregarding the links) will be present in the tree after the two insertions? $1$ $2$ $3$ $4$