GateAI
Concurrency and Synchronization

GATE CSE 2023 · Q38

MCQ2 marksMedium

Time

Consider the two functions incr and decr shown below.incr(){ wait(s); X = X+1; signal(s); } decr(){ wait(s); X = X-1; signal(s); }There are 5 threads each invoking incr once, and 3 threads each invoking decr once, on the same shared variable XX. The initial value of XX is 10. Suppose there are two implementations of the semaphore ss, as follows: **I-1:** ss is a binary semaphore initialized to 1. **I-2:** ss is a counting semaphore initialized to 2. Let V1,V2V_1, V_2 be the values of XX at the end of execution of all the threads with implementations **I-1**, **I-2**, respectively. Which one of the following choices corresponds to the minimum possible values of V1,V2V_1, V_2, respectively?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: C) 12, 7

Concurrency and Synchronization

### Implementation I-1 (Binary Semaphore initialized to 1): A binary semaphore initialized to 11 provides strict mutual exclusion for the critical sections. At any point in time, at most one thread can execute inside its critical section.

Since each thread executes atomically with respect to the others:

  • 55 increments add +5+5 to XX.

  • 33 decrements subtract −3-3 from XX.

Final Value V1=10+5−3=12\text{Final Value } V_1 = 10 + 5 - 3 = 12

Because mutual exclusion is guaranteed, no race condition can occur, so the minimum (and only) possible value of V1V_1 is 12.

---

### Implementation I-2 (Counting Semaphore initialized to 2): A counting semaphore initialized to 22 allows up to 22 threads to enter their critical sections concurrently. This enables race conditions where thread updates can be overwritten.

Each operation X=X±1X = X \pm 1 comprises three steps: Read(X), Modify, and Write(X).

To minimize the final value V2V_2, we want all 55 increments to be overwritten while all 33 decrements take effect:

  1. Thread D1D_1 enters the critical section (ss becomes 11), executes Read(X) and gets 1010, computes 10−1=910 - 1 = 9, and is preempted before writing back.

  2. Since s=1s = 1, all 55 incr threads (I1I_1 to I5I_5) execute one by one, updating XX sequentially from 1010 to 1515.

  3. Thread D1D_1 resumes and writes its stored value 99 to XX, completely overwriting all 55 increments.

  4. Threads D2D_2 and D3D_3 execute normally one after another, decrementing XX from 9→8→79 \to 8 \to 7.

Since XX starts at 1010 and there are at most 33 decrement operations, the value of XX cannot decrease below 10−3=710 - 3 = 7.

Thus, the minimum possible value of V2V_2 is 7.

Therefore, the minimum possible values of (V1,V2)(V_1, V_2) are (12, 7), which corresponds to option (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 Q38 of the GATE CSE 2023 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. 9 of them are 2-mark MCQs like this one, and it is the only one in GATE CSE 2023.

How GATE tests Concurrency and Synchronization

More from GATE CSE 2023

Share this solution

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

GateAI solution cardGATE CSE 2023 · Q38 · MCQ

Consider the two functions incr and decr shown below. incr(){ wait(s); X = X+1; signal(s); } decr(){ wait(s); X = X-1; signal(s); } There are 5 threads each invoking incr once, and 3 threads each invoking decr once, on the same shared variable X. The initial value of X is 10. Suppose there are two implementations of the semaphore s, as follows: I-1: s is a binary semaphore initialized to 1. I-2: s is a counting semaphore initialized to 2. Let V1, V2 be the values of X at the end of execution of all the threads with implementations I-1, I-2, respectively. Which one of the following choices corresponds to the minimum possible values of V1, V2, respectively?

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.