User Tools

Site Tools


c-linda

Let us implement a bounded FIFO buffer in C-Linda. There are a number of producers and a number of consumer. Assume that the functions consume and produce have already been defined. Now consider the functions consumer and producer.

void consumer() 
{
   int data;
   while (1) 
   {
      data = get();
      consume(data);
   }
}
 
void producer() 
{
   int data;
   while (1) 
   {
      data = produce();
      put(data);
   }
}

The functions get and put can be defined as follows.

int get()
{
   int data;
   int index;
   in("head", ?index);
   in("full", index, ?data);
   out("head", ++index);
   out("empty");
   return data;
}
 
void put(int data)
{
   int index;
   in("empty");
   in("tail", ?index);
   out("full", index, data);
   out("tail", ++index);
}

A number of consumers and producers can be created as follows.

#define CONSUMER 10
#define PRODUCER 5
#define CAPACITY 25
 
void real_main() 
{
   for (int i = 0; i < CAPACITY)
   {
      out("empty");
   }
   out("head", 0);
   out("tail", 0);
 
   for (int i = 0; i < CONSUMER; i++)
   {
      eval("consumer", consumer());
   }
   for (int i = 0; i < PRODUCER; i++)
   {
      eval("producer", producer());
   }
}
c-linda.txt · Last modified: 2007/09/30 16:24 by franck

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki