Question

Consider three concurrent processes P1, P2 and P3 as shown below, which access a shared variable D that has been initialized to 100.

P1          P2          P3
:           :           :
:           :           :
D = D + 20  D = D - 50  D = D + 10
:           :           :
:           :           :

The processes are executed on a uniprocessor system running a time-shared operating system. If the minimum and maximum possible values of D after the three processes have completed execution are X and Y respectively, then the value of Y - X is _____________.

Official answer and solution
Official answer
80
From
Official GATE 2019 answer key

Solution

Analyze the possible race conditions and lost updates on variable D to determine the minimum and maximum possible final values.

Each high-level statement D = D + C on a shared variable involves three machine-level steps:

  1. Read D into local register R = D
  2. Compute R = R + C
  3. Store D = R

Initially, D=100D = 100. The three updates are:

  • P1P_1: add 2020
  • P2P_2: subtract 5050
  • P3P_3: add 1010

Finding Maximum Value YY:
To maximize the final value, we want the subtraction by P2P_2 to be overwritten (lost update), while the additions by P1P_1 and P3P_3 take effect:

  1. P2P_2 reads D=100D = 100, then gets preempted.
  2. P1P_1 reads D=100D = 100.
  3. P2P_2 resumes and writes 100−50=50100 - 50 = 50 (D=50D = 50).
  4. P1P_1 resumes and writes 100+20=120100 + 20 = 120 (D=120D = 120), overwriting P2P_2's decrement.
  5. P3P_3 runs to completion: reads 120120, adds 1010, writes 130130.
    Hence, the maximum possible value is Y=130Y = 130.

Finding Minimum Value XX:
To minimize the final value, we want the additions by P1P_1 and P3P_3 to be overwritten by P2P_2's subtraction:

  1. P2P_2 reads D=100D = 100, computes 100−50=50100 - 50 = 50, and is preempted before storing.
  2. P1P_1 runs to completion: reads 100100, writes 120120.
  3. P3P_3 runs to completion: reads 120120, writes 130130.
  4. P2P_2 resumes and writes its stored value 5050 into DD.
    Hence, the minimum possible value is X=50X = 50.

Therefore, the difference is:

Y−X=130−50=80Y - X = 130 - 50 = 80
Final answer

80

Common trap

Assuming sequential execution without interleaving, which yields Y - X = 0.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2019 Computer Science and Information Technology (CS)
Question
33 of 65, printed as CS Q.23
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.