GATE / CS ARCHIVE

GATE CSE 2021 SET 2 / QUESTION 42

GATE CSE 2021 Set 2 Q42

Operating Systems · Processes, Threads & System Calls · Full GATE CSE 2021 Set 2 paper

GATE 2021 Set 2, Q42 2 marks · MSQ
Report issue

Processes, Threads & System Calls

Question 42, GATE 2021: Two separate processes each execute: global x=0 and global lock L1; main creates two threads running foo, joins both, prints x. foo has local y=0; acquire L1; x=x+1;y=y+1;release L1;print y. A both processes print x=2; B at least one prints x=4; C some thread prints y=2; D all four threads print y=1.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: A; D

Each process has its own address space, hence its own global x and lock. Within one process the lock serializes the two increments, giving x=2 after both threads finish. Each invocation has its own stack-local y, initialized to zero and incremented once, so every thread prints 1. The two processes do not share x merely because it is global. Thus A, D.

Threads share a process's global variables and synchronization objects, but ordinary separate processes do not share those objects without explicit shared-memory setup. Joining both threads ensures main prints after both increments. Each thread's y is an automatic local variable, so the lock is not needed to protect y, though it is needed for the shared x increment within each process.

Original paper, page 79 · Answer key