- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Producer Consumer Problem using Semaphores
The producer consumer problem is a synchronization problem. There is a fixed size buffer and the producer produces items and enters them into the buffer. The consumer removes the items from the buffer and consumes them.
A producer should not produce items into the buffer when the consumer is consuming an item from the buffer and vice versa. So the buffer should only be accessed by the producer or consumer at a time.
The producer consumer problem can be resolved using semaphores. The codes for the producer and consumer process are given as follows −
Producer Process
The code that defines the producer process is given below −
do { . . PRODUCE ITEM . wait(empty); wait(mutex); . . PUT ITEM IN BUFFER . signal(mutex); signal(full); } while(1);
In the above code, mutex, empty and full are semaphores. Here mutex is initialized to 1, empty is initialized to n (maximum size of the buffer) and full is initialized to 0.
The mutex semaphore ensures mutual exclusion. The empty and full semaphores count the number of empty and full spaces in the buffer.
After the item is produced, wait operation is carried out on empty. This indicates that the empty space in the buffer has decreased by 1. Then wait operation is carried out on mutex so that consumer process cannot interfere.
After the item is put in the buffer, signal operation is carried out on mutex and full. The former indicates that consumer process can now act and the latter shows that the buffer is full by 1.
Consumer Process
The code that defines the consumer process is given below:
do { wait(full); wait(mutex); . . . REMOVE ITEM FROM BUFFER . signal(mutex); signal(empty); . . CONSUME ITEM . } while(1);
The wait operation is carried out on full. This indicates that items in the buffer have decreased by 1. Then wait operation is carried out on mutex so that producer process cannot interfere.
Then the item is removed from buffer. After that, signal operation is carried out on mutex and empty. The former indicates that consumer process can now act and the latter shows that the empty space in the buffer has increased by 1.
- Related Articles
- What is the shared memory concept by using producer consumer problem?
- How to implement monitors using semaphores?
- Explain the terms ‘producer’ and ‘consumer’. Give two examples of producers and two of consumers.
- If a grasshopper is eaten by a frog, then the energy transfer will be from : producer to decomposerproducer to primary consumerprimary consumer to secondary consumersecondary consumer to tertiary consumer
- Monitors vs Semaphores
- Semaphores in Operating System
- Combination sum problem using JavaScript
- Consumer Choices
- Consumer Budget
- Consumer Equilibrium
- Consumer Awareness
- Lock & Key problem using Hash-map
- How to use POSIX semaphores in C language
- What is meant by a primary consumer, secondary consumer, and tertiary consumer? Give one example of each.
- Rational Consumer Choice
