Next: Critical Section
Up: No Title
Previous: Too much milk problem
- Never more than one person buys milk, and someone buys if
needed
- Basic Idea (Sol 1) :
Leave a note (kind of like a "lock")
Remove note (kind of like an "unlock")
if note present - don't buy (wait)
- Use atomic load and store as building blocks
if (noMilk) {
if (noNote) {
leave Note;
buy milk;
remove note; } }
- Process (thread) can get context switched after checking milk and
leaving note, but before buying milk
- Bad solution - occasional failure - hard to detect
Solution 2
Process A
leave noteA
if (noNoteB) {
if (noMilk)
buy milk }
remove noteA
ProcessB
leave noteB
if (noNoteA) {
if (noMilk)
buy milk }
remove noteB
- Context switch at the wrong time - possible for neither process to buy
milk - as each thinks the other is buying milk
- Starvation : processes wait forever
Solution 3
Process A
leave note A
while (note B) // Pos: 1
do nothing ; if
(noMilk)
buy milk ;
remove noteA
Process B
leave note B
if (noNoteA) { // Pos: 2
if (noMilk)
buy milk }
remove note B
- At Pos1: if noNote A, safe for B to buy (means A has not started yet).
If note, A is either buying, or waiting for B to quit, so ok for B to quit
- At Pos2: if noNote B, safe to buy. If note B, don't know. A hangs around.
Either: if B buys, done. If B doesn't buy, A will.
- Too complicated - must be simpler way
Omer F Rana
Tue Feb 11 19:19:09 GMT 1997