データ D を更新する二つの処理 A,B が,①→③→②→④ のタイミングで実行された場合,D の値は幾らになるか。ここで,D の初期値は 2 とする。
| 処理A 〔処理内容〕 | 処理B 〔処理内容〕 |
|---|---|
| ① D を読み込み,3倍する。 | ③ D を読み込み,5を加える。 |
| ② ①の結果を,D に書き込む。 | ④ ③の結果を,D に書き込む。 |
This question's explanation and your answers so far.
データ D を更新する二つの処理 A,B が,①→③→②→④ のタイミングで実行された場合,D の値は幾らになるか。ここで,D の初期値は 2 とする。
| 処理A 〔処理内容〕 | 処理B 〔処理内容〕 |
|---|---|
| ① D を読み込み,3倍する。 | ③ D を読み込み,5を加える。 |
| ② ①の結果を,D に書き込む。 | ④ ③の結果を,D に書き込む。 |
6 is the value right after ② writes the result of ① (2×3), but Process B then overwrites it with 7 in ④, so it does not remain as the final value. 6 would be the answer in the order ①→③→④→②, that is, when Process B writes first and Process A writes afterwards, which differs from the ①→③→②→④ order of this question.
正解 The correct answer is イ (7). Separate reading (getting the current value) from writing (updating it), and trace both the value of D and the result each process is holding, following the order ①→③→②→④. The initial value is D=2. ① Process A reads D and computes 2×3=6, but at this point it has not written anything yet, so D is still 2. ③ Process B reads D. Because the write in ② has not been done yet, Process B reads the old value 2 and computes 2+5=7. ② Process A writes the result of ①, which is 6, into D, so D becomes 6. ④ Process B writes the result of ③, which is 7, into D, overwriting D=6, so D becomes 7. The value written last, by Process B, is the one that remains, so the answer is 7. Here the update made by Process A (6) has been overwritten by Process B and disappeared; this is the lost update that happens when no exclusive control (locking) is applied.
11 is the value when Process B reads only after Process A has completely finished, that is, the value when the steps run serially in the order ①→②→③→④ ((2×3)+5=11). In this question ③ runs before ②, so Process B reads the initial value 2 rather than the updated 6, and the result does not become 11. Do not confuse it with the ideal result obtained when exclusive control works correctly.
21 is the value when Process A reads only after Process B has finished first, that is, the value when the steps run serially in the order ③→④→①→② ((2+5)×3=21). In this question ① comes first, so Process A reads 2, and a situation where 7 is multiplied by 3 never arises.