Professional Documents
Culture Documents
3.7 Producer Consumer Problem
3.7 Producer Consumer Problem
Introduction:
Producer-Consumer
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
producer-consumer
consumer problem, the
producer produces an item and the consumer consumes the item produced by the producer.
There is a buffer of N slots and each slot is capable of storing one unit of data.
There are two processes running, i.e. Producer and Consumer, which are currently operated in
the buffer.
There are certain restrictions/conditions for both the producer and consumer process, so that
data synchronization can be done without interruption. These are as follows:
The producer tries to insert data into an empty slot of the buffer.
The consumer tries to remove data from a filled slot in the buffer.
The producer must not insert data when the buffer is full.
The consumer must not remove data when the buffer is empty.
The producer and consumer should not insert and remove data simultaneously.
m(mutex) : A binary semaphore which is used to acquire and release the lock.
empty(), a counting semaphore whose initial value is the number of slots in the buffer,
since initially, all slots are empty.
full, a counting semaphore, whose initial value is 0.
wait(empty): If the producer has to produce/insert something into the buffer, it needs
to first check whether there are empty slots in the buffer. If true, the producer inserts
data into the buffer and then decrements one empty slot.
wait(mutex): It is a binary semaphore, hence acquires the lock. This is shared among
the producer and consumer. Hence, if the producer is acquiring the lock,
the consumer cannot make any change in the buffer, until the lock is released.
add(): It adds the data to the buffer.
wait(full): If the consumer has to remove data from the buffer, it needs to first check
whether the buffer contains some item or not. If true, the consumer removes the data
from the buffer and then decrements one full slot.
wait(mutex): It is a binary semaphore, hence acquires the lock. This is shared among
the producer and consumer. Hence, if the consumer is acquiring the lock,
the producer cannot make any change in the buffer, until the lock is released.
consumer(): It removes the data from the buffer.
signal(mutex): It simply releases the lock acquired by the producer since the addition of
data has been done in the buffer.
signal(empty): This increments the empty semaphore since one of the empty slots have
now been emptied.
Mutex is used to solve the producer-consumer problem as mutex helps in mutual exclusion. It
prevents more than one process to enter the critical section. As mutexes have binary values i.e 0
and 1. So whenever any process tries to enter the critical section code it first checks for the mutex
value by using the wait operation.
wait(mutex);
wait(mutex) decreases the value of mutex by 1. so, suppose a process P1 tries to enter the critical
section when mutex value is 1. P1 executes wait(mutex) and decreases the value of mutex. Now,
the value of mutex becomes 0 when P1 enters the critical section of the code.Now, suppose
Process P2 tries to enter the critical section then it will again try to decrease the value of mutex. But
the mutex value is already 0. So, wait(mutex) will not execute, and P2 will now keep waiting for P1
to come out of the critical section.
In the above section in both the Producer process code and consumer process code, we have the wait
and signal operation on mutex which helps in mutual exclusion and solves the problem of the
Producer consumer process.
Conclusion
Producer Process produces data item and consumer process consumes data item.
Both producer and consumer processes share a common memory buffer.
Producer should not produce any item if the buffer is full.
Consumer should not consume any item if the buffer is empty.
Not more than one process should access the buffer at a time i.e mutual exclusion
should be there.
Full, Empty and mutex semaphore help to solve Producer-consumer problem.
Full semaphore checks for the number of filled space in the buffer by the producer
process
Empty semaphore checks for the number of empty spaces in the buffer.
mutex checks for the mutual exclusion.