Producer Consumer Problem and its Implementation with C++
In this article, we will discuss the Producer-Consumer Problem and its Implementation with C++. The Producer-Consumer problem is a classical two-process synchronization problem. Let’s discuss it one by one.
Problem Statement :
There is one Producer and one Consumer in the producer-consumer problem.
- Producer –
The producer process executes a set of statements int produce to create a data element and stores it in the buffer.
- Consumer –
If the buffer has items, a consumer process executes a statement consume with the data element as a parameter.
The problem arises because the process is not synchronized because of which the items produced and consumed may not be consistent. In order to solve this problem, we use semaphore for solving this problem i.e. problem of the critical section.
Implementation in C++ :
This problem can be further subdivided into two parts as follows.
Part-1: Where the buffer size is infinite –
In this case, the buffer from where the producer stores the item and the consumer consumes the item size is not fixed.
Part-2: Where the buffer size is fixed or finite –
In this case, the buffer from where the producer stores the item and the consumer consumes the item size is fixed.