Skip to content
Official PYQ · 2024

GATE CS 2024 Set 2 Q36 — Concurrency and Synchronization

Explain with AI

​​​​​Consider a multi-threaded program with two threads T1\mathrm{T} 1 and T2\mathrm{T} 2. The threads share two semaphores: s1s1 (initialized to 11) and s2s2 (initialized to 00). The threads also share a global variable x\mathrm{x} (initialized to 00). The threads execute the code shown below.

//code of T 1
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\mathrm{T} 1 and T2\mathrm{T} 2 execute concurrently?

Select all correct options
View worked solution

Answer: B;C

To find the possible outcomes, let us trace the execution of threads T1\text{T1} and T2\text{T2} under different scheduling scenarios.

Initial State:
s1=1s1 = 1, s2=0s2 = 0, and x=0x = 0.


Scenario 1: Thread T1\text{T1} starts first

  1. T1\text{T1} executes wait(s1): Since s1=1s1 = 1, it decrements to 00, and T1\text{T1} proceeds.
  2. T1\text{T1} executes x = x + 1: xx becomes 11.
  3. T1\text{T1} executes print(x): Prints 11.
  4. T1\text{T1} executes wait(s2): Since s2=0s2 = 0, T1\text{T1} blocks and goes to the waiting queue of s2s2.
  5. Now, T2\text{T2} attempts to run and executes wait(s1): Since s1=0s1 = 0, T2\text{T2} blocks and goes to the waiting queue of s1s1.

At this point, T1\text{T1} is blocked waiting for s2s2 (which only T2\text{T2} can signal), and T2\text{T2} is blocked waiting for s1s1 (which only T1\text{T1} can signal). This is a deadlock.

Outcome: T1\text{T1} prints 11, and T2\text{T2} does not print anything due to deadlock. (Matches Option C)


Scenario 2: Thread T2\text{T2} starts first

  1. T2\text{T2} executes wait(s1): Since s1=1s1 = 1, it decrements to 00, and T2\text{T2} proceeds.
  2. T2\text{T2} executes x = x + 1: xx becomes 11.
  3. T2\text{T2} executes print(x): Prints 11.
  4. T2\text{T2} executes signal(s2): s2s2 becomes 11.
  5. T2\text{T2} executes signal(s1): s1s1 becomes 11. T2\text{T2} finishes execution.
  6. Now, T1\text{T1} runs and executes wait(s1): Since s1=1s1 = 1, it decrements to 00, and T1\text{T1} proceeds.
  7. T1\text{T1} executes x = x + 1: xx becomes 22.
  8. T1\text{T1} executes print(x): Prints 22.
  9. T1\text{T1} executes wait(s2): Since s2=1s2 = 1, it decrements to 00, and T1\text{T1} proceeds without blocking.
  10. T1\text{T1} executes signal(s1): s1s1 becomes 11. T1\text{T1} finishes execution.

Outcome: T2\text{T2} prints 11, and then T1\text{T1} prints 22. (Matches Option B)


Conclusion

  • Option A is incorrect because if T1\text{T1} runs first, it inevitably blocks on wait(s2) before T2\text{T2} can execute.
  • Option D is incorrect because if T2\text{T2} runs first, it signals both semaphores, allowing T1\text{T1} to complete successfully without any deadlock.

Thus, the correct options are B and C.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Concurrency and Synchronization questions