What is Producer-Consumer Problem?

The Producer-Consumer problem is a classical problem. The Producer-Consumer problem is used for multi-process synchronization, which means synchronization between more than one processes.

In this problem, we have one producer and one consumer. The producer is the one who produces something, and the consumer is the one who consumes something, produced by the producer. The producer and consumer both share the common memory buffer, and the memory buffer is of fixed-size.

The task performed by the producer is to generate the data, and when the data gets generated, and then it put the data into the buffer and again generates the data. The task performed by the consumer is to consume the data which is present in the memory buffer.

What are the Problems in the Producer-Consumer Problem?

There are various types of problems in the Producer-Consumer problem:

  1. At the same time, the producer and consumer cannot access the buffer.
  2. The producer cannot produce the data if the memory buffer is full. It means when the memory buffer is not full, then only the producer can produce the data.
  3. The consumer can only consume the data if the memory buffer is not vacant. In a condition where memory buffer is empty, the consumer is not allowed to take data from the memory buffer.

What is the solution for the Producer-Consumer Problem?

There are three semaphore variables that we use to solve the problems that occur in the Producer-Consumer problem.

  1. Semaphore S
  2. Semaphore E
  3. Semaphore F

Semaphore S: – With the help of the Semaphore ‘S’ variable, we can achieve mutual exclusion among the processes. By using the Semaphore variable ‘S,’ the producer or consumer can access and use the shared buffer at a specific time. Initially, the value of the Semaphore variable ‘S’ is set to 1.

Semaphore E: – With the help of the Semaphore ‘E’ variable, we can define the vacant (empty) space in the memory buffer. Initially, the value of the Semaphore ‘E’ variable is set to ‘n’ because initially, the memory buffer is empty.

Semaphore F: – We use Semaphore ‘F’ variable to define the filled space, which is filled by the producer. Firstly, we set the value of Semaphore variable ‘F’ to 0 because in starting, no space is filled by the producer.

With the help of these Semaphore variables, we can solve the problems that occur in the Producer-Consumer problem. We can also use two types of functions to solve this problem, and the functions are wait() and signal().

  1. wait(): – By using wait() function, we can decrease the value of the Semaphore variable by 1.
  2. signal(): – By using signal() function, the value of the Semaphore variable is incremented by 1.

Below is the pseudocode for the producer:

Producer-Consumer Problem

Explanation of the above code

  1. while(): – We used while() to produce the data again and again.
  2. produce(): – We called the produce() function to tell producer to produce the data.
  3. wait(E): – With the help wait() function, the value of the Semaphore variable ‘E’ can be decremented by 1. If a producer produces something, then we have to decrease the value of the Semaphore variable ‘E’ by 1.
  4. wait(S): – The wait(S) function is used to set the value of the Semaphore variable ‘S’ to ‘0’, so that other processes cannot enter into the critical section.
  5. append(): – By using append() function, new data is added in the memory buffer.
  6. signal(S): – We used the signal(S) function to set the value of the Semaphore variable ‘S’ to 1, so that another process can enter into the critical section.
  7. signal(F): – By using the signal(F) function, the value of the Semaphore variable ‘F’ is incremented by one.  In this, we increment the value by 1 because if we add the data into the memory buffer, there is one space that is filled in the memory buffer. So, we have to update the variable ‘F’.

 Below is the pseudocode for the consumer:

Producer-Consumer Problem

Explanation of the above code

  1. while(): – By using while(), the data can be consumed again and again.
  2.  wait(F): – We used wait(F) function to decrease the value of the Semaphore variable ‘F’ by 1. It is because if the consumer consumes some data, we have to reduce the value of the Semaphore variable ‘F’ by 1.
  3. wait(S): – The wait(S) function is used to set the value of the Semaphore variable ‘S’ to ‘0’, so that other processes cannot enter into the critical section.
  4. take():- We used take() function to take the data from the memory buffer by the consumer.
  5. signal(S): – We used the signal(S) function to set the value of the Semaphore variable ‘S’ to 1, so that other processes can enter into the critical section.
  6. signal(E): – The signal(E) function is used to increment the value of the Semaphore variable ‘E’ by 1. It is because after taking data from the memory buffer, space is freed from the buffer, and it is must to increase the value of the Semaphore variable ‘E’.
  7. use(): – By using the use() function, we can use the data taken from the memory buffer, so that we can perform some operations.

Pin It on Pinterest

Share This