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 using Semaphores. Consumer gets tail, not head of queue

As the title suggests, this is a Producer/Consumer implementation using Semaphores. There are no deadlocking issues, but it seems whenever the Consumer looks to remove the next item in the queue, it is always the tail. Pseudo-code: class Consumer…
ryuu9187
  • 1
  • 1
0
votes
1 answer

Multiprocessed producer consumer audio player with buffering

I need to implement audio player that could deal with jitter. So I need buffering and hence I need to have minimal buffer size and to know how much elements are in buffer at the time. But in python Queue qsize() method is not implemented. What can I…
user1685095
  • 5,787
  • 9
  • 51
  • 100
0
votes
1 answer

Implementing producers/consumers with bacon.js event stream pool

I want to implement the producer consumer pattern with a pool of bacon.js event streams. Here's the specific problem I'm trying to solve: I have a list of 'n' urls. I want to create event streams to make http requests for those urls, but I want to…
tldr
  • 11,924
  • 15
  • 75
  • 120
0
votes
1 answer

Locking in Multiple producer single consumer

I am trying to solve a multiple producer single consumer problem with the producers generating integers and the consumer making a sorted list out of all these integers(using AVL tree). How should I be locking my tree data structure? Or is it…
user2951692
0
votes
1 answer

what's wrong with my java(producer-consumer) code?

I wrote this java code with synchronized and busy waiting but I don't know why it doesn't work right! for example I get this output produced:6 produced:7 consumed:0 consumed:0 how 6 and 7 are produced but 0 is consumed ??? I don't know what's wrong…
0
votes
2 answers

Product-consumer with semaphores and forks

Don't know why is consumer doing all the work? I create a semaphor for the prodcut-consumer with an array of 10 integers, the array fills with names and it is return in ones and zeros (binary).The consumer is called even when the producer is unsing…
user3606231
  • 11
  • 1
  • 1
0
votes
0 answers

coding a simple server - 1 producer, multiple consumer

I'm attempting to create a simple server using the producer/consumer problem, pthreads, and mutexes/condition variables. My server accepts a port number, number of threads to create, how big the buffer should be, and what type of scheduling it…
katiea
  • 223
  • 1
  • 5
  • 19
0
votes
1 answer

How do I get a producer and a consumer thread to continuously load and unload a shared buffer?

I'd like to have two threads, a producer and a consumer, which continuously fill and empty a shared buffer. The code is a little pedantic, but I'm going to expand on it later to include actual stop conditions. Here is my situation: I have started…
rafafan2010
  • 1,559
  • 2
  • 14
  • 23
0
votes
0 answers

Task doesn't work parallel

I have two task which work as producer and consumer, but they don't run in parallel. Second one waits for the first one to compelete. Can you explain why and how can I correct this? I want both of them run at the same time. (I try many varians, but…
beta-tank
  • 390
  • 1
  • 4
  • 17
0
votes
1 answer

File copy using C with semaphores and threads, for consumer/producer

I'm required to do as the question title states. The restriction is that the consumer and producer have only 12 slots to put up to 20 chars into. I've got it working except that the last producer puts in only 13 chars (this is correct), and the last…
codenaugh
  • 857
  • 2
  • 12
  • 27
0
votes
0 answers

producer consumer threads using thread and condition

i am try to understand this code of producer&consumer using threads,since i am new in multi-threading programming: #include #include #include #include #define SIZE 10 pthread_mutex_t region_mutex =…
0
votes
1 answer

Producer/Consumer - Calculate average time

I have 4 class - Producer, Consumer, Shop, Main. Trying to calculate average waiting time which customers are in the queue for waiting to enter the store. My run method in Producer - public void run() { for (int i = 1; i < size; i++) { …
0
votes
1 answer

Using EventMachine to setup infinite loop to generate random data

I'm trying to setup an automated stress test, and feed the randomly generated data into Redis, and then have the consumer (as another component read from Redis) to process the random data. To simulate the randomly generated data as close to…
samxiao
  • 2,587
  • 5
  • 38
  • 59
0
votes
1 answer

Inefficient Python Scipy Matrix Addition in Multi-threaded Program

I want to process a huge text corpus, i have written two classes which a main class is calling.I have removed some fields and methods in order to be more readable. import queue import threading class Vectorizer(): def __init__(self,…
Ash
  • 3,428
  • 1
  • 34
  • 44
0
votes
2 answers

Consumer Producer issue in c

Hey so I'm doing a simple producer problem but for some reason my producer runs through and then my consumer runs, even though my producer should be stopped once it can't get the semaphore initialized to 15: #define _REENTRANT #define BUFFER_SIZE…
Dante Hoyte
  • 333
  • 2
  • 15