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

Single Producer and Consumer example in Thinking In Java 4

In TIJ4 P1208, there is one consumer (WaitPerson) and one producer (Chef). Each synchronize on itself when checking if there is meal available. I think they should synchronize on the meal instead. Otherwise, when the waitperson is checking if the…
sxy
  • 187
  • 1
  • 3
  • 10
0
votes
1 answer

Put method into a buffer in C++

I have created this method to put some data in a buffer: template void locked_buffer::put(const T & x, bool last) noexcept { using namespace std; unique_lock l{mut_}; not_full_.wait(l, [this] { return !do_full(); }); …
giorgioW
  • 331
  • 5
  • 20
0
votes
0 answers

How to keep UI responsive when consuming items produced by background thread producer?

I've offloaded a long-running, synchronous, operation to a background thread. It takes a while to get going, but eventually it starts producing items very nicely. The question is then how to consume then - while maintaining a responsive UI (i.e.…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
0
votes
1 answer

Queue with lock vs Lock Free Queue

I am involved a task of listening to a web service. Which will send a xml data through push service. The data have to undergo some calculation and then it will we displayed. I have planned to use queue to store the data by service listener and read…
Kid
  • 169
  • 1
  • 19
0
votes
1 answer

POSIX Producer-consumer

I have a problem connected with producers and consumers. I have 1 producer and 3 consumers. Producer produce letters which i put in my queue, and consumers take this letters. It is considered that letter has been removed from the queue, when 2 of…
koshachok
  • 460
  • 5
  • 19
0
votes
1 answer

Producer-Consumer producer creating 2 elements POSIX Semaphores

3 Consumers 2 producers. Reading and writing to one buffer. Producer A is pushing 1 element to buffer (length N) and Producer B is pushing 2 elements to buffer. No active waiting. I can't use System V semaphores. Sample code for producer A: void…
Aaroneiros
  • 111
  • 7
0
votes
1 answer

pthreads producer-consumer deadlock

I wrote following code: void *produce(void* arg) { buffer* buff = (buffer *) arg; while (1) { pthread_mutex_lock(&mutex); if (elements_produced == JOB_SIZE) { pthread_mutex_unlock(&mutex); pthread_exit(NULL); } …
Klark
  • 8,162
  • 3
  • 37
  • 61
0
votes
1 answer

Why are two semaphores and one mutex required in solving bounded buffer instance of producer-consumer?

Why must one use a mutex in addition to a semaphore when using a bounded buffer in producer consumer problem?
Varun Rao
  • 393
  • 1
  • 5
  • 19
0
votes
1 answer

reduce CPU overhead while proccessing Video Stream

I am developing C# WPF Auto Number Plate Recognition Using an OCR. The Flow is, i am getting a pictures from a video stream MJPEG and this images should be passed to the OCR to get the plate number and other details. The problem is : the Video…
0
votes
1 answer

Producer consumer - what to lock

I have many producers that producer/consumer objects of different types, e.g. ProducerOfX, ProducerOfY, ConsumerOfX, ConsumerOfY. The producer puts an an object (X or Y) onto a queue, and notifies its relevant consumer (ProducerOfX notifies…
TheCoder
  • 8,413
  • 15
  • 42
  • 54
0
votes
1 answer

Producer Consumer Misunderstanding in Threading

I want the ProducerThread to produce random values upto 10 and then expect ConsumerThread to consumer those values of Queue. Somewhere Producer is generating adding the values more than once. I have a concept that when we call notify on an object…
Imam Bux
  • 1,006
  • 11
  • 27
0
votes
0 answers

Add priority to producer-consumer design pattern

I have implemented consumer-producer with concurrentLinkedList in java. I need to add a request to the head of the queue instead od its tail, in order to set priority. any idea ? thanks
D Azad
  • 1
0
votes
0 answers

Kafka produce or consume msg failed from windows clients to remote Linux servers

I both download kafka_2.10-0.10.0.1 into my windows and my linux machine(I have a cluster which has 3 linux machines ,192.168.80.128/129/130).So , i use my windows machine as a kafka client and linux machines as kafka servers. I try to produce msg…
wuchang
  • 3,003
  • 8
  • 42
  • 66
0
votes
0 answers

Expanding to consumer to cater for each producer?

I have the following socket connection codes. Where for every connection is one new thread. After receiving the data which is done via the producer then I put into a queue for next database processor which is the consumer. This works fine but the…
user5313398
  • 713
  • 3
  • 9
  • 28
0
votes
1 answer

Producer/consumer in Java: keeping it balanced

I have a text processing application in Java, which reads a file chunk by chunk (~100000 lines) and processes each chunk in a separate thread. It works well, but there is an issue. Reading lines is much faster than processing them and program ends…
Denis Kulagin
  • 8,472
  • 17
  • 60
  • 129