The GATE Grind

GATE 2015 CS – Question 30

Operating System · Concurrency and Synchronization · 1 mark · Numerical answer

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

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 3

Explanation

Label the steps a: `C = B - 1`, b: `B = 2 * C`, c: `D = 2 * B`, d: `B = D - 1`. Step a must come before b and step c before d, which gives six interleavings. Running a, b, c, d gives 3, a, c, b, d gives 3, c, a, b, d gives 3, a, c, d, b gives 2, c, a, d, b gives 2, and c, d, a, b gives 4. The distinct final values are 2, 3 and 4, so there are 3.