edited by
28,689 views
72 72 votes

Consider the following two transactions$: T1$ and $T2.$

$\begin{array}{clcl} T1: & \text{read (A);} & T2: & \text{read (B);} \\ & \text{read (B);} & & \text{read (A);} \\  & \text{If}\;A = 0\; \text{then}\; B \leftarrow B + 1;  & &  \text{If}\;B \neq 0\;\text{then}\;A \leftarrow A  – 1; \\ & \text{write (B);} & & \text{write (A);}\\\hline\end{array}$

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

  1. $\begin{array} {c l c l}  S1: & \text{lock S(A);} & S2: & \text{lock S(B);} \\ & \text{read (A);} &  & \text{read (B);} \\ & \text{lock S(B);} & & \text{lock S(A);}  \\ & \text{read (B);} &  & \text{read (A);} \\ & \text{If}\; A = 0 & & \text{If}\; B \neq 0 \\  & \text{then}\; B \leftarrow B + 1; & & \text{then}\; A \leftarrow A - 1; \\ & \text{write (B);} & & \text{write (A);} \\  & \text{commit;} & & \text{commit;} \\ & \text{unlock (A);} & & \text{unlock (B);} \\ & \text{unlock (B);} & & \text{unlock (A);} \\ \hline\end{array}$
  2. $\begin{array} {c l c l}  S1: & \text{lock X(A);} & S2: & \text{lock X(B);} \\ & \text{read (A);} &  & \text{read (B);} \\ & \text{lock X(B);} & & \text{lock X(A);}  \\ & \text{read (B);} &  & \text{read (A);} \\ & \text{If}\; A = 0 & & \text{If}\; B \neq 0 \\  & \text{then}\; B \leftarrow B + 1; & & \text{then}\; A \leftarrow A - 1; \\ & \text{write (B);} & & \text{write (A);} \\ & \text{unlock (A);} & & \text{unlock (A);} \\  & \text{commit;} & & \text{commit;} \\ & \text{unlock (B);} & & \text{unlock (A);} \\ \hline\end{array}$
  3. $\begin{array} {c l c l} S1: & \text{lock S(A);} & S2: & \text{lock S(B);} \\ & \text{read (A);} &  & \text{read (B);} \\ & \text{lock X(B);} & & \text{lock X(A);}  \\ & \text{read (B);} &  & \text{read (A);} \\ & \text{If}\; A = 0 & & \text{If}\; B \neq 0 \\  & \text{then}\; B \leftarrow B + 1; & & \text{then}\; A \leftarrow A - 1; \\ & \text{write (B);} & & \text{write (A);} \\ & \text{unlock (A);} & & \text{unlock (B);} \\  & \text{commit;} & & \text{commit;} \\ & \text{unlock (B);} & & \text{unlock (A);} \\\hline \end{array}$
  4. $\begin{array} {c l c l} S1: & \text{lock S(A);} & S2: & \text{lock S(B);} \\ & \text{read (A);} &  & \text{read (B);} \\ & \text{lock X(B);} & & \text{lock X(A);}  \\ & \text{read (B);} &  & \text{read (A);} \\ & \text{If}\; A = 0 & & \text{If}\; B \neq 0 \\  & \text{then}\; B \leftarrow B + 1; & & \text{then}\; A \leftarrow A - 1; \\ & \text{write (B);} & & \text{write (A);} \\ & \text{unlock (A);} & & \text{unlock (A);}  \\ & \text{unlock (B);} & & \text{unlock (A);} \\  & \text{commit;} & & \text{commit;} \\ \hline\end{array}$

8 Answers

Best answer
90 90 votes

Answer is (C).

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 by
24 24 votes

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)

C) is the answer.

  •  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.
10 10 votes

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

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

edited by
7 7 votes
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.
3 3 votes

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.

 

Answer:
Position:
Show:

Related questions

7 7 votes
2 answers 2 answers
7.3k
7.3k views
Ishrat Jahan asked Oct 29, 2014
7,293 views
In the Spiral model of software development, the primary determinant in selecting activities in each iteration isIteration sizeCostAdopted process such as Rational Unifie...
0 0 votes
1 answers 1 answer
589
589 views
ParthPratim asked Jan 6, 2023
589 views
With reference to the GATE IT 2007 | Question : 66In the following article from GeeksForGeeks : Lock Compatibility DiagramIt can be clearly seen that if a transaction Ti ...
61 61 votes
5 answers 5 answers
35.1k
35.1k views
Ishrat Jahan asked Oct 30, 2014
35,052 views
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 orde...
67 67 votes
5 answers 5 answers
25.2k
25.2k views
Ishrat Jahan asked Oct 30, 2014
25,241 views
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....