Producer Consumer Problem
Producer Consumer Problem
Producer-Consumer problem is a classical synchronization problem in the operating system. With the presence of
more than one process and limited resources in the system the synchronization problem arises. If one resource is
shared between more than one process at the same time then it can lead to data inconsistency. In the producer-
consumer problem, the producer produces an item and the consumer consumes the item produced by the producer.
Before knowing what is Producer-Consumer Problem we have to know what are Producer
and Consumer.
In operating System Producer is a process which is able to produce data/item.
Consumer is a Process that is able to consume the data/item produced by the Producer.
Both Producer and Consumer share a common memory buffer. This buffer is a space of a certain size in the
memory of the system which is used for storage. The producer produces the data into the buffer and the
consumer consumes the data from the buffer.
For consistent data synchronization between Producer and Consumer, the above problem should be resolved.
Semaphores are variables used to indicate the number of resources available in the system at a particular time.
semaphore variables are used to achieve `Process Synchronization.
Full
The full variable is used to track the space filled in the buffer by the Producer process. It is initialized to 0 initially as
initially no space is filled by the Producer process.
Empty
The Empty variable is used to track the empty space in the buffer. The Empty variable is initially initialized to the
BUFFER-SIZE as initially, the whole buffer is empty.
Mutex
Mutex is used to achieve mutual exclusion. mutex ensures that at any particular time only the producer or the
consumer is accessing the buffer.
Mutex - mutex is a binary semaphore variable that has a value of 0 or 1.
We will use the Signal() and wait() operation in the above-mentioned semaphores to arrive at a solution to the
Producer-Consumer problem.