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/21 14:31] zypanboundedbuffer [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 80: Line 80:
 int get() int get()
 { {
-  int i, data;+  int i,j, data;
   in("full");   in("full");
   down_mutex();   down_mutex();
Line 144: Line 144:
 </code> </code>
  
 +variation of the same concept
 +
 +<code c>
 +
 +#define SEMAPHORE        "semaphore"
 +#define PUT_COUNTER      "put"
 +#define GET_COUNTER      "get"
 +
 +//---------------------- counter API ---------------------
 +void counter_init(char * conterType) {
 + out(conterType,NULL);
 +}
 +
 +
 +int counter_next(char * conterType) {
 + int seq;
 + in(conterType,?seq);
 + out(conterType,++seq);
 + return seq;
 +}
 +
 +//-------------------- semaphore API -----------------
 +
 +void semaphore_init (int counter) {
 + for (int i = 0; i < counter; i++ )
 + out(SEMAPHORE); 
 +}
 +
 +void semaphore_acquire() {
 + in(SEMAPHORE);
 +}
 +
 +void semaphore_release() {
 + out(SEMAPHORE);
 +}
 +
 +//-------------------------------------------------
 + 
 +void put(int data) {
 + semaphore_acquire();
 + int seq_id = counter_next(PUT_COUNTER);
 + out(seq_id,data); 
 +}
 +
 +int get() {
 + int seq_id = counter_next(GET_COUNTER);
 + in(seq_id,?data);
 + semaphore_release();
 + return data;
 +}
 +
 +void real_main() 
 +{
 +   semaphore_init(BUFFER_SIZE);
 +
 +   counter_init(GET_COUNTER);
 +   counter_init(PUT_COUNTER);
 +
 +   eval("consumer", consumer());
 +   eval("producer", producer());
 +}
 +
 +</code>
 +
 +Another variation.
 +<code c>
 +#define BUFFERSIZE 200
 +
 +int get()
 +{
 +  int i,j, data;
 +  in("head",?i);
 +  rd("tail",?j);
 +  #wait when the buffer is empty
 +  while (i==j) rd("tail",?j);
 +  in("buffer",i,?data);
 +  i=(i+1)%BUFFERSIZE;
 +  out("head",i);
 +  return data;
 +}
 + 
 +void put(int data)
 +{
 +  int i,j;
 +  in("tail",?j);
 +  rd("head",?i);
 +  #wait when the buffer is full
 +  while ( j-i==BUFFERSIZE-1 || i-j==1 ) rd("head",?i);
 +  j=(j+1)%BUFFERSIZE;
 +  out("buffer",j,data);
 +  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>
boundedbuffer.1190385091.txt.gz · Last modified: 2007/09/21 14:31 by zypan

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki