Skip to main content

GATE CSE 2024Operating System › Process Concepts, Threads & System CallsVerified

GATE CSE 2024 PYQ: ​​​Consider the following two threads T 1 \mathrm{T} 1 T 1 and T 2 \mathrm{T} 2 T 2 that update two shared…

​​​Consider the following two threads T1\mathrm{T} 1 and T2\mathrm{T} 2 that update two shared variables a\mathrm{a} and b\mathrm{b}. Assume that initially a=b=1\mathrm{a}=\mathrm{b}=1. Though context switching between threads can happen at any time, each statement of  T1\mathrm{T} 1 or  T2\mathrm{T} 2 is executed atomically without interruption.

T1a=a+1; b=b+1;\begin{gathered}\mathrm{T} 1 \\ \mathrm{a}=\mathrm{a}+1; \\ \mathrm{~b}=\mathrm{b}+1;\end{gathered} \quad T2b=2∗b;a=2∗a;\begin{gathered}\mathrm{T} 2 \\ \mathrm{b}=\mathrm {2 * b}; \\ \mathrm{a}=\mathrm 2 * {a}; \end{gathered}

Which one of the following options lists all the possible combinations of values of a and b after both T1 and T2 finish execution?

  1. A
    (a=4, b=4);(a=3, b=3);(a=4, b=3)(\mathrm{a}=4, \mathrm{~b}=4) ;(\mathrm{a}=3, \mathrm{~b}=3) ;(\mathrm{a}=4, \mathrm{~b}=3)
    Correct answer
  2. B
    (a=3, b=4);(a=4, b=3);(a=3, b=3)(\mathrm{a}=3, \mathrm{~b}=4) ;(\mathrm{a}=4, \mathrm{~b}=3) ;(\mathrm{a}=3, \mathrm{~b}=3)
  3. C
    (a=4, b=4);(a=4, b=3);(a=3, b=4)(\mathrm{a}=4, \mathrm{~b}=4) ;(\mathrm{a}=4, \mathrm{~b}=3) ;(\mathrm{a}=3, \mathrm{~b}=4)
  4. D
    (a=2, b=2);(a=2, b=3);(a=3, b=4)(\mathrm{a}=2, \mathrm{~b}=2) ;(\mathrm{a}=2, \mathrm{~b}=3) ;(\mathrm{a}=3, \mathrm{~b}=4)

Answer

(A) ( a = 4 , b = 4 ) ; ( a = 3 , b = 3 ) ; ( a = 4 , b = 3 ) (\mathrm{a}=4, \mathrm{~b}=4) ;(\mathrm{a}=3, \mathrm{~b}=3) ;(\mathrm{a}=4, \mathrm{~b}=3) ( a = 4 , b = 4 ) ; ( a = 3 , b = 3 ) ; ( a = 4 , b = 3 )

Approach

Because each statement is atomic and context switches can occur only at statement boundaries, the execution of the four statements is some linear extension of the two per-thread orderings: within T1, a = a+1 must precede b = b+1; within T2, b = 2*b must precede a = 2*a. Enumerate all $4!/(2!\,2!) = 6$ valid interleavings and compute the final $(a,b)$ for each starting from $a=b=1$.

With $a=b=1$ initially and atomic statements, the reachable final states are the six interleavings' results. Only three distinct pairs arise: $(4,4)$ (T1 fully then T2 fully, or $A,B,C,D$), $(3,4)$-free $(4,3)$ (any interleaving in which $C$ precedes $B$, e.g. $T2.1,\ T1.1,\ T1.2,\ T2.2$), and $(3,3)$ (T2 fully then T1 fully). Thus the complete set is $\{(4,4),(3,3),(4,3)\}$, matching option A.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.