User Tools

Site Tools


boundedbuffer

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
boundedbuffer [2007/09/24 17:16] jyangboundedbuffer [2007/09/25 05:20] (current) nshafiei
Line 1: Line 1:
 How do you implement a bounded FIFO buffer in C-Linda? How do you implement a bounded FIFO buffer in C-Linda?
 Assume the buffer contains integers (to make life a little Assume the buffer contains integers (to make life a little
-easier).  There are a number of producers and a number of+ easier).  There are a number of producers and a number of
 consumer.  Assume that the functions consume and produce consumer.  Assume that the functions consume and produce
 have already been defined.  Now consider the functions consumer have already been defined.  Now consider the functions consumer
 and producer. and producer.
  
-<code c>+ <code c>
 void consumer()  void consumer() 
 { {
Line 216: Line 216:
   int i,j, data;   int i,j, data;
   in("head",?i);   in("head",?i);
 +  rd("tail",?j);
 +  #wait when the buffer is empty
 +  while (i==j) rd("tail",?j);
   in("buffer",i,?data);   in("buffer",i,?data);
   i=(i+1)%BUFFERSIZE;   i=(i+1)%BUFFERSIZE;
Line 221: Line 224:
   return data;   return data;
 } }
- + 
 void put(int data) void put(int data)
 { {
Line 227: Line 230:
   in("tail",?j);   in("tail",?j);
   rd("head",?i);   rd("head",?i);
-  #loop when the buffer is full+  #wait when the buffer is full
   while ( j-i==BUFFERSIZE-1 || i-j==1 ) rd("head",?i);   while ( j-i==BUFFERSIZE-1 || i-j==1 ) rd("head",?i);
   j=(j+1)%BUFFERSIZE;   j=(j+1)%BUFFERSIZE;
Line 233: Line 236:
   out("tail",j);   out("tail",j);
 } }
 +</code>
 +
 +Another Version
 +
 +<code c>
 +
 +#define SIZE 100
 +
 +Void Initialize()
 +{
 + out("head", 0);
 + out("tail", 0);
 +}
 +
 +int get()
 +{
 +  int head, tail, data;
 +  in("head",?head);
 +  rd("tail",?tail);
 +  while(tail == head) rd("tail",?tail);
 +  in("data",?data, head);
 +  out("head",(head+1)%SIZE);
 +  return data;
 +}
 + 
 +void put(int data)
 +{
 +  int head,tail;
 +  in("tail",?tail);
 +  rd("head",?head);
 +  while( head == (tail+1)%SIZE ) rd("head",?head);
 +  out("data",data, tail);
 +  out("tail",(tail+1)%SIZE);
 +}
 +
 +</code>
 +
 +
 +
 +<code c>
 +
 +// i1 and i2 are initialized to 0
 +void put(int data)
 +{
 +      int i1, i2;
 +      read("consumer", ?i2);
 +      in("producer",?i1);
 +      if(i1==size)i1=0;      
 +      while(i1+1==i2 || (i1+1==size && i2==0))
 +         read("consumer",?i2);      
 +      out("data", data, i1); 
 +      out("producer", i1+1);        
 +}
 +
 +
 +int get()
 +{
 +      int i2, data;
 +      in("consumer",?i2);
 +      if(i2==size)i2=0;
 +      in("data",?data,i2);
 +      out("consumer", i2+1);       
 +      return data; 
 +}
 +
 </code> </code>
boundedbuffer.1190654206.txt.gz · Last modified: 2007/09/24 17:16 by jyang

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki