Skip to content
GATEiit

GATE CSE 2021 Set 2 Q52: Values printed by two processes each running two locked threads

Question

Consider the following multi-threaded code segment (in a mix of C and pseudo-code), invoked by two processes P1 and P2, and each of the processes spawns two threads T1 and T2:

int x = 0;  // global
Lock L1;    // global
main() {
 create a thread to execute foo();  // Thread T1
 create a thread to execute foo();  // Thread T2
 wait for the two threads to finish execution;
 print (x);}

foo() {
 int y = 0;
 Acquire L1;
 x = x + 1;
 y = y + 1;
 Release L1;
 print (y);}

Which of the following statement(s) is/are correct?

Try it: multiple select: one or more correct options

Select one or more options
Select all correct options. No negative marking, no partial marks.
Show answer and explanation

Answer: A, D (official GATE 2021 final answer key)

Concept used: Threads of one process share globals; separate processes have separate address spaces; local variables are per thread.

First step: Within one process, x is incremented once by each thread under the lock; y lives on each thread's own stack.

Full step-by-step solution

Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.

Share this question

WhatsAppDownload image

Source: official GATE 2021 Set 2 CS question paper and final answer key, organised by IIT Bombay. Question paper · Answer key