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.
Attention reader! Don’t stop learning now. Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.
Learn all GATE CS concepts with Free Live Classes on our youtube channel.