Getting ready

In this section, we show you how to use a queue for a producer-consumer problem, that is a classic example of process synchronization.

The producer-consumer problem describes two processes: one is the producer and the other is a consumer, sharing a common buffer of a fixed size.

The task of the producer is to generate data and to deposit it in the buffer continuously. At the same time, the consumer will use the data produced, removing it from the buffer from time to time. The problem is to ensure that the producer does not process new data if the buffer is full and that the consumer does not look for data if the buffer is empty. The solution for the producer is to suspend its execution if the buffer is full.

As soon as the consumer has taken an item from the buffer, the producer wakes up and starts to fill the buffer again. Similarly, the consumer will suspend if the buffer is empty. As soon as the producer has downloaded the data into the buffer, the consumer wakes up.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.203.143