GATE / CS ARCHIVE

GATE CSE 2024 SET 1 / QUESTION 40

Enumerate atomic thread interleavings

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

GATE 2024 Set 1, Q40 2 marks · MCQ
Report issue

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