GateAI
Concurrency and Synchronization

GATE CSE 2021 Set 2 · Q42

MSQ2 marksMedium

Time

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?

Select all that applyOne or more correct

Pick every correct option

Solution

Step-by-step verified solution

Official answer: A) Both P1 and P2 will print the value of x as 2.; D) Both T1 and T2, in both the processes, will print the value of y as 1.

Concurrency and Synchronization
  1. Process Isolation: Processes P1P_1 and P2P_2 run in separate address spaces. Therefore, each process has its own independent copy of the global variables x and lock L1. The execution of threads in P1P_1 does not affect the address space or variables of P2P_2.

  2. Execution within each Process:

    • In each process, the global variable x is initialized to 00.

    • Two threads, T1T_1 and T2T_2, are spawned to execute foo().

    • Within foo(), the increment x = x + 1 is enclosed within a critical section protected by Acquire L1 and Release L1. This guarantees mutual exclusion and prevents race conditions.

    • Since both threads increment x exactly once, the final value of x in each process is 0+1+1=20 + 1 + 1 = 2.

    • Therefore, both P1P_1 and P2P_2 will print the value of x as 2 (making statement (A) correct and statement (B) incorrect).

  3. Local Variable y:

    • The variable y is declared as a local variable inside foo(), meaning it is allocated on each thread's individual private stack.

    • Each thread initializes its own y = 0, increments it to 11, and then prints y.

    • Thus, every thread (T1T_1 and T2T_2) in both processes (P1P_1 and P2P_2) will independently print y=1y = 1.

    • Therefore, statement (D) is correct and statement (C) is incorrect.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Concurrency and Synchronization previous year question is Q42 of the GATE CSE 2021 Set 2 paper. Find more GATE CSE PYQs on Concurrency and Synchronization in Operating Systems, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Concurrency and Synchronization PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Concurrency and Synchronization in GATE CSE

Concurrency and Synchronization has 18 GATE CSE PYQs across 15 papers from 2012 to 2026. 4 of them are 2-mark MSQs like this one, and it is the only one in GATE CSE 2021 Set 2.

How GATE tests Concurrency and Synchronization

More from GATE CSE 2021 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2021 Set 2 · Q42 · MSQ

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?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.