[9 p] Critical section. Assume that multi-threaded program has three variables (X, Y and Z) in shared memory and we want to protect them with critical section. Variables are referenced in 5 different locations (A, B, C, D and E) in program:
A: X <- 7 B: some1 <- X C: Y <- 87 D: some4 <- X E: some5 <- Y
Y <- 4 some2 <- Y Y <- 0
Z <- X+Y some3 <- Z
In location A, the old values of variables Y and Z must not be visible once X has received a new value, and the new value of Z must the the sum of new values for X and Y. In location B, values for variables some1, some2 and some3 must be those of X, Y and Z at execution start time for this code sequence. In location E, variable value Y must be reset to zero immediately after reading its value.
- Which of these (pseudo-)code sequences (A, B, C, D and E) must be protected as critical section and why? Who do the other sequences have no need for protection?
- Give a scenario leading to an erroneous situation, if the critical sections are not protected. Explain why the result is erroneous and what caused the error. Will the same error happen every execution time?
- Show (with pseudo-code), how critical sections in these threads are protected with busy-wait loops (e.g., with test-and-set instruction). In what type of environment would this be a reasonable solution? Why?