Questions tagged [producer-consumer]

The Producer-Consumer Problem (also known as the bounded-buffer problem) is a classical example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.

The Producer-Consumer Problem (also known as the bounded-buffer problem) is a classical example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue. The producer's job is to generate a piece of 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. The problem is 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.

The solution for 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. The solution can be reached by means of inter-process communication, typically using semaphores. An inadequate solution could result in a deadlock where both processes are waiting to be awakened. The problem can also be generalized to have multiple producers and consumers.

References

1564 questions
0
votes
1 answer

Producer Consumer setup: How to handle Database Connections?

I'm building my first single-producer/single-consumer app in which the consumer takes items off the queue and stores them in a MySQL database. Previously, when it was a single thread app, I would open a connection to the DB, send the query, close…
Kyle G.
  • 870
  • 2
  • 10
  • 22
0
votes
2 answers

Perl: Recommendations on how to forward messages received from blocking calls to multiple network sockets

I have the following Perl problem: I am trying to build a simple network-enabled forwarder/consumer script. the forwarder part loops and calls a local blocking function to retrieve a new string (i.e., from an existing library) the consumer part…
msto
  • 63
  • 1
  • 4
0
votes
2 answers

Index out of Range in consumer Thread

I have a Thread that should wait for tasks to arrive from different multible Threads and execute them until no task is left. If no task is left it should wait again. I tried it with this class (only relevant code): from threading import Event,…
Rafael T
  • 15,401
  • 15
  • 83
  • 144
0
votes
1 answer

process sync using sigusr1 and sigusr2

i am writing a producer process to write a block of data to a file, and a consumer process to read the same block, but the below code gets stuck in an infinite loop, now from my side i think i am having a problem in the consumer process…
Thair Abdalla
  • 279
  • 1
  • 5
  • 16
0
votes
1 answer

Producer consumer in java multithreading

I have my own implementation of producer and consumer in java multithreading. But consumer gets the product before producer puts the product. How to overcome this using wait and notify. package prod; public class InitProCon { volatile static int…
seenome
  • 80
  • 1
  • 9
0
votes
2 answers

Producer Consumer program using multithreading

I am trying to implement standard Producer Consumer problem using java. I done some code to do it. Here is the code: Producer Class: class Producer implements Runnable { public Producer() { new Thread(this,"Producer").start(); } public…
Nirav Kamani
  • 3,192
  • 7
  • 39
  • 68
0
votes
1 answer

waking up consumer ever 3 sec and taking 6 chunks of data from producer in rabbit mq

I have written following working producer consumer code for rabbit mq in python. But I have a twist in it. The consumer is continuously putting data in the queue ever 0.5 sec but now i want my consumer to wake up every 3 sec and take all the 6 data…
Shweta B. Patil
  • 1,131
  • 2
  • 11
  • 19
0
votes
1 answer

Why Won't my producer/consumer wake up? C

So i'm working on this code that is a producer-consumer code. It runs completely through the program, it just never actually executes anything in the critical section because it never really wakes up from sleep! I've added print statements…
angyxpoo
  • 193
  • 2
  • 5
  • 16
0
votes
1 answer

A consumer needs to know that all messages have been handled, before dealing with the data they contain

If I have a stateless message consumer, which is waiting for several asynchronous messages to come, how can I make sure that I have received all the messages, before I start working with the data that I get from them? In other words, I would either…
Preslav Rachev
  • 3,983
  • 6
  • 39
  • 63
0
votes
0 answers

Mixing Producer-Consumer with Wait-All-One-By-One (WhenAny)

I've been working on my first TPL project recently, and I'm trying to mix a couple of patterns that apparently aren't used together very much. Given a huge volume of IO-bound http request tasks, and decent but not unlimited concurrent server…
KurtisR
  • 45
  • 5
0
votes
0 answers

Producer Consumer: Program received signal SIGSEGV, Segmentation Fault

I am trying to write the Operating systems producer consumer problem in C. I am getting the above mentioned "Segmentation Fault". Its hard to explain everything here, so I have posted the code on github. A look would give you a better picture of…
0
votes
1 answer

Producer-Consumer Log File Output Duplication

I am having some issues with my implementation of the producer-consumer problem. It seems to run fine for the most part, but when I implemented a log file for the capture of critical events, I realized that there was some duplication of events.…
Don
  • 87
  • 6
0
votes
2 answers

Java multithreading queries on Producer Consumer application

I have written a simple java multithreading program. I am having some questions regarding the code. Please help me with these questions. Thanks in advance! Here is my code: Producer.java package com.prodcon; import java.util.Stack; public class…
Nullpointer
  • 1,086
  • 7
  • 20
0
votes
2 answers

How to Assign a Thread to a Type of Data?

I have a stream of sequential data with two types. And, there are an observer subscribed (listen) for each type of data. Observer observer1 = new Observer() { @Override public void next(Data data) { //Do something } } Observer…
Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
0
votes
1 answer

Producer-consumer and how it works

Can anyone explain the producer-consumer problem to me when solved using semaphores? More specifically, I'm having trouble understanding what happens when the order of ups and downs is changed, both in the producer code and consumer. semaphore…
SGWF
  • 51
  • 1
  • 3
  • 10