User Tools

Site Tools


boundedbuffer

This is an old revision of the document!


How do you implement a bounded FIFO buffer in C-Linda? Assume the buffer contains integers (to make life a little easier). 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);
   }
}

A single consumer and producer can be created as follows.

void real_main() 
{
   eval("consumer", consumer());
   eval("producer", producer());
}

How can the above be extended to create multiple consumers and producers?

It remains to define the functions get and put.

int get()
{
   int data;
 
 
 
   return data;
}
 
void put(int data)
{
 
}

How should get and put be completed?


A try for comment:

<code c>

void down_mutex() {

in("mutex", 1);
out("mutex", 0);

}

void up_mutex() {

in("mutex", 0);
out("mutex", 1);

}

int get() {

int i, data;
in("full");
down_mutex();
rd("size",?i);
in("head",?j);
in("buffer",j,?data);
j=(j+1)%i;
out("head",j);
up_mutex();
out("empty")
return data;

}

void put(int data) {

int i,j;
in("empty");
down_mutex();
rd("size",?i);
in("tail",?j)
j=(j+1)%i;
out("tail",j);
out("buffer",j,?data);
up_mutex();
out("full");
return;

}

void consumer() {

int data;
while (1)
{
  data = get();
  consume(data);
}

}

void producer() {

int data;
while (1)
{
  data = produce();
  put(data);
}

} void real_main() {

#define Len 10
#define Run 5
for (int i =0 ; i<Len; i++)
  out("empty");
out("head",1);
out("tail", 1);
out("size",len);
out("mutex",1);
for (int i =0 ; i<5; i++){
  eval("consumer", consumer());
  eval("producer", producer());

} </code c>

boundedbuffer.1190384703.txt.gz · Last modified: 2007/09/21 14:25 by zypan

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki