# The following two functions P1 and P2 that share a variable B with an initial value of 2 execute concurrently. P1 ( ) { P2 ( )

243 views
in Computer
closed

The following two functions P1 and P2 that share a variable B with an initial value of 2 execute concurrently.

 P1 ( ) { P2 ( ) { C = B – 1; D = 2 * B; B = 2 * C; B = D – 1; } }
The number of distinct values that B can possibly take after the execution is __________.

by (25.6k points)
selected by

Initial value of B is 2 //value stored in main memory

Value of B = 4

First execute P2()

P2 ( ) {

D = 2 * B;   // 2 × 2 = 4

B = D – 1; // B = 4 – 1 = 3

}

write the value of B = 3 to memory

execute P1()

P1 ( ) {

C = B – 1; // C = 3 – 1 = 2

B = 2 * C; // B = 2 × 2 = 4

}

write the value of B = 4 is memory.

Distinct value of B is 4.

Value of B =3

First Execute P1()

P1 ( ) {

C = B – 1; // C = 2 – 1 = 1

B = 2 * C; // B = 2 × 1 =2

}

write the value of B = 2 to memory

then execute P2()

P2 ( ) {

D = 2 * B;   // 2 × 2 = 4

B = D – 1; // B = 4 – 1 = 3

}

write the value of B = 3 to memory

One of the distinct values of B is 3.

Value of B = 2

First execute P2()

P2 ( ) {

D = 2 * B;   // 2 × 2 = 4

B = D – 1; // B = 4 – 1 = 3

}

//don’t write to memory

execute P1()  // with initial value of B = 2

P1 ( ) {

C = B – 1; // C = 2 – 1 = 1

B = 2 * C; // B = 2 × 1 = 2

}

write the value B = 3 by P2() in memory.

overwrite the value written by P2() as B = 2 by P1() in memory.

One of the distinct values of B is 2.

Therefore, the distinct values of B are 2, 3 and 4.

The number of distinct values that B can possibly take after the execution is 3.