edited by
11,568 views
31 votes
31 votes

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.

  1. $P(S_Y), P(S_X); P(S_X), P(S_Y)$

  2. $P(S_X), P(S_Y); P(S_Y), P(S_X)$

  3. $P(S_X), P(S_X); P(S_Y), P(S_Y)$

  4. $P(S_X), P(S_Y); P(S_X), P(S_Y)$

edited by

4 Answers

Best answer
41 votes
41 votes
  1. deadlock $p1$ : line$1$|$p2$:line$3$| $p1$: line$2$(block) |$p2$ :line$4$(block)
    So, here $p1$ want $s(x)$ which is held by $p2$ and $p2$ want $s(y$) which is held by $p1$.
    So, its circular wait (hold and wait condition). So. there is deadlock.
     
  2. deadlock $p1$ : line $1$| $p2$ line $3$|$p1$: line $2$(block) |$p2$ : line $4$(block)
    Som here $p1$ wants sy which is held by $p2$ and $p2$ wants $sx$ which is held by $p1$. So its circular wait (hold and wait ) so, deadlock.
     
  3. $p1$ :line $1$|$p2$ : line $3| p2$ line $4$ (block) $|p1$ line $2$ (block) here, $p1$ wants $sx$ and $p2$ wants $sy$, but both will not be release by its process $p1$ and $p2$ because there is no way to release them. So, stuck in deadlock.
     
  4. $p1$ :line $1$ $|p2$ : line $3$ (block because need sx ) $|p1$ line $2 |p2$ : still block $|p1$: execute cs then up the value of sx |p2 :line 3 line $4$(block need $sy$)$| p1$ up the$ sy$ $|p2$ :lin$4$ $4$ and easily get $cs$.
    We can start from $p2$ also, as I answered according only $p1$, but we get same answer.
    So, option (D) is correct 
edited by
17 votes
17 votes

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.


14 votes
14 votes
let execute P1 till L1 and then execute P2 till L3..
now check options.
Option A,B,C results deadlock.
even Option C always deadlocked..

Option D works fine ..
edited by
2 votes
2 votes
Deadlock is possible when there is no order defined how resources will be consumed by processes. Therefore graph based protocol is free from deadlock as every process try checking from root and if first resource is occupied then the process is blocked and the process which has locked it first completes its execution. After checking the option you can see both processes are consuming resources in an order in option d. Sx followed by Sy which prevents deadlock.
Answer:

Related questions

40 votes
40 votes
6 answers
1
23 votes
23 votes
3 answers
2
Kathleen asked Sep 18, 2014
12,470 views
Consider the following set of processes, with the arrival times and the CPU-burst times gives in milliseconds.$$\small \begin{array}{|c|c|c|} \hline \textbf{Process} & \t...
66 votes
66 votes
9 answers
3
Kathleen asked Sep 18, 2014
23,663 views
The minimum number of page frames that must be allocated to a running process in a virtual memory environment is determined bythe instruction set architecturepage sizenum...