• edited by
24,006 views
68 68 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______________________.

7 Answers

0 0 votes

    The 4 Quick Math Tracks

  1. Track 1 ($P_1 \rightarrow P_2$ Completely):
    • $P_1$ finishes: $C = 2 - 1 = 1 \rightarrow B = 2 \times 1 = 2$.
    • $P_2$ takes over with $B = 2$: $D = 2 \times 2 = 4 \rightarrow \mathbf{B = 3}$.
  2. Track 2 ($P_2 \rightarrow P_1$ Completely):
    • $P_2$ finishes: $D = 2 \times 2 = 4 \rightarrow B = 4 - 1 = 3$.
    • $P_1$ takes over with $B = 3$: $C = 3 - 1 = 2 \rightarrow \mathbf{B = 4}$.
  3. Track 3 ($P_1$ Starts $\rightarrow P_2$ Finishes $\rightarrow P_1$ Overwrites):
    • $P_1$ copies $B=2$ to compute $C=1$ and pauses.
    • $P_2$ runs completely and changes $B$ to $3$.
    • $P_1$ wakes up, ignores the change, and applies its cached $C=1$: $B = 2 \times 1 = \mathbf{2}$.
  4. Track 4 ($P_2$ Starts $\rightarrow P_1$ Finishes $\rightarrow P_2$ Overwrites):
    • $P_2$ copies $B=2$ to compute $D=4$ and pauses.
    • $P_1$ runs completely and keeps $B$ at $2$.
    • $P_2$ wakes up and applies its cached $D=4$: $B = 4 - 1 = \mathbf{3}$.

Unique Set: The final values can only be --- {2, 3, 4}.

 


 

 

Answer:
Position:
Show:

Related questions

79 79 votes
11 answers 11 answers
34.4k
34.4k views
Misbah Ghaya asked Feb 13, 2015
34,380 views
Consider a disk pack with a seek time of $4$ milliseconds and rotational speed of $10000$ rotations per minute (RPM). It has $600$ sectors per track and each sector can s...
180 180 votes
17 answers 17 answers
66.6k
66.6k views
Misbah Ghaya asked Feb 13, 2015
66,644 views
Consider a uniprocessor system executing three tasks $T_{1}, T_{2}$ and $T_{3}$ each of which is composed of an infinite sequence of jobs (or instances) which arrive peri...
56 56 votes
7 answers 7 answers
42.9k
42.9k views
Misbah Ghaya asked Feb 13, 2015
42,927 views
Suppose the following disk request sequence (track numbers) for a disk with $100$ tracks is given:$45, 20, 90, 10, 50, 60, 80, 25, 70.$Assume that the initial position of...
39 39 votes
2 answers 2 answers
24.8k
24.8k views
Misbah Ghaya asked Feb 13, 2015
24,787 views
Consider a main memory with five-page frames and the following sequence of page references: $\text{3, 8, 2, 3, 9, 1, 6, 3, 8, 9, 3, 6, 2, 1, 3}$. Which one of the followi...