Project ForgeSign up free
Operating Systems
Process Synchronization
GATE 2015
Expert

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 ____.

3

Explanation

Enumerating all 6 valid interleavings of P1's two statements (read-then-write) with P2's two statements: orderings P1a,P1b,P2a,P2b and P1a,P2a,P1b,P2b and P2a,P1a,P1b,P2b each give final B=3; orderings P1a,P2a,P2b,P1b and P2a,P1a,P2b,P1b each give final B=2; ordering P2a,P2b,P1a,P1b gives final B=4. So B can take exactly 3 distinct values: {2, 3, 4}.

Source: GATE CS 2015 (Session 1 / 7th Feb Shift 1) (2015)

This question is part of a 15-day revision cycle — sign in and Project Forge will bring it back the day before you'd forget it.

Start revising free