I am reading the famous Operating System Concepts book of (Avi Silberschatz, Peter Baer Galvin, Greg Gagne) edition 9: http://codex.cs.yale.edu/avi/os-book/OS9/
In the book, in Section 5 "Process Synchronization", there is the following example:
Suppose that we wanted to provide a solution to the consumer-producer problem that fills all the buffers. We can do so by having an integer counter that keeps track of the number of full buffers. Initially, counter is set to 0. It is incremented by the producer after it produces a new buffer and is decremented by the consumer after it consumes a buffer.
And the algorithms for this problem are as follow:
Producer algorithm:
while (true)
{
/* produce an item in nextproduced */
while (counter == BUFFER_SIZE) ;
/* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
Consumer algorithm:
while (true)
{
while (counter == 0) ; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}
You can find these algorithms in the slides of the Process Synchronization chapter: http://codex.cs.yale.edu/avi/os-book/OS9/slide-dir/PPT-dir/ch5.ppt
I didn't understand how these algorithms are supposed to fill ALL the buffers. As far as I see they fill only one buffer. Am I right or I am missing something? These algorithms already mentioned in the book since 2007 at least, so I suppose that there is no error, but I misunderstood them?
Any explanation is welcome.
while (counter == BUFFER_SIZE);in the producer algorithm, when it fills the first buffer it will continue looping till the consumer consumes an item, so it will never start filling a second buffer. Maybe I am missing something essential here. - Ramibuffer[]. - Henk Holterman