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?
- $\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}$
- $\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}$
- $\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}$
- $\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}$