GateAI
Concurrency and Synchronization

GATE CSE 2024 Set 2 · Q46

MSQ2 marksMedium

Time

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?

Select all that applyOne or more correct

Pick every correct option

Solution

Step-by-step verified solution

Official answer: 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)

Concurrency and Synchronization

Let us trace the possible execution orders of the two threads:

  1. Case 1: Thread T2 runs first

    • T2 executes wait(s1): s1 becomes 00.

    • T2 executes x = x + 1: x becomes 11.

    • T2 executes print(x): prints 1.

    • T2 executes signal(s2): s2 becomes 11.

    • T2 executes signal(s1): s1 becomes 11.

    • Now, thread T1 can execute:

      • T1 executes wait(s1): s1 becomes 00.

      • T1 executes x = x + 1: x becomes 22.

      • T1 executes print(x): prints 2.

      • T1 executes wait(s2): s2 was 11, decrements to 00 without blocking.

      • T1 executes signal(s1): s1 becomes 11.

    • Outcome:T2 runs first and prints 1, T1 runs next and prints 2. Thus, (B) is possible.

  2. Case 2: Thread T1 runs first

    • T1 executes wait(s1): s1 becomes 00.

    • T1 executes x = x + 1: x becomes 11.

    • T1 executes print(x): prints 1.

    • T1 executes wait(s2): since s2 is initialized to 00, T1 blocks waiting on s2.

    • Now, T2 tries to run and executes wait(s1): since s1 is 00 (held by T1), T2 blocks waiting on s1.

    • T1 is waiting for s2 (which can only be signaled by T2), and T2 is waiting for s1 (which can only be signaled by T1).

    • Outcome: Both threads are blocked, resulting in a deadlock where T1 has printed 1 and T2 does not print anything. Thus, (C) is possible.

Therefore, the correct options are B and C.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Concurrency and Synchronization previous year question is Q46 of the GATE CSE 2024 Set 2 paper. Find more GATE CSE PYQs on Concurrency and Synchronization in Operating Systems, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Concurrency and Synchronization PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Concurrency and Synchronization in GATE CSE

Concurrency and Synchronization has 18 GATE CSE PYQs across 15 papers from 2012 to 2026. 4 of them are 2-mark MSQs like this one, and it is the only one in GATE CSE 2024 Set 2.

How GATE tests Concurrency and Synchronization

More from GATE CSE 2024 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2024 Set 2 · Q46 · MSQ

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?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.