GateAI
Concurrency and Synchronization

GATE CSE 2015 Set 1 · Q9

NAT1 markMedium

Time

The following two functions P1 and P2 that share a variable B with an initial value of 2 execute concurrently.P1 ( ) { C = B - 1; B = 2 * C; }P2 ( ) { D = 2 * B; B = D - 1; }The number of distinct values that B can possibly take after the execution is ___________.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 3

Concurrency and Synchronization

Let the statements in P1P1 be:

  • I1:C=B−1I_1: C = B - 1

  • I2:B=2×CI_2: B = 2 \times C

Let the statements in P2P2 be:

  • I3:D=2×BI_3: D = 2 \times B

  • I4:B=D−1I_4: B = D - 1

Initially, B=2B = 2.

Let's analyze all possible valid interleavings of the statements:

  1. P1P1 executes completely, then P2P2 executes (I1→I2→I3→I4I_1 \to I_2 \to I_3 \to I_4):

    • I1:C=2−1=1I_1: C = 2 - 1 = 1

    • I2:B=2×1=2I_2: B = 2 \times 1 = 2

    • I3:D=2×2=4I_3: D = 2 \times 2 = 4

    • I4:B=4−1=3I_4: B = 4 - 1 = 3

    • Final B=3B = 3

  2. P2P2 executes completely, then P1P1 executes (I3→I4→I1→I2I_3 \to I_4 \to I_1 \to I_2):

    • I3:D=2×2=4I_3: D = 2 \times 2 = 4

    • I4:B=4−1=3I_4: B = 4 - 1 = 3

    • I1:C=3−1=2I_1: C = 3 - 1 = 2

    • I2:B=2×2=4I_2: B = 2 \times 2 = 4

    • Final B=4B = 4

  3. Interleaved execution with P1P1 writing last (I1→I3→I4→I2I_1 \to I_3 \to I_4 \to I_2 or I3→I1→I4→I2I_3 \to I_1 \to I_4 \to I_2):

    • I1I_1 reads initial B=2B=2, so C=2−1=1C = 2 - 1 = 1

    • P2P2 executes (I3I_3 and I4I_4), modifying BB

    • I2I_2 writes B=2×C=2×1=2B = 2 \times C = 2 \times 1 = 2

    • Final B=2B = 2

  4. Interleaved execution with P2P2 writing last (I3→I1→I2→I4I_3 \to I_1 \to I_2 \to I_4 or I1→I3→I2→I4I_1 \to I_3 \to I_2 \to I_4):

    • I3I_3 reads initial B=2B=2, so D=2×2=4D = 2 \times 2 = 4

    • P1P1 executes (I1I_1 and I2I_2), modifying BB

    • I4I_4 writes B=D−1=4−1=3B = D - 1 = 4 - 1 = 3

    • Final B=3B = 3

Thus, the set of distinct possible final values of BB is {2,3,4}\{2, 3, 4\}.

The number of distinct values is 3.

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 Q9 of the GATE CSE 2015 Set 1 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. This is the only 1-mark NAT question among them, and it is the only one in GATE CSE 2015 Set 1.

How GATE tests Concurrency and Synchronization

More from GATE CSE 2015 Set 1

Share this solution

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

GateAI solution cardGATE CSE 2015 Set 1 · Q9 · NAT

The following two functions P1 and P2 that share a variable B with an initial value of 2 execute concurrently. P1 ( ) { C = B - 1; B = 2 * C; } P2 ( ) { D = 2 * B; B = D - 1; } The number of distinct values that B can possibly take after the execution is …

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.