Professional Documents
Culture Documents
A producer tries to fill an empty space in the buffer with data. A consumer
attempts to retrieve data from that buffer slot. If those two processes run
concurrently, as you may have anticipated, they will not give the desired
outcome.
There must be a method to make both the producer and the customer operate
independently.
The following are some of the issues that might arise in the Producer-Consumer:
The producer should generate data only if the buffer is not full. When the
buffer is filled, the producer should not be able to add any more data to it.
When the buffer is not empty, the consumer can consume the data. The
consumer should not be able to take any data from the buffer if it is empty.
The buffer should not be used by both the producer and the consumer at the
same time.
signal(S){
S++; // increasing S by one
}
There are two types of semaphores:
Binary Semaphore - This is related to, but not the same as, a mutex lock. It can
only have two possible values: 0 and 1. Its value is set to 1 at the start. It is used
to implement a solution to a critical section problem including multiple processes.
Counting Semaphore - Its value can traverse an unbounded domain. It's used
to limit who has access to a resource with numerous instances.
To solve this problem, We employ three semaphore variables:-
mutex - The lock is acquired and released using a mutex, a binary
semaphore.
empty - empty is a counting semaphore that is initialized on the basis of the
number of slots present in the buffer, at first all the slots are empty.
full - a counting semaphore with a value of zero as its starting value.
At any particular time, the current value of empty denotes the number of vacant
slots in the buffer, while full denotes the number of occupied slots.