In the commentary on Figure 5.9 and Table 5.4, it was stated that “it would not do simply
to move the conditional statement inside the critical section (controlled by s) of
the consumer because this could lead to deadlock.” Demonstrate this with a table
similar to Table 5.4. 5.21 Consider the solution to the infinite-buffer producer/consumer problem defined in Figure 5.10. Suppose we have the (common) case in which the producer and consumer are running at roughly the same speed.The scenario could be
Producer: append; semSignal; produce; . . . ; append; semSignal; produce; . . .
Consumer: consume; . . . ; take; semWait; consume; . . . ; take; semWait; . . . The producer always manages to append a new element to the buffer and signal during
the consumption of the previous element by the consumer.The producer is always
appending to an empty buffer and the consumer is always taking the sole item in the
buffer. Although the consumer never blocks on the semaphore, a large number of
calls to the semaphore mechanism is made, creating considerable overhead.
Construct a new program that will be more efficient under these circumstances.
Hints: Allow n to have the value -1, which is to mean that not only is the buffer empty
but that the consumer has detected this fact and is going to block until the producer
supplies fresh data. The solution does not require the use of the local variable m
found in Figure 5.10.