B424 Parallel and Distributed Programming
Producers and Consumers
-
A producer computes and outputs a stream of results.
-
A consumer inputs and analyzes a stream of values.
-
A pipeline can be seen as a sequence of producers-consumers. Each
process in a pipeline is a filter that consumes the output of the
previous process and produces input for the next process. Example:
Unix pipelines
ls -Al /usr/include/ | more
General model
-
Suppose that the data are in a stack of limited size (an array). We will
suppose that the order in which the data are processed doesn't matter.
-
There is an index, counter, that tells us which is the size of the current
data.
-
The data is supposed to be accessible to all of the processes - shared
memory.
-
For message-passing, the data will be manipulated by the master, which
can be implemented as a server.
// global variables
some_type data[max];
int counter = 0;
Producer()
{
while (true) {
item = Produce_item();
while (!Deposit(item)) ;
}
}
Consumer()
{
while (true) {
while (!Fetch(item));
Consume(item);
}
}
int Deposit(some_type item)
{
if (Data_is_full())
return 0;
else {
data[counter++] = item;
return 1;
}
}
int Fetch(some_type &item)
{
if (Data_is_empty())
return 0;
else {
item = data[--counter];
return 1;
}
}
int Data_is_full()
{ return (counter == max); }
int Data_is_empty()
{ return (counter == 0); }
The code of the program using pthreads:
producer_consumer.cc
producer_consumer.h