Consider two processes $P_1$ and $P_2$ accessing the shared variables $X$ and $Y$ protected by two binary semaphores $S_X$ and $S_Y$ respectively, both initialized to 1. $P$ and $V$ denote the usual semaphore operators, where $P$ decrements the semaphore value, and $V$ increments the semaphore value. The pseudo-code of $P_1$ and $P_2$ is as follows: $$\begin{array}{|l|l|}\hline \text{$P_1$:} & \text{$P_2$:} \\\hline \text{While true do \{} & \text{While true do \{} \\ L_1:\ldots\ldots & L_3:\ldots\ldots \\ L_2:\ldots\ldots & L_4:\ldots\ldots \\ \text{X = X + 1;} & \text{Y = Y + 1;} \\ Y=Y-1;&X = Y-1; \\ \text{$V(S_X);$} & \text{$V(S_Y);$} \\ \text{$V(S_Y);$} & \text{$V(S_X);$} \\\}&\} \\\hline \end{array}$$ In order to avoid deadlock, the correct operators at $L_1$, $L_2$, $L_3$ and $L_4$ are respectively.
$P(S_Y), P(S_X); P(S_X), P(S_Y)$
$P(S_X), P(S_Y); P(S_Y), P(S_X)$
$P(S_X), P(S_X); P(S_Y), P(S_Y)$
$P(S_X), P(S_Y); P(S_X), P(S_Y)$
Option (A):
$\Rightarrow$ Deadlock possible
Option (B) . Same as Option (A). Deadlock possible.
Option (C):
No need to think of another process to get a deadlock situation:
$\Rightarrow$ Both processes will be blocked once they start.
Option (D) is ok ! as far as deadlock is concerned.
@debashidh
in option A there may b not deadlock .
64.3k questions
77.9k answers
244k comments
80.0k users