edited by
17,687 views
53 votes
53 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}$
edited by

5 Answers

Best answer
65 votes
65 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
17 votes
17 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.
9 votes
9 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
5 votes
5 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.
Answer:

Related questions

5 votes
5 votes
2 answers
2
Ishrat Jahan asked Oct 29, 2014
6,167 views
In the Spiral model of software development, the primary determinant in selecting activities in each iteration isIteration sizeCostAdopted process such as Rational Unifie...
46 votes
46 votes
5 answers
4