StreakPeaked· Practice

ExamsGATETechnical

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. // code of T1 wait(s1); x = x+1; print(x); wait(s2); signal(s1); // code of T2 wait(s1); x = x+1; print(x); signal(s2); signal(s1); Which of the following outcomes is/are possible when threads T1 and T2 execute concurrently?

  1. T1 runs first and prints 1, T2 runs next and prints 2
  2. T2 runs first and prints 1, T1 runs next and prints 2
  3. T1 runs first and prints 1, T2 does not print anything (deadlock)
  4. T2 runs first and prints 1, T1 does not print anything (deadlock)

Correct answer: T1 runs first and prints 1, T2 does not print anything (deadlock)

Solution

The correct option describes a scenario where T1 successfully increments and prints the value of x before waiting on s2, which is not signaled by T2. Since T2 cannot proceed without the signal from T1, it leads to a deadlock situation where T2 is blocked and cannot print anything.

Related GATE Technical questions

⚔️ Practice GATE Technical free + battle 1v1 →