GATE / CS ARCHIVE

GATE CSE 2015 SET 1 / QUESTION 30

GATE CSE 2015 Set 1 Q30

Operating Systems · Synchronization & Concurrency · Full GATE CSE 2015 Set 1 paper

GATE 2015 Set 1, Q30 1 mark · NAT
Report issue

Synchronization & Concurrency

Question 30, GATE 2015: Shared B starts at 2. Concurrent P1 executes C=B−1; B=2*C. Concurrent P2 executes D=2*B; B=D−1. How many distinct final values can B have? Treat each assignment as atomic.
Tap to enlarge
Answer & solution

Official answer: 3

Name P1's statements a,b and P2's c,d. The six order-preserving interleavings abcd,acbd,acdb,cabd,cadb,cdab end with B values 3,3,2,3,2,4 respectively. C and D retain the values calculated when their own reads occurred; later writes to B do not recompute them. Thus the distinct set is {2,3,4}. This is the exam's interleaving model, not an assertion that unsynchronized shared-variable C code has defined behavior.

Solution: Respecting each process’s assignment order, the possible final values of B are 2, 3, 4. Running P1 then P2 gives 3; P2 then P1 gives 4; reading both old B values before either write and leaving P1’s write last gives 2. Enumerating the six order-preserving interleavings adds no others. Thus three distinct values, with atomic statements and sequential consistency.

Original paper, page 12 · Answer key