Frage

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.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top