boundedbuffer
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
boundedbuffer [2007/09/24 17:16] – jyang | 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 216: | Line 216: | ||
int i,j, data; | int i,j, data; | ||
in(" | in(" | ||
+ | rd(" | ||
+ | #wait when the buffer is empty | ||
+ | while (i==j) rd(" | ||
in(" | in(" | ||
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(" | in(" | ||
rd(" | rd(" | ||
- | #loop when the buffer is full | + | #wait when the buffer is full |
while ( j-i==BUFFERSIZE-1 || i-j==1 ) rd(" | while ( j-i==BUFFERSIZE-1 || i-j==1 ) rd(" | ||
j=(j+1)%BUFFERSIZE; | j=(j+1)%BUFFERSIZE; | ||
Line 233: | Line 236: | ||
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.1190654206.txt.gz · Last modified: 2007/09/24 17:16 by jyang