lecture 8: interprocess communications ii -both use busy wait, which is bad, and can be "fatal" with PRIORITY INVERSION (high priority proc is busy waiting for resource held by low priority proc, which never gets a chance to run) Sleep and wakeup PRODUCER-CONSUMER problem (aka BOUNDED BUFFER problem) what are examples of this? disk writes, display writes, in general any kind of data being prepped for output. 1 buffer producer puts things in consumer takes things out what when buffer is full? -producer goes to sleep -consumer wakes producer when it removes item BUT: simple solution (pg 109) suffers from race condition problem; if count == 0 & consumer is about to sleep, then scheduler switches to producer, producer puts 1 into buffer and tries to wake consumer, but consumer's already awake and wake signal is lost. need WAKEUP WAITING BIT to solve. this is generalized to the SEMAPHORE, introduced by E. W. Dijkstra (1965). semaphore is like a count of waiting wakeup's. DOWN and UP on a semaphore as generalization of SLEEP and WAKEUP. Dijkstra's notation: P == down and V == up. testing value, changing it, and possibly going to sleep must be a single, indivisible ATOMIC ACTION. once a semaphore operation has started, no other proc can access that semaphore until the operation has completed or blocked (sleep). UP increments. if one or more procs is sleeping on that semaphore (unable to complete a DOWN), the OS chooses one of the sleeping procs at random and allows it to complete the down. thus, after an UP with a proc sleeping on it, the semaphore will still be 0, but it will have 1 fewer sleeping proc. no proc ever blocks using an UP. producer - consumer solution (pp 112) using semaphores: 1 called mutex for mutual exclusion, two (empty and full) for SYNCHRONIZATION.