121k views
2 votes
The following pair of processes share a common variable X :

Process A
int Y;
A1: Y = X*2;
A2: X = Y;

Process B
int Z;
B1: Z = X+1;
B2: X = Z;

X is set to 5 before either process begins execution. As usual, statements within a process are executed sequentially, but statements in process A may execute in any order with respect to statements in process B.
i) How many different values of X are possible after both processes finish executing ?
a) two
b) three
c) four
d) eight

User Philask
by
8.8k points

1 Answer

5 votes

Final answer:

After analyzing the possible interleavings of both processes, there are four possible outcomes for the shared variable X. The concurrent processes lead to variations in the final value due to the different execution orders. Option C is the correct answer.

Step-by-step explanation:

We are considering the possible values of a shared variable X after the execution of two concurrent processes. Process A and Process B are both altering the value of X, which is initially set to 5. The operations within each process occur sequentially but may interleave in any order with operations from the other process.

The sequence of operations in Process A and Process B is:

  • A1: Y = X*2
  • A2: X = Y
  • B1: Z = X+1
  • B2: X = Z

Considering all the different interleavings, the possible final values of X are:

  1. X = 10, if A executes fully before B starts
  2. X = 12, if A1, then B, then A2 execute
  3. X = 11, if B executes fully before A starts
  4. X = 22, if B1, then A, then B2 execute

There are four possible outcomes for X after both processes finish, showing concurrent execution can lead to different results.

The correct option is c) four.

User Graell
by
8.1k points