2台のPCから一つのファイルを並行して更新した。ファイル中の同一データ(データ1)に対する処理が①~④の順に行われたとき,データ1はどの値になるか。ここで,データ1の初期値は5であった。
| PC−A 【処理内容】 | PC−B 【処理内容】 |
|---|---|
| ① データ1を読み込み,10を足す。 | ② データ1を読み込み,1を引く。 |
| ③ ①の結果を,データ1に書き込む。 | ④ ②の結果を,データ1に書き込む。 |
This question's explanation and your answers so far.
2台のPCから一つのファイルを並行して更新した。ファイル中の同一データ(データ1)に対する処理が①~④の順に行われたとき,データ1はどの値になるか。ここで,データ1の初期値は5であった。
| PC−A 【処理内容】 | PC−B 【処理内容】 |
|---|---|
| ① データ1を読み込み,10を足す。 | ② データ1を読み込み,1を引く。 |
| ③ ①の結果を,データ1に書き込む。 | ④ ②の結果を,データ1に書き込む。 |
正解 Follow the order (1) to (4) and track when each process reads and when it writes, and the answer falls out. The initial value is 5. In (1), PC-A reads Data 1, but the value in the file at that moment is 5, so A computes 5 + 10 = 15 on its own side. In (2), PC-B reads Data 1, but the write in (3) has not been done yet, so the value in the file is still 5, and B computes 5 - 1 = 4 on its own side. In (3), PC-A writes the result of (1), 15, to the file, so Data 1 becomes 15. In (4), PC-B writes the result of (2), 4, so the 15 that was just there is overwritten and disappears, and Data 1 finally becomes 4. What is happening here is a lost update: when the same data is updated concurrently without exclusive control, the update that was applied first (+10) is lost by the later write. Therefore the correct answer is ア (4).
5 is the initial value itself, the value before any update. Writes to the file are actually carried out in (3) and (4), so the value cannot stay at the initial value. In addition, +10 and −1 do not cancel each other out, so the reading that the two updates offset each other and bring the value back to 5 does not hold either.
14 is 5 + 10 - 1, the value that ought to result when the updates from both PC-A and PC-B are correctly applied. That is the outcome when exclusive control (locking) is in effect and (2) reads after the write in (3); in the order (1) to (2) to (3) to (4) given in this question, (2) reads the value 5 from before the write, so B's computed result is 4, not 14.
15 is the value right after PC-A writes in (3), but in the last step (4) PC-B writes its own computed result, 4, so the 15 is overwritten and does not remain. Note that what is being asked is the final value after step (4) has finished.