50 50 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 P_1: & P_2: \\\hline \text{While true do \{} & \text{While true do \{} \\ L_1:\dots\dots & L_3:\dots\dots \\ L_2:\dots\dots & L_4:\dots\dots \\ \text{X = X + 1;} & \text{Y = Y + 1;} \\ Y = Y - 1; & X = Y - 1; \\ V(S_X); & V(S_Y); \\ V(S_Y); & 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)$ Operating System gatecse-2004 operating-system process-synchronization normal + – Kathleen 19.5k views answer comment Share Follow Print See 1 comment 1 1 comment reply rawan commented Jan 8, 2019 reply Follow flag Look at the first locks of both processes (L1 and L3). If both hold different locks, that is, L1 holds Sx and L3 holds Sy (or vice versa), there will always be deadlock. 2 2 replyShare Please log in or register to add a comment.
Best answer 48 48 votes 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. 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. $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. $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 minal answered Nov 8, 2015 • edited Jul 4, 2018 by kenzou minal comment Share Follow See all 9 Comments 9 9 Comments reply Show 6 previous comments LiteYagami commented Feb 2, 2024 reply Follow flag E) Sy,Sx,Sy,Sx is also another valid option, although not in options 1 1 replyShare moh_haris commented Nov 4, 2024 reply Follow flag one more option if given P(Sy) P(Sx) P(Sy) P(Sx) is correct or not 3 3 replyShare Meet Boda commented Jan 8, 2025 reply Follow flag @moh_haris Yes it would also be correct 0 0 replyShare Please log in or register to add a comment.
21 21 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. dd answered Jan 4, 2017 dd comment Share Follow See all 8 Comments 8 8 Comments reply Show 5 previous comments Wanted commented Jan 16, 2017 reply Follow flag @debashidh in option A there may b not deadlock . 0 0 replyShare dd commented Jan 16, 2017 reply Follow flag QS asks to avoid options having non zero probability of deadlock. Qs does not ask which option may have deadlock. 0 0 replyShare chandan sahu commented Dec 17, 2018 i edited by chandan sahu Dec 17, 2018 reply Follow flag well 0 0 replyShare Please log in or register to add a comment.
14 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 .. Digvijay Pandey answered May 4, 2015 • edited May 19, 2015 by Digvijay Pandey Digvijay Pandey comment Share Follow 0 reply Please log in or register to add a comment.
2 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. tusharp answered Oct 26, 2018 tusharp comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote Option DP1 P2down Sx down Sx (here Sx becomes -1)down Sy down Sy (here Sy becomes -1)after that P1 can continue execute and up both Sx & Sy js__ answered Oct 18, 2025 js__ comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes Video Explanation by Vishhwadeep Gothi sir ---https://youtu.be/2yIqMvQtYzY?list=PLG9aCp4uE-s3klreqEhbzOBQDg5Ha0U38&t=1373 ashish_ranjan 1 answered Jul 1 ashish_ranjan 1 comment Share Follow 0 reply Please log in or register to add a comment.