0 0 votes Consider a multi-threaded program with two threads T1 and T2. The threads share two semaphores: S1 and S2 both initialised to $1$. The threads also share a global variable $x$ (initialized to $0$ ). The threads execute the code shown below.$$\begin{array}{|c|c|}\hline\text{Thread T1:} & \text{Thread T2:} \\\text{wait}(S1); & \text{wait}(S2); \\x = x + 1; & \text{wait}(S1); \\\text{print}(x); & x = x + 2; \\\text{signal}(S1);& \text{print}(x); \\ & \text{signal}(S2); \\\hline\end{array}$$Which of the following outcomes is/are possible when threads execute concurrently?T1 runs first and prints $1$, T2 runs next and prints $3$.T2 runs first and prints $2$, T1 runs next and prints $3$.T1 runs first and prints $1$, T2 does not print anything (deadlock)T2 runs first and prints $2$, T1 does not print anything (deadlock) Operating System goclasses_cs_os_sw3 goclasses goclasses-testseries-2026 operating-system multiple-selects two-marks + – GO Classes 102 views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
0 0 votes Answer: A and D are possible.Given :-S1 and S2 start at 1.T1 only waits/signals S1. It does wait(S1) → x = x+1 → print(1) → signal(S1).T2 does wait(S2) → wait(S1) → x = x+2 → print(x) → signal(S2) — note: T2 never signals S1.Two possibilities :-T1 runs first: T1 acquires S1, does x = 1, prints 1, signals S1. Then T2 can acquire S2 and S1, do x = 3, print 3, signal S2. → Outcome A (1 then 3) is possible.T2 runs first: T2 acquires S2 and S1, does x = 2, prints 2, signals S2 but does not release S1. Now T1 blocks on wait(S1) forever → T1 never prints. → Outcome D (2 then deadlock / T1 prints nothing) is possible.Outcomes B and C are not possible because T2 never signals S1, so T1 cannot run after T2 has acquired S1. Ajay_Joshi answered Oct 29, 2025 Ajay_Joshi comment Share Follow 0 reply Please log in or register to add a comment.