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
0 answers

Multiple producer multiple consumer but consumer should execute serially

I have case in that we need to have multiple producer and multiple consumer. Producers & consumers should run on separate Task. When I googled it I saw multiple options available in C# like BlockingCollection, DataFlow, Queue etc. I have tried all…
CSharpDev
  • 370
  • 1
  • 7
  • 28
0
votes
2 answers

notifyAll in java

Is it wrong to use notifyAll() inside the loop in a multiple producer-consumer problem in Java? Here is the code snippet which I am talking about. public void run() { while(producer_counter <= 64) { synchronized(list) { …
Addy
  • 1
0
votes
1 answer

Passing data to function from different thread

I have long-running function in my C++ 11 application that is essentially sub_main. I need to inform this function about situation risen in different thread. Requirements: The situation can be risen at any time The application should handle the…
PiotrK
  • 4,210
  • 6
  • 45
  • 65
0
votes
0 answers

Producer/Consumer with two pipes - consumer doesn't write anything

After producing SIZE items, the producer process should write to the fd_pipe_p2c pipe and be read by the consumer process, which then outputs what it is consuming. After that the consumer should write back to the producer via the pipe_fd_c2p pipe to…
Nathan
  • 246
  • 1
  • 4
  • 10
0
votes
1 answer

FreeRTOS Task Notification

Quick Architecture Question... There are 4 producers and 1 consumer for a message queue. Each of the 4 producers have their own task and are there to service incoming data from 4 specific hardware communications references. Should the Consumer task…
0
votes
0 answers

Need to avoid blocking calls on a service bus architecture.

I am using python and flask. I am using a microservices built using flask and configured to support a service bus architecture. The microservice is capable of making concurrent service calls and now I need to improve the performance in the service…
Varad
  • 920
  • 10
  • 25
0
votes
1 answer

Will this producer-consumer scenario with semaphores ever deadlock?

I have read through a whole bunch of producer-consumer problems that use semaphores, but I haven't been able to find an answer for this exact one. What I want to know is if this solution will ever deadlock? semaphore loadedBuffer = 0 semaphore…
Hopsain
  • 1
  • 2
0
votes
0 answers

TPL Dataflow - How to ensure one block of work is complete before moving onto next?

I'm still relatively new to TPL Dataflow, and not 100% sure if I am using it correctly or if I'm even suppose to use it. I'm trying to employ this library to help out with file-copying+file-upload. Basically the structure/process of handling files…
wpa
  • 220
  • 3
  • 11
0
votes
4 answers

How to implement producer-consumer using processes?

I'm trying to implement a producer-consumer application using 1 parent process and 1 child process. The program should work like this: 1 - The parent process is the producer and the child process is the consumer. 2 - The producer creates a file,…
sziko
  • 53
  • 2
  • 11
0
votes
3 answers

C++ Producer Consumer, same consumer thread grabs all tasks

I am implementing a producer consumer project in c++, and when I run the program, the same consumer grabs almost all of the work, without letting any of the other consumer threads grab any. Sometimes, other threads do get some work, but then that…
0
votes
0 answers

Java Threads on wait() loses lock but stays inside synchronized block

I tought calling wait() then the thread loses the lock and has to regain it? Is this true? But obviously the Thread doesnt leaves the synchronized block. He is waiting on the same line where wait() was invoked and then moves on and doesnt has to…
0
votes
1 answer

Erlang producers and consumers - strange behaviour of program

I am writing a program that solves producers-consumers problem using Erlang multiprocessing with one process responsible for handling buffer to which I produce/consume and many producers and many consumers processes. To simplify I assume…
Dawid
  • 153
  • 3
  • 18
0
votes
2 answers

Writer/Reader using Monitor not working

I´m doing an assignment where are implementing a Writer/Reader problem that uses a monitor for synchronization. The scenario: The writer should write a string from a list one at a time into a buffer (the buffer can only hold one single string at a…
0
votes
0 answers

Basic multithreaded Consumers algorithm in PowerShell

I want create a queue of ffmpeg commands and then let N threads consume the queue launching an instance of ffmpeg with parameters: here some code: #looping on $items to prepare the params foreach($input in $items){ {...} #adding a param in…
Lamberto Basti
  • 478
  • 1
  • 6
  • 24
0
votes
1 answer

How to copy only newly added elements into std::deque

I am working on a C++ project where in I need to compute average of std::deque data. This is what I have written so far std::deque list1(200,0); std::deque list2; pthread_mutex_t mut; /*thread1 populates list2 say every 50…
Harry
  • 2,177
  • 1
  • 19
  • 33