GATE 2024 CS (CS1) – Question 40
Two threads T1 (a=a+1; b=b+1;) and T2 (b=2*b; a=2*a;) update shared variables a and b, initially a=b=1. Each statement is atomic. Which option lists all possible combinations of a and b after both finish?
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (C) (a=4,b=4); (a=4,b=3); (a=3,b=4)
Explanation
Enumerating interleavings: T1 then T2 gives (a=4,b=4); T2 then T1 gives (a=3,b=3)... exploring all 6 interleavings yields outcomes (4,4), (4,3), (3,4) only, matching option C.