boundedbuffer
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
boundedbuffer [2007/09/21 20:10] – mark | boundedbuffer [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). | + | easier). |
consumer. | consumer. | ||
have already been defined. | have already been defined. | ||
and producer. | and producer. | ||
- | <code c> | + | <code c> |
void consumer() | void consumer() | ||
{ | { | ||
Line 208: | Line 208: | ||
</ | </ | ||
+ | Another variation. | ||
+ | <code c> | ||
+ | #define BUFFERSIZE 200 | ||
+ | |||
+ | int get() | ||
+ | { | ||
+ | int i,j, data; | ||
+ | in(" | ||
+ | rd(" | ||
+ | #wait when the buffer is empty | ||
+ | while (i==j) rd(" | ||
+ | in(" | ||
+ | i=(i+1)%BUFFERSIZE; | ||
+ | out(" | ||
+ | return data; | ||
+ | } | ||
+ | |||
+ | void put(int data) | ||
+ | { | ||
+ | int i,j; | ||
+ | in(" | ||
+ | rd(" | ||
+ | #wait when the buffer is full | ||
+ | while ( j-i==BUFFERSIZE-1 || i-j==1 ) rd(" | ||
+ | j=(j+1)%BUFFERSIZE; | ||
+ | out(" | ||
+ | out(" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Another Version | ||
+ | |||
+ | <code c> | ||
+ | |||
+ | #define SIZE 100 | ||
+ | |||
+ | Void Initialize() | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | int get() | ||
+ | { | ||
+ | int head, tail, data; | ||
+ | in(" | ||
+ | rd(" | ||
+ | while(tail == head) rd(" | ||
+ | in(" | ||
+ | out(" | ||
+ | return data; | ||
+ | } | ||
+ | |||
+ | void put(int data) | ||
+ | { | ||
+ | int head,tail; | ||
+ | in(" | ||
+ | rd(" | ||
+ | while( head == (tail+1)%SIZE ) rd(" | ||
+ | out(" | ||
+ | out(" | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | <code c> | ||
+ | |||
+ | // i1 and i2 are initialized to 0 | ||
+ | void put(int data) | ||
+ | { | ||
+ | int i1, i2; | ||
+ | read(" | ||
+ | in(" | ||
+ | if(i1==size)i1=0; | ||
+ | while(i1+1==i2 || (i1+1==size && i2==0)) | ||
+ | | ||
+ | out(" | ||
+ | out(" | ||
+ | } | ||
+ | |||
+ | |||
+ | int get() | ||
+ | { | ||
+ | int i2, data; | ||
+ | in(" | ||
+ | if(i2==size)i2=0; | ||
+ | in(" | ||
+ | out(" | ||
+ | return data; | ||
+ | } | ||
+ | |||
+ | </ |
boundedbuffer.1190405422.txt.gz · Last modified: 2007/09/21 20:10 by mark