Question:

Consider a multi-threaded program with two threads \(T1\) and \(T2\). The threads share two semaphores: \(s1\) (initialized to 1) and \(s2\) (initialized to 0). The threads also share a global variable \(x\) (initialized to 0). The threads execute the code shown below. \begin{verbatim} // code of T1 // code of T2 wait(s1); wait(s1); x = x+1; x = x+1; print(x); print(x); wait(s2); signal(s2); signal(s1); signal(s1); \end{verbatim} Which of the following outcomes is/are possible when threads \(T1\) and \(T2\) execute concurrently?

Show Hint

Analyze semaphore dependencies to identify potential deadlocks and permissible thread execution orders.
Updated On: Jan 23, 2025
  • \(T1\) runs first and prints 1, \(T2\) runs next and prints 2
  • \(T2\) runs first and prints 1, \(T1\) runs next and prints 2
  • \(T1\) runs first and prints 1, \(T2\) does not print anything (deadlock)
  • \(T2\) runs first and prints 1, \(T1\) does not print anything (deadlock)
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

Outcome (A): This is not possible because \(T2\) cannot proceed after \(T1\) without acquiring \(s1\), but \(s1\) is released only when \(T1\) completes. Thus, (A) is incorrect.
Outcome (B): \(T2\) can run first, acquire \(s1\), increment \(x\), and print \(1\). Then \(T1\) acquires \(s1\), increments \(x\), and prints \(2\). This outcome is possible.
Outcome (C): \(T1\) acquires \(s1\), increments \(x\), and prints \(1\). However, it waits on \(s2\), which is never signaled by \(T2\), causing a deadlock. This outcome is possible.
Outcome (D): This is not possible because \(T2\) signals \(s2\) after printing, allowing \(T1\) to proceed. Thus, (D) is incorrect. Final Answer: \[ \boxed{\text{(B), (C)}} \]
Was this answer helpful?
0
0

Top Questions on Lexical analysis

View More Questions

Questions Asked in GATE CS exam

View More Questions