GATE CS 2024 Set 1 — Question 40

MCQ+2 / -0.67MediumRace Conditions & Critical SectionProcess SynchronizationOperating System

Operating System → Process Synchronization → Race Conditions & Critical Section

Last updated

Question

Consider the following two threads T1 and T2 that update two shared variables a and b. Assume that initially a = b = 1. Though context switching between threads can happen at any time, each statement of T1 or T2 is executed atomically without interruption.
T1T2
a = a + 1;b = 2 * b;
b = b + 1;a = 2 * a;
Which one of the following options lists all the possible combinations of values of a and b after both T1 and T2 finish execution?
A.
(a = 4, b = 4); (a = 3, b = 3); (a = 4, b = 3)
B.
(a = 3, b = 4); (a = 4, b = 3); (a = 3, b = 3)
C.
(a = 4, b = 4); (a = 4, b = 3); (a = 3, b = 4)
D.
(a = 2, b = 2); (a = 2, b = 3); (a = 3, b = 4)

Correct answer

(A) (a = 4, b = 4); (a = 3, b = 3); (a = 4, b = 3)

Solution

Let the statements be:
T1:
S1:a=a+1S_1: a = a + 1
S2:b=b+1S_2: b = b + 1
Constraint: S1S_1 must execute before S2S_2.
T2:
S3:b=2bS_3: b = 2 * b
S4:a=2aS_4: a = 2 * a
Constraint: S3S_3 must execute before S4S_4.
Initially a=1,b=1a=1, b=1.
Analysis of variable aa:
Operations on aa are S1(+1)S_1 (+1) and S4(2)S_4 (*2).
  • If order is S1,S4S_1, S_4: 1+12241 \xrightarrow{+1} 2 \xrightarrow{*2} 4. (a=4a=4)
  • If order is S4,S1S_4, S_1: 122+131 \xrightarrow{*2} 2 \xrightarrow{+1} 3. (a=3a=3)

So a{3,4}a \in \{3, 4\}.
Analysis of variable bb:
Operations on bb are S2(+1)S_2 (+1) and S3(2)S_3 (*2).
  • If order is S2,S3S_2, S_3: 1+12241 \xrightarrow{+1} 2 \xrightarrow{*2} 4. (b=4b=4)
  • If order is S3,S2S_3, S_2: 122+131 \xrightarrow{*2} 2 \xrightarrow{+1} 3. (b=3b=3)

So b{3,4}b \in \{3, 4\}.
Valid Interleavings:
We must respect thread order (S1<S2S_1 < S_2 and S3<S4S_3 < S_4).
Total permutations of 4 instructions is 4!2!2!=6\frac{4!}{2!2!} = 6.
1.S1,S2,S3,S4S_1, S_2, S_3, S_4: a(S1,S4)4a(S_1, S_4) \to 4, b(S2,S3)4b(S_2, S_3) \to 4. Result: (4,4)(4, 4).
2.S1,S3,S2,S4S_1, S_3, S_2, S_4: a(S1,S4)4a(S_1, S_4) \to 4, b(S3,S2)3b(S_3, S_2) \to 3. Result: (4,3)(4, 3).
3.S1,S3,S4,S2S_1, S_3, S_4, S_2: a(S1,S4)4a(S_1, S_4) \to 4, b(S3,S2)3b(S_3, S_2) \to 3. Result: (4,3)(4, 3).
4.S3,S1,S2,S4S_3, S_1, S_2, S_4: a(S1,S4)4a(S_1, S_4) \to 4, b(S3,S2)3b(S_3, S_2) \to 3. Result: (4,3)(4, 3).
5.S3,S1,S4,S2S_3, S_1, S_4, S_2: a(S1,S4)4a(S_1, S_4) \to 4, b(S3,S2)3b(S_3, S_2) \to 3. Result: (4,3)(4, 3).
6.S3,S4,S1,S2S_3, S_4, S_1, S_2: a(S4,S1)3a(S_4, S_1) \to 3, b(S3,S2)3b(S_3, S_2) \to 3. Result: (3,3)(3, 3).
Possible outcomes: {(4,4),(4,3),(3,3)}\{(4, 4), (4, 3), (3, 3)\}.
Note: The outcome (3,4)(3, 4) requires S4<S1S_4 < S_1 (for a=3a=3) and S2<S3S_2 < S_3 (for b=4b=4).
Combined with thread constraints S1<S2S_1 < S_2 and S3<S4S_3 < S_4, we get cycle: S4<S1<S2<S3<S4S_4 < S_1 < S_2 < S_3 < S_4. Impossible.
Thus, the set of possible values is {(a=4,b=4),(a=3,b=3),(a=4,b=3)}\{(a=4, b=4), (a=3, b=3), (a=4, b=3)\}.

More questions on Process Synchronization

Practice GATE CS PYQs with adaptive difficulty

Timed practice, skill tracking, and AI explanations — free to start.

Start practicing free