Producer- Consumer Problem:
What is Producer Consumer Problem:- The problem describe two processes, User and Consumer, who share a common fixed size buffer. Producer consumer problem also known as the "Bounded Buffer Problem" is a multi-process synchronization problem.
Producer: The producer's job is to generate a bit of data, put it into the buffer and start again.
Consumer: The consumer is consuming the data(i.e remaining it from the buffer) one piece at a time.
If the buffer is empty, then a consumer should not try to access the data item from it.0
Similarly, a producer should not produce any data item if the buffer is full.
Counter: It counts the data items in the buffer. or to track whether the buffer is empty or full. Counter is shared between two processes and updated by both.
How it works?
• Counter value is checked by consumer before consuming it.
• If counter is 1 or greater than 1 then start executing the process and updates the counters.
• Similarly producer check the buffer for the value of Counter for adding data.
• If the counter is less than its maximum values, it means that there is some space in Buffer.
It starts executing for producing the data items and update the counter by implementing it by one.
Let max= maximum size of the buffer.
If buffer is full then counter=max and consumer is busy executive other instructions or has not been allotted its time slice yet.
In this buffer is full producer has to wait until consumer set counter by commenting its value by 1.
In this situation, buffer is empty, that is counter =0, and the producer is busy executive other instructions or has not been allotted its time slice yet. At this consumer is ready to consume an item from the buffer.
Consumer waits until counter =1
when the buffer is empty and producer busy in filling data items in Buffer in while consumer goes to SLEEP. When the counter goes to 1, then system generates WAKEUP calls to make consumer to wake up and start executing it.