GATE 2021 CS (CS2) – Question 52
Processes P1,P2 each execute this code with ordinary process-private globals; each creates two threads. Which statements hold?
int x=0; global lock L1;
main: create T1 to run foo; create T2 to run foo;
wait for both; print(x);
foo: local int y=0; acquire L1;
x=x+1; y=y+1; release L1; print(y);Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (A) Both processes print x=2; (D) All four threads print y=1
Explanation
Threads of one process share its global x and lock, so the two protected increments produce x=2. The other process has a separate address space and independently also prints two. Each foo invocation has its own local y initialized to zero and incremented once, so each prints one. Thus A,D.