GATE / CS ARCHIVE

GATE CSE 2013 / QUESTION 34

Largest raced shared-variable value

Operating Systems · Synchronization & Concurrency · Full GATE CSE 2013 paper

GATE 2013, Q34 2 marks · MCQ
Report issue

Synchronization & Concurrency

Question 34, GATE 2013: Shared x starts 0. W and X each read x, add1 locally, then store; Y and Z each read x, subtract 2 locally, then store. Each process waits on a counting semaphore initially 2 before reading and signals after storing. Each executes once. Maximum final x: A−2, B−1, C1, D2.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Each increment operation adds 1 to a previously read value, while each decrement subtracts 2. A causal chain of reads and writes contains at most the two increment operations, so no value can exceed 2 starting from 0. This is attainable with semaphore capacity 2: W reads 0 and pauses; Y reads 0 and writes−2; Z reads−2 and writes−4; W then writes 1; X reads 1 and writes 2. The capacity restriction is respected throughout. Thus the largest final value is 2, D.

A write stores a locally computed value based on an earlier read, so updates need not add algebraically to−2. With capacity 2, W can occupy one slot while reading 0; the other slot is reused by Y then Z, producing−2 then−4. W’s delayed write overwrites these with 1. After W leaves, X can read 1 and write 2. For the upper bound, trace a written value backward through the reads it depends on. Each increment process can appear at most once in such a causal chain, so no chain adds more than 2; decrements only decrease it. This proves both attainability and optimality.

Original paper, page 7 · Answer key