Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

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.

What is Producer Consumer Problem?


Before knowing what is Producer
Producer-Consumer
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.
data/item
 Consumer is a Process that is able to consume the data/item produced by the Producer.
Producer
 Both Producer and Consumer share a common memory buffer. This buffer is a space of
a certain size in the memory of the syst
system
em which is used for storage. The producer
produces the data into the buffer and the consumer consumes the data from the buffer.

Author: M.Vinitha IIIT-Ongole Subject: Operating System


Problem Statement
Producer-Consumer Problem is also known as bounded buffer problem. The Producer-
Consumer Problem is one of the classic problems of synchronization.

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.

For solving the producer-consumer problem, three semaphores are used:

 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.

Implementation of producer code


void Producer(){
do{
//wait until empty > 0
wait(Empty);
wait(mutex);
add()
signal(mutex);
signal(Full);
}while(TRUE);
}

In the above code:

 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.

Author: M.Vinitha IIIT-Ongole Subject: Operating System


 signal(mutex): It simply releases the lock acquired by the producer since the addition of
data has been done in the buffer.
 signal(full): This increments the full semaphore since one of the empty slots has now
been filled.

Implementation of consumer code


void Consumer(){
do{
//wait until empty > 0
wait(full);
wait(mutex);
consume()
signal(mutex);
signal(empty);
}while(TRUE);
}
In the above code:

 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.

Why can mutex solve the producer consumer Problem ?

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.

Author: M.Vinitha IIIT-Ongole Subject: Operating System


Now, suppose if P2 comes out of the critical section by executing signal(mutex)
signal(mutex)
signal(mutex) increases the value of mutex by 1.mutex value again becomes 1.
Now, the process P2 which was in a busy-waiting state will be able to enter the critical section by
executing wait(mutex).
So, mutex helps in the mutual exclusion of the processes.

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.

Can the consumer remove an item from the buffer if full() = 0?


No, if there are no items in the buffer, i.e. full semaphore = 0, the consumer cannot remove
any item.

Can the producer and consumer acquire the lock simultaneously?


No, either the producer or consumer can acquire the lock at a time. The
operation wait(mutex) is used to acquire the lock.

Author: M.Vinitha IIIT-Ongole Subject: Operating System

You might also like