Hardware Reference
In-Depth Information
varying, speeds. Consider the case where only one number is left in the buffer, in
entry 21, and in
21, as shown in Fig. 6-27(a). The producer is at
statement P1 looking for a prime and the consumer is busy at C5 printing out the
number in position 20. The consumer finishes printing the number, makes the test
at C1, and takes the last number out of the buffer at C2. It then increments out .At
this instant, both in and out have the value 22. The consumer prints the number
and then goes to C1, where it fetches in and out from memory in order to compare
them, as shown in Fig. 6-27(b).
=
22 and out
=
Producer at P5
sends wakeup
Consumer at C1
Producer at P1
Consumer at C5
Producer at P1
Consumer at C1
99
99
99
Buffer
empty
In=23
Out=22
In=22
Out=21
In=Out=22
Prime
Prime
1 number
in buffer
1 number
in buffer
0
0
0
(a)
(b)
(c)
Figure 6-27. Failure of the producer-consumer communication mechanism.
At this very moment, after the consumer has fetched in and out but before it
has compared them, the producer finds the next prime. It puts the prime into the
buffer at P3 and increments in at P4. Now in =23and out = 22. At P5 the pro-
ducer discovers that in
next ( out ). In other words, in is one higher than out , sig-
nifying that there is now one item in the buffer. The producer therefore (incorrect-
ly) concludes that the consumer must be sleeping, so it sends a wakeup signal (i.e.,
calls resume ), as shown in Fig. 6-27(c). Of course, the consumer is still awake, so
the wakeup signal is lost. The producer begins looking for the next prime.
At this point in time the consumer continues. It has already fetched in and out
from memory before the producer put the last number in the buffer. Because they
both have the value 22, the consumer goes to sleep. Now the producer finds anoth-
er prime.
=
22, therefore it as-
sumes that there are two numbers in the buffer (true) and that the consumer is
awake (false). The producer continues looping. Eventually, it fills the buffer and
goes to sleep. Now both processes are sleeping and will remain so forever.
The big difficulty here is that between the moment when the consumer fetched
in and out and the time it went to sleep, the producer snuck in, discovered that
It checks the pointers and finds in
=
24 and out
=
 
Search WWH ::




Custom Search