• S
    SwaseekhGATE Preparation
General
  • Dashboard
  • Syllabus
  • Questions
  • Aptitude
  • Mock Tests
  • TCS NQT 2026
Account
  • Pricing
  • Contact
  1. GATE CS
  2. PYQs
  3. Operating System

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? A. T1 runs first and prints 1, T2 runs next and prints 2 B. T2 runs first and prints 1, T1 runs next and prints 2 C. T1 runs first and prints 1, T2 does not print anything (deadlock) D. T2 runs first and prints 1, T1 does not print anything (deadlock)

GATE 2024 · Operating System · Process Synchronization · medium

Answer: Option B: T2 runs first and prints 1, T1 runs next and prints 2 (and also C: T1 runs first prints 1, deadlock prevents T2 from printing). The primary possible correct completion outcome is B.

  1. Trace Scenario A: T1 runs first, prints 1; T2 runs next, prints 2: T1 calls wait(s1): s1=1->0, succeeds. T1 sets x=1, prints 1. T1 calls wait(s2): s2=0, T1 BLOCKS. Now T2 calls wait(s1): s1=0, T2 BLOCKS. Both threads are blocked: T1 waits for s2 (only T2 can signal it), T2 waits for s1 (only T1 can signal it after getting s2). This is a DEADLOCK. Scenario A is IMPOSSIBLE.
  2. Trace Scenario B: T2 runs first, prints 1; T1 runs next, prints 2: T2 calls wait(s1): s1=1->0, succeeds. T2: x=1, prints 1. T2: signal(s2): s2=0->1. T2: signal(s1): s1=0->1. Now T1 can run: wait(s1): s1=1->0, succeeds. T1: x=2, prints 2. T1: wait(s2): s2=1->0, succeeds. T1: signal(s1): s1=0->1. Both threads complete normally. Scenario B IS POSSIBLE.
  3. Trace Scenario C: T1 first prints 1, T2 does not print (deadlock): As analyzed in Step 1, if T1 runs first, T1 blocks on wait(s2) and T2 blocks on wait(s1). T1 has already printed 1. T2 never gets to print. So 'T1 prints 1, T2 does not print' IS a state we reach — but this is a deadlock where neither completes. The question asks which outcomes are possible. C describes T1 prints 1 and deadlock occurs (T2 never prints). This IS reachable/possible.
  4. Trace Scenario D: T2 first prints 1, T1 does not print (deadlock): If T2 runs first: T2 acquires s1, prints 1, signals s2 (s2=1), signals s1 (s1=1). After T2 finishes its critical section, s1=1 and s2=1. T1 will call wait(s1) which succeeds (s1=1->0). T1 prints 2. T1 calls wait(s2): s2=1->0, succeeds. T1 signals s1. No deadlock possible if T2 goes first. Scenario D is IMPOSSIBLE.
  5. Determine correct answer: Possible outcomes: B (T2 first, both complete normally printing 1 then 2) and C (T1 first, T1 prints 1, deadlock — T2 never prints). The question asks which outcomes 'is/are possible'. Both B and C are possible. However, looking at the options as an MCQ with single correct answer — if only one answer is expected, the image shows this as a standard MCQ. Option B is a valid normal execution. Option C is also valid (deadlock scenario). Since the question shows options A, B, C, D and asks 'which outcomes is/are possible', and the answer key for GATE 2024 indicates B and C are correct. But for a single MCQ, checking: B describes a normal completion which is definitely possible. C describes a deadlock which is also possible.