edited by
12,406 views
48 votes
48 votes
The following two functions $P1$ and $P2$ that share a variable $B$ with an initial value of $2$ execute concurrently.
$$\begin{array}{|l|l|}\hline  \text{P1() \{ } & \text{P2()\{} \\  \text{ C = B - 1;} & \text{ D = 2 * B; } \\  \text{ B = 2 * C;} & \text{ B = D - 1;} \\ \text{\}} & \text{\}} \\\hline \end{array}$$
The number of distinct values that $B$ can possibly take after the execution is______________________.
edited by

6 Answers

1 votes
1 votes

Observe closely, In both P1 and P2 writing to B is happening only at second(final) statement.


Case I: P2 value get's overwritten by P1 $\Rightarrow$ Only P1

It's like in effect only P1 executes. With B=2 as initial value, final value will be 2.

Case II: P1 value gets overwritten by P2 $\Rightarrow$ Only P2

With B=2 as initial value final value will be 3.

Case III: P2 executes first and writes the value which is read by P1 $\Rightarrow$ P2 then P1

P2 writes 3 as in Case II. With B=3 P1 writes final value 4.

Case IV: P1 executes first and writes the value which is read by P2 $\Rightarrow$ P1 then P2

P1 writes value 2 as in Case I which is same as initial value. So as in Case II P2 writes final value 3 with initial value 2.


So in effect there are only 3 possible values which are {2,3,4}. Answer: 3

0 votes
0 votes
Since no while loops are present, only 2 values are taken by B.

P1- P2 : 3

P2 - P1: 4

Answer: 2
Answer:

Related questions

31 votes
31 votes
2 answers
4