1
votes

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.

1
I don't understand how you think they'd only fill/consume one buffer. They loop until the number of buffers filled or consumed is BUFFER_SIZE or 0, respectively. - ooga
Well, as I understood, the producer algorithm for example, it increments the counter variable each time it produces an item (no matter if the current buffer is filled or not), this variable for me doesn't correspond to the number of buffer... it should increment only when the current buffer is filled and we want to start filling a new one no? - Rami
The 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. - Rami
Ohhhh, I see your misunderstanding now! BUFFER_SIZE is badly named, I guess. It's not the size of a single buffer or of all buffers together, but simply the total number of buffers. It should be called NUMBER_OF_BUFFERS. - ooga
No, it's not badly names. Rami misinterpreted it as ITEM_SIZE but it's the size of buffer[]. - Henk Holterman

1 Answers

0
votes

OK I got it, the problem is in the semantic of the variables. The next_produced variable will fill a full buffer directly, so the buffer[] is an array of '''BUFFER_SIZE''' buffers, each loop in the producer will produce a next_produced item which will fill directly an entire buffer (element) int the buffer[] array.