SlideShare a Scribd company logo
2
Most read
3
Most read
6
Most read
Problematic Scenario
-Count is initially 0
Consumer reads the count-
-Producer produces the item, inserts it, and
increments count to 1
-Producer executes wakeup, but there is no
waiting consumer (at this point)
PRODUCER CONSUMER
PROBLEM
-Consumer continues its execution and
goes to sleep
-Consumer stays blocked forever
unnecessarily
-Main problem: wakeup was lost
THE PRODUCER/CONSUMER PROBLEM
WITH DIAGRAM
from time to time, the producer places an item in the buffer
the consumer removes an item from the buffer
careful synchronization required
the consumer must wait if the buffer empty
the producer must wait if the buffer full
producer
process
consumer
process
P
buffe
r
C
typical solution would involve a shared variable count
also known as the Bounded Buffer problem
Example: in UNIX shell
eqn myfile.t | troff
5
OVERVIEW OF SOLUTIONS
Low-level (for mutual exclusion)
Interrupt disabling
Using read/write instructions
Using powerful instructions (Test-and-set, Compare-and Swap…)
OS-level support (mutual exclusion and synchronization)
Special variables: Semaphores, Mutexes
Message passing primitives (send and receive)
High-level Synchronization Primitives
Monitors (Hoare, Brinch-Hansen)
Synchronized method in Java
Idealized Problems
Producer-Consumer
Dining Philosophers
Readers-Writers
6
SLEEP/WAKEUP SOLUTION TO
PRODUCER-CONSUMER PROBLEM
bounded buffer (of size N)
producer writes to it, consumer reads from it
Solution using sleep/wakeup synchronization
int count = 0 /* number of items in buffer
*/
Producer code:
while (TRUE) {
/* produce */
if (count == N) sleep;
/* add to buffer */
count = count + 1;
if (count == 1)
wakeup(Consumer);
}
Consumer code:
while (TRUE) {
if (count==0) sleep;
/* remove from buffer */
count = count -1;
if (count == N-1)
wakeup(Producer);
/* consume */
}

More Related Content

PPTX
Producer consumer
PPTX
Producer sonsumer ppt
PPTX
Producer consumer problem operating system
PPTX
Semophores and it's types
PPTX
Synchronization problems
PPTX
Operating system 25 classical problems of synchronization
PPTX
Sleeping barber problem
PDF
Semaphores
Producer consumer
Producer sonsumer ppt
Producer consumer problem operating system
Semophores and it's types
Synchronization problems
Operating system 25 classical problems of synchronization
Sleeping barber problem
Semaphores

What's hot (20)

PDF
ONLINE SHOPPING SYSTEM -SEPM
PPTX
Critical Section in Operating System
PPTX
Types of operating system.................
PPT
Virtual memory
PPTX
Memory Management in OS
DOCX
Parallel computing persentation
PPTX
UNIX Operating System
PPT
DATA PERSISTENCE IN ANDROID OPERATING SYSTEM
PDF
CS9222 ADVANCED OPERATING SYSTEMS
PDF
Monitors
PPTX
Consistency in NoSQL
PPTX
Operating System- Multilevel Paging, Inverted Page Table
PDF
Python projects
PDF
An Online Food Ordering Service
PPT
Wireless Markup Language,wml,mobile computing
PPTX
Presentation on Segmentation
PDF
Android Data Persistence
PPTX
Threads (operating System)
PPTX
Linq to sql
PPTX
Multi core processor
ONLINE SHOPPING SYSTEM -SEPM
Critical Section in Operating System
Types of operating system.................
Virtual memory
Memory Management in OS
Parallel computing persentation
UNIX Operating System
DATA PERSISTENCE IN ANDROID OPERATING SYSTEM
CS9222 ADVANCED OPERATING SYSTEMS
Monitors
Consistency in NoSQL
Operating System- Multilevel Paging, Inverted Page Table
Python projects
An Online Food Ordering Service
Wireless Markup Language,wml,mobile computing
Presentation on Segmentation
Android Data Persistence
Threads (operating System)
Linq to sql
Multi core processor
Ad

Viewers also liked (20)

PPTX
Producer and consumer classical problem
PPTX
Producer Consumer
PDF
Dining Philosopher's Problem
PPT
Peterson Critical Section Problem Solution
PDF
The bounded buffer
PPT
Semaphores OS Basics
PPT
PPT
OS Process Synchronization, semaphore and Monitors
PPT
Ch7 OS
 
PPT
Lecture 7, 8, 9 and 10 Inter Process Communication (IPC) in Operating Systems
ODP
Semaphore
PDF
The Dining Philosophers problem in Bangla
PPTX
dining philosophers problem using montiors
PPTX
SYNCHRONIZATION
PPT
Classical problem of synchronization
PDF
Operating Systems - Synchronization
PPT
Lec11 semaphores
PPT
Chapter 6 - Process Synchronization
PDF
Producer consumer-problems
ODP
Shellscripting
Producer and consumer classical problem
Producer Consumer
Dining Philosopher's Problem
Peterson Critical Section Problem Solution
The bounded buffer
Semaphores OS Basics
OS Process Synchronization, semaphore and Monitors
Ch7 OS
 
Lecture 7, 8, 9 and 10 Inter Process Communication (IPC) in Operating Systems
Semaphore
The Dining Philosophers problem in Bangla
dining philosophers problem using montiors
SYNCHRONIZATION
Classical problem of synchronization
Operating Systems - Synchronization
Lec11 semaphores
Chapter 6 - Process Synchronization
Producer consumer-problems
Shellscripting
Ad

Similar to Producer Consumer Problem (20)

DOCX
Producer consumer.docx
PPTX
Producer-Consumer problem.pptx practices
PPT
Process Synchronization -1.ppt
PPTX
Concurrent Programming Patterns
PDF
Inter Process Communication in operating system
PDF
operating system
PPTX
1 Synchronization.pptx
PPT
Slot03 concurrency2
PPTX
Reactive solutions using java 9 and spring reactor
PDF
producer consumer problem example in java.pdf
PPTX
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PPTX
794985751-Unit-3-Inter-Process-Communication.pptx
PPT
Kafka Reliability - When it absolutely, positively has to be there
PPT
Apache Kafka Reliability Guarantees StrataHadoop NYC 2015
PPTX
synchronization in operating system structure
DOCX
UNIT III Process Synchronization.docx
PPT
Ipc feb4
PPT
Classical-Problem-of-Synchronization in OS
PPT
Producer Consumer Problem in C explained.ppt
PDF
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
Producer consumer.docx
Producer-Consumer problem.pptx practices
Process Synchronization -1.ppt
Concurrent Programming Patterns
Inter Process Communication in operating system
operating system
1 Synchronization.pptx
Slot03 concurrency2
Reactive solutions using java 9 and spring reactor
producer consumer problem example in java.pdf
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
794985751-Unit-3-Inter-Process-Communication.pptx
Kafka Reliability - When it absolutely, positively has to be there
Apache Kafka Reliability Guarantees StrataHadoop NYC 2015
synchronization in operating system structure
UNIT III Process Synchronization.docx
Ipc feb4
Classical-Problem-of-Synchronization in OS
Producer Consumer Problem in C explained.ppt
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
KodekX | Application Modernization Development
PDF
Electronic commerce courselecture one. Pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Empathic Computing: Creating Shared Understanding
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Encapsulation theory and applications.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Network Security Unit 5.pdf for BCA BBA.
Advanced methodologies resolving dimensionality complications for autism neur...
Building Integrated photovoltaic BIPV_UPV.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Unlocking AI with Model Context Protocol (MCP)
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Machine learning based COVID-19 study performance prediction
KodekX | Application Modernization Development
Electronic commerce courselecture one. Pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Diabetes mellitus diagnosis method based random forest with bat algorithm
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Empathic Computing: Creating Shared Understanding
20250228 LYD VKU AI Blended-Learning.pptx
Encapsulation theory and applications.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf

Producer Consumer Problem

  • 1. Problematic Scenario -Count is initially 0 Consumer reads the count- -Producer produces the item, inserts it, and increments count to 1 -Producer executes wakeup, but there is no waiting consumer (at this point) PRODUCER CONSUMER PROBLEM
  • 2. -Consumer continues its execution and goes to sleep -Consumer stays blocked forever unnecessarily -Main problem: wakeup was lost
  • 3. THE PRODUCER/CONSUMER PROBLEM WITH DIAGRAM from time to time, the producer places an item in the buffer the consumer removes an item from the buffer careful synchronization required the consumer must wait if the buffer empty the producer must wait if the buffer full producer process consumer process P buffe r C
  • 4. typical solution would involve a shared variable count also known as the Bounded Buffer problem Example: in UNIX shell eqn myfile.t | troff
  • 5. 5 OVERVIEW OF SOLUTIONS Low-level (for mutual exclusion) Interrupt disabling Using read/write instructions Using powerful instructions (Test-and-set, Compare-and Swap…) OS-level support (mutual exclusion and synchronization) Special variables: Semaphores, Mutexes Message passing primitives (send and receive) High-level Synchronization Primitives Monitors (Hoare, Brinch-Hansen) Synchronized method in Java Idealized Problems Producer-Consumer Dining Philosophers Readers-Writers
  • 6. 6 SLEEP/WAKEUP SOLUTION TO PRODUCER-CONSUMER PROBLEM bounded buffer (of size N) producer writes to it, consumer reads from it Solution using sleep/wakeup synchronization int count = 0 /* number of items in buffer */ Producer code: while (TRUE) { /* produce */ if (count == N) sleep; /* add to buffer */ count = count + 1; if (count == 1) wakeup(Consumer); } Consumer code: while (TRUE) { if (count==0) sleep; /* remove from buffer */ count = count -1; if (count == N-1) wakeup(Producer); /* consume */ }