(Usually RAM; could be files or other forms of "memory")
Producer: overwrite memory, starting from the “top”, with numbers given to you in sorted order. 412 18 17 411 414 413 415 50 54 Consumer: Return the median value (ideally 411)
==> Problem: Consumer consumes before producer is done producing! (Race condition)
for ( int i = 1000; i < 1012; i++ ) memory[i] = nn
==> Problem: Processes are overwriting each other; need synchronization, “atomic” operations
A: ... b = x + (5 * x); ... B: ... x = z + (3 * z); ...
(variation of example from Wikipedia article on critical sections)
==> Problem: A might get two different values of x!
Get resource A Get resource B Do something with them Put result in resource C Release resources |
Get resource B Get resource A Do something with them Put result in resource D Release resources |
==> Example: 4 cars arrive at 4-way Stop Sign at the same time