IAE Academy

GATE 2023 CS Q.38: solution and answer

GATE 2023 · CSQ.38 · 2 marks · 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?

  1. (A)

    15, 7

  2. (B)

    7, 7

  3. (C)

    12, 7

  4. (D)

    12, 8

See the question in the paper

Answer and solution

ANSWER

The minimum possible values are V1 = 12 and V2 = 7, option (C).

Official answer: CSource: Official answer key, GATE 2023 CS (IIT Kanpur)

  1. I-1 (binary semaphore, initial 1). Only one thread can be between wait(s) and signal(s) at a time, so each update of X is atomic and no update is lost.

  2. Every incr adds 1 and every decr subtracts 1, in any order, so . It is the same in every execution, so the minimum is .

  3. I-2 (counting semaphore, initial 2). Two threads can be inside the critical section together, so two read-modify-write sequences can overlap and an update can be overwritten (lost). A thread always writes the value it read, plus 1 (incr) or minus 1 (decr).

  4. Lower bound. Let be the number of decr writes completed so far. Claim: at all times. Initially and . An incr writes , where is an earlier value of , and , so the claim still holds. A decr writes with , and rises by 1, so . With only 3 decrements, , so the final value is at least .

  5. A schedule that reaches 7. Decr thread takes one permit, reads , and stalls inside the critical section.

  6. The other permit is free for one thread at a time, so the 5 incr threads run one after another: .

  7. resumes and writes , overwriting all five increments, so .

  8. The two remaining decr threads run one after another: . (If they overlapped and both read 9, the result would be 8, which is not smaller.)

  9. So is reachable and cannot be beaten, and the minimum pair is , option (C).

KEY IDEA

A binary semaphore initialised to 1 gives mutual exclusion, so there are no lost updates. A counting semaphore initialised to 2 lets two threads overlap, so one thread's stale write can wipe out many increments. The value can never fall below , because only decrements lower it, and each can lower it by at most 1 from some value that was itself at least that high.

WATCH OUT FOR

Taking 7 for V1 as well, as if the binary semaphore also allowed lost updates. Another slip is thinking the counting semaphore lets the decrements stack to only without losing increments, or forgetting that a stalled decr can overwrite the increments.

From the GATE 2023 CS paper. Every question of this paper · Download the full paper · All 7 Synchronisation questions, with notes

More Synchronisation questions