YES, THIS IS A TEST FILE FOR PROBLEM 6. HOMEWORK 4 SOLUTION ------------------- Mar 27, 2006 (Version 2) Total Points = 19 Problem 1 (0 Points) --------- Two identical processes execute in parallel, each trying to increment the shared variable 'X' n times as long as it is less than 10. The fundamental idea here is that user-level assignment statements (e.g., ++X) are not atomic: they are compiled into code that updates a memory location using multiple machine instructions. When there is more than one process (and potentially more than one CPU), the processes do not see a consistent view of that memory location's content. The maximum value of 'X' is 10, and the minimum value is 2. [[ It is also possible to argue that the maximum value is 11 if you assume a the compiler generates extra STORE instructions, but this is not normal. ]] Clearly, the maximum value of 'X' will be 10 when the processes execute one after the other until 'X' reaches 10; i.e., one process finishes before the other starts. It is not clear why the minimum value is 2. Assume that the machine code for "if (X < 10) ++X" is: LOAD R1,X # R1 = X IF (X >= 10) GOTO L1 INC R1 # R1 = R1 + 1 STORE R1,X # X = R1 L1: ... The "+" operation is performed 10 times. If each takes affect and there are 2 processes, 'X' will get incremented 10 times. This can occur so long as both processes see (LOAD) the value of 'X' produced by the other process. The minimum value of 2 occurs when the following unusual sequence occurs. Call the two processes A and B. A could load a 0 and take a very long time to increment. Meanwhile B could load a 0 and loop 99 times incrementing and finally leaving 'X' equal to 10. Next, A finally stores a 1 on top of the 10 and finishes its first iteration. Then, B (in its last iteration) loads the 1 followed by A completing its computation and eventually storing a 10 into 'X'. But B is still holding the 1 value in its register and only has one more iteration. Finally, B increments the 1 and stores a value of 2 on top of the 10. Gobal A B X n R1 n R1 ======================================================= 0 0 0 --> 1 0 0 --> 1; X=R1; 1 1 1 --> 2 ... ... 10 98 10 --> X=R1 1 99 1 --> 2 1 1 --> 2 2 2 --> 3 ... ... 99 10; X=R1 10 X=R1 2