Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
337 views
in Computer by (96.5k points)
closed by

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

1 Answer

0 votes
by (85.8k points)
selected by
 
Best answer

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.

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...