GATE / CS ARCHIVE

SIMILAR QUESTIONS / OPERATING SYSTEMS

Bound final values under semaphore-limited interleavings

2 questions solved the same way, newest first. Back to Operating Systems

How these are solved

Separate each non-atomic read/modify/write into events, respect semaphore occupancy, and construct an interleaving attaining the requested extreme. Initial values, update counts and extrema differ;

2 marks · MCQ
Report issue

Synchronization & Concurrency

Question 38, GATE 2023: Shared X starts at 10. Five threads each do wait(s); X=X+1; signal(s), and three do wait(s); X=X−1; signal(s). The updates are non-atomic read/modify/write operations. Find minimum final values V1,V2 for a binary semaphore initially 1 and a counting semaphore initially 2. A 15,7; B 7,7; C 12,7; D 12,8.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

With binary semaphore1 the updates are mutually exclusive: V1=10+5−3=12. With counting semaphore2, two updates may overlap. For a witness to V2=7, let one decrement enter and read 10, then pause before its write. Through the second permit run all five increments to completion. Resume the held decrement to overwrite X with 9, then run the other two decrements serially, giving 8 then 7. No value below 7 can arise because a value’s chain of read/write dependencies starts at 10 and contains at most the three decrement operations; increments only increase it. Thus12, 7, option C, under the intended non-atomic read-modify-write interleaving model.

Teaching detail. A semaphore with capacity two limits concurrency but does not make each arithmetic assignment atomic. Two threads can read the same old value and later overwrite each other's results. The witness yielding 7 uses only two simultaneous entrants at any moment: one delayed decrement occupies one permit while all other selected operations use the second. The lower-bound argument follows the ancestry of the final written value, not the total number of writes. Each decrement in that ancestry contributes −1 once at most, and there are only three decrements available.

Original paper, page 39 · Answer key

2 marks · MCQ
Report issue

Largest raced shared-variable value

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