In computing, the producer–consumer problem (also known as the bounded-buffer problem) is a classic example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, which share a common, fixed-size buffer used as a queue.
- The producer’s job is to generate data, put it into the buffer, and start again.
- At the same time, the consumer is consuming the data (i.e. removing it from the buffer), one piece at a time.
Problem : To make sure that the producer won’t try to add data into the buffer if it’s full and that the consumer won’t try to remove data from an empty buffer.
Solution : The producer is to either go to sleep or discard data if the buffer is full. The next time the consumer removes an item from the buffer, it notifies the producer, who starts to fill the buffer again. In the same way, the consumer can go to sleep if it finds the buffer to be empty. The next time the producer puts data into the buffer, it wakes up the sleeping consumer.
An inadequate solution could result in a deadlock where both processes are waiting to be awakened.
In the post Producer-Consumer solution using threads in Java, we have discussed above solution by using inter-thread communication(wait(), notify(), sleep()). In this post, we will use Semaphores to implement the same.
The below solution consists of four classes:
- Q : the queue that you’re trying to synchronize
- Producer : the threaded object that is producing queue entries
- Consumer : the threaded object that is consuming queue entries
- PC : the driver class that creates the single Q, Producer, and Consumer.
Producer produced item : 0 Consumer consumed item : 0 Producer produced item : 1 Consumer consumed item : 1 Producer produced item : 2 Consumer consumed item : 2 Producer produced item : 3 Consumer consumed item : 3 Producer produced item : 4 Consumer consumed item : 4
Explanation : As you can see, the calls to put() and get( ) are synchronized, i.e. each call to put() is followed by a call to get( ) and no items are missed. Without the semaphores, multiple calls to put() would have occurred without matching calls to get(), resulting in items being missed. (To prove this, remove the semaphore code and observe the results.)
The sequencing of put() and get() calls is handled by two semaphores: semProd and semCon.
- Before put( ) can produce an item, it must acquire a permit from semProd. After it has produce the item, it releases semCon.
- Before get( ) can consume an item, it must acquire a permit from semCon. After it consumes the item, it releases semProd.
- This “give and take” mechanism ensures that each call to put( ) must be followed by a call to get( ).
- Also notice that semCon is initialized with no available permits. This ensures that put( ) executes first. The ability to set the initial synchronization state is one of the more powerful aspects of a semaphore.
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Operating System | Sleeping Barber problem
- Producer Consumer Problem using Semaphores | Set 1
- Operating System | Introduction of System Call
- Operating System | Dining Philosopher Problem Using Semaphores
- Java.lang.ThreadLocal class in Java
- Reentrant Lock in Java
- Multi-threaded Chat Application in Java | Set 2 (Client Side Programming)
- Semaphore in Java
- Producer-Consumer solution using threads in Java
- Inter-thread Communication in Java
- Synchronized in Java
- Readers-Writers Problem | Set 1 (Introduction and Readers Preference Solution)
- Operating System | Thread
- Process Synchronization | Monitors
- Operating System | Critical Section