The GATE Grind

GATE 2021 CS (CS2) – Question 52

Operating System · System Calls, Processes, Threads and IPC · 2 marks · Multiple select

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);
  1. Both processes print x=2
  2. At least one process prints x=4
  3. At least one thread prints y=2
  4. All four threads print y=1

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.

More System Calls, Processes, Threads and IPC questions

All System Calls, Processes, Threads and IPC questions →