in Databases edited by
17,869 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}$
in Databases edited by
17.9k views

4 Comments

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
0
For Strict 2PL, All X_LOCKS() are held till commit in addition to to locks being 2PL.
0
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().

0
0

5 Answers

1 vote
1 vote

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:

Related questions