GATE / CS ARCHIVE

SIMILAR QUESTIONS / OPERATING SYSTEMS

Enumerate order preserving atomic interleavings

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

How these are solved

Two threads each perform two atomic assignments; enumerate order-preserving shuffles and collect terminal shared values. 2015 uses local temporaries and asks number of B values;

2 marks · MCQ
Report issue

Enumerate atomic thread interleavings

Synchronization & Concurrency

Question 40, GATE 2024: Initially a=b=1. Thread T1 performs a=a+1 followed by b=b+1. Thread T2 performs b=2*b followed by a=2*a. Each assignment is atomic, but threads may interleave. Which set lists every possible final (a,b)? A {(4,4),(3,3),(4,3)}; B {(3,4),(4,3),(3,3)}; C {(4,4),(4,3),(3,4)}; D {(2,2),(2,3),(3,4)}.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Approach: Preserve each thread's internal order while allowing every merge of the two two-statement sequences.
1. Label T1's statements A: increment a, B: increment b; label T2's statements C: double b, D: double a. Legal interleavings satisfy A before B and C before D.
2. There are six such orders: ABCD gives (4,4); ACBD gives (4,3); ACDB gives (4,3); CABD gives (4,3); CADB gives (4,3); CDAB gives (3,3). For example, ABCD increments each initial 1 to 2 before doubling it to 4.
3. Removing duplicates leaves {(4,4),(4,3),(3,3)}.
4. To see why (3,4) is impossible, a=3 would require D before A, while b=4 would require B before C. Together with the thread orders this demands C<D<A<B<C, an impossible cycle.
Answer: A. Atomic assignments prevent lost updates inside an assignment, but do not force a whole thread to run as one unit.

Original paper, page 26 · Answer key

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