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
(A) 3
(B) 2
(C) 5
(D) 4
Answer: (A)
Explanation: There are following ways that concurrent processes can follow.
C = B – 1; // C = 1 B = 2*C; // B = 2 D = 2 * B; // D = 4 B = D - 1; // B = 3 C = B – 1; // C = 1 D = 2 * B; // D = 4 B = D - 1; // B = 3 B = 2*C; // B = 2 C = B – 1; // C = 1 D = 2 * B; // D = 4 B = 2*C; // B = 2 B = D - 1; // B = 3 D = 2 * B; // D = 4 C = B – 1; // C = 1 B = 2*C; // B = 2 B = D - 1; // B = 3 D = 2 * B; // D = 4 B = D - 1; // B = 3 C = B – 1; // C = 2 B = 2*C; // B = 4
There are 3 different possible values of B: 2, 3 and 4.
Quiz of this Question