- blocks instead of wasting CPU time (while loop) when they are not allowed to enter their CRs
- sleep and wakeup pair
- sleep is a system call that causes the caller to block (be suspended until another process wakes is up)
- The Producer-Consumer Problem
- Suppose one process is creating information that is going to be used by another process, e.g., suppose one process reads information from the disk, and another compiles that information from source to machine code.
- Producer: creates copies of a resource
- Consumer: uses up copies of a resource
- Buffers: used to hold information after producer has created it but before consumer has used it
- Signaling: keeping control of producer and consumer (e.g., preventing overrun of the producer)
- Constraints:
- Consumer must wait for a producer to fill buffers. ( signaling)
- Producer must wait for consumer to empty buffers, when all buffer space is in use. ( signaling)
- Only one process must manipulate buffer pool at once. ( mutual exclusion)
- Trouble arises when the producer wants to put a new item in the buffer, but it is already full
- The solution is for the producer to go to sleep, to be awakened when the consumer has removed one or more items
- RACE CONDITION can occur because access to count (see Fig. 2.13) is unconstrained.
- the buffer is empty
- the consumer has read count to see if it is 0, sleeping
- at that instant, the scheduler started running the producer
- the producer inserts an item in the buffer, count is 1
- the consumer should be awaken up, the producer calls wakeup
- the consumer is not logically asleep, so the wakeup signal is lost
- the producer will fill up the buffer and also go to sleep
- BOTH WILL SLEEP FOREVER.
Figure 2.13:
The producer-consumer problem with a fatal race problem
|
2004-05-25