What is the Producer-Consumer Problem in C?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The producer-consumer problem in C is one of the most famous problems associated with operating systems.

In the producer-consumer problem in C, there is a producer which produces products (data) and there is a consumer who consumes the products produced by the producer. Now, in the producer-consumer problem in C, there is a buffer. A buffer is a temporary storage area in the memory that stores data. The buffer in the Producer-Consumer Problem in C contains the produced items by the producer. The consumer consumes the products from the same buffer. In simpler terms, we can say that the buffer is a shared space between the producer and consumer.

Before getting into the producer-consumer problem in C, let us briefly discuss the work of the producer and consumer.

  • Producer: The producer's role is to produce or generate the data and put it in the buffer, and start again with the same.
  • Consumer: The consumer's role is to consume the produced data from the same shared buffer. When the consumer consumes the data, the data is removed from the buffer.

Refer to the image provided below for more clarity.

producer-consumer-problem-example

Note: The producer produces one data at a time and similarly, the consumer consumes one data at a time. So, the consumer and producer can work parallelly.

In the producer-consumer problem in C, we have been provided with a fixed-sized buffer. The problem states that there may arise a situation when the buffer is full and the producer is still producing the product similarly, the consumer tries to consume the product(s) when there is no product to be consumed from the buffer so both will parallel not be able to perform their jobs. So, we have to fix this issue so that the producer doesn’t try to produce the data in the buffer when the buffer is full and on the other hand, the consumer doesn’t try to consume the produced data when the buffer is empty. We should know that the producer-consumer problem in C is an example of the multi-process synchronization problem.

Refer to the next section to understand the solution to the producer-consumer problem in C.

What is the Solution for the Producer-Consumer Problem in C?

Since the problem is that the producer keeps on producing the data even if the buffer is full and the consumer tries to consume the data even if the buffer is empty. We can either put the producer to sleep or discard the data produced by the producer if the buffer is full. So, whenever the consumer tries to consume the data from the buffer, it will notify the producer that the data is being consumed. This will make the producer produce the data again.

Similarly, we can put the consumer to sleep when there is no data in the buffer (empty buffer case). So, whenever the producer produces the data and put it into the buffer, it will notify the consumer that the data is being produced. This will make the consumer consume the data again.

We should also notice that if there is an inadequate solution is proposed then there may arise a situation when both the producer and consumer are on wait (to be awakened)

What is the Main Cause of the Producer-Consumer Problem in C?

The main cause behind the producer-consumer problem in C is that there is no way that will tell the consumer if the buffer is empty or not. Similarly, the producer cannot know that the buffer is already full.

So, as we have discussed above, we can put the consumer to sleep when there is no data in the buffer (empty buffer case). We can similarly put the producer to sleep if the buffer is full. By the term sleep, we mean that the producer should not produce any product when the consumer is consuming the product and vice versa. We can use the sleep() function to put the consumer or the producer to sleep.

Examples of Producer-Consumer Problems in C

Example 1: Suppose we have a buffer of size = 5.

Initially, the buffer is empty and neither producer is producing the data nor the consumer is consuming the data.

Suppose that the producer starts producing the data one at a time. The producer produces 3 data during the production and when the consumer comes he/she consumes two of the three produced data. Now in this scenario, we can see that neither of the two has caused the problem as when the consumer was consuming the data, the buffer was not empty. Similarly, when the producer was producing the data the buffer was not full.

Refer to the image of the buffer shown below for more clarity.

buffer-image

Let us take another example when the buffer is full and the producer is still producing the data.

Example 2: Suppose we have a buffer of size = 3.

Initially, the buffer is empty and neither producer is producing the data nor the consumer is consuming the data.

Suppose that the producer starts producing the data one at a time. The producer produces 3 data during the production. The consumer has not consumed any of these produced data. The producer on the other hand keeps on producing the data. Here we can see, that the problem has arrived as the producer is been producing data more than the capacity of the shared buffer. As the buffer is full the data is lost and the producer is not producing any useful data.

Refer to the image of the buffer shown below for more clarity.

full-buffer

Let us take another example when the buffer is empty and the consumer tries to consume the data.

Example 3: Suppose we have a buffer of size = 3. (Refer to the image provided above for an understanding of the buffer size).

Initially, the buffer is empty and neither producer is producing the data nor the consumer is consuming the data.

Suppose that the producer starts producing the data one at a time and has produced two data. Now the capacity of the buffer is not full, there is still space for one more piece of data. Now the consumer comes and consumes one data at a time. He/She consumes the two produced data. Now the buffer has become empty but the consumer tries to consume the data again and again. The producer on the other hand is not producing any data.

Here we can see, that the problem has arrived as the consumer is been consuming the data more than the capacity of the shared buffer (No of produced data). As the consumer tries to consume data from an empty buffer he/she is not getting any data. To solve this problem, we can maintain a counter of the produced products, the total size of the buffer, and another boolean or flag variable that will check whether either consumer is consuming the product or the producer is producing the product. We will only allow one work at a time (either producer can produce the product or the consumer can consume the product from the common buffer). We can increase or decrease the number of products available after production and consumption.

So, let us look at an approach that can solve this problem. In the solution, we will be using a variable called mutex that will increase when the consumer consumes the data and it will decrease when the producer produces the data.

Besides this, we are using a variable called full that will be initialized with 0 which means that the current buffer is empty. Now, if the buffer is full, we will store 1 in the full variable.

Let us look at the implementation for a better understanding

Output

Learn More About C Programming Tutorial

To better understand the producer-consumer problem in C. We must be familiar with the C programming language.

The C programming language is one of the most primitive coding languages that follow the procedural approach of programming. C programming is composed of various functions defined together to form the entire program.

To learn more about the C Programming language, refer here

Conclusion

  • In the producer-consumer problem in C, there is a producer which produces items and there is a consumer who consumes the products produced by the producer.
  • The producer's role is to produce or generate the data and put it in the buffer and start again with the same. The consumer's role is to consume the produced data from the same shared buffer. When the consumer consumes the data, the data is removed from the buffer.
  • The producer produces one data at a time and similarly, the consumer consumes one data at a time.
  • We can either put the producer to sleep or discard the data produced by the producer if the buffer is full. We can similarly put the consumer to sleep when there is no data in the buffer (empty buffer case).
  • The main cause behind the producer-consumer problem in C is that there is no signal or anything that will tell the consumer if the buffer is empty or not. Similarly, the producer is not signaled that the buffer is full.