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 14:26] – zypan | 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 80: | Line 80: | ||
int get() | int get() | ||
{ | { | ||
- | int i, data; | + | int i,j, data; |
in(" | in(" | ||
down_mutex(); | down_mutex(); | ||
Line 100: | Line 100: | ||
rd(" | rd(" | ||
in(" | in(" | ||
+ | out(" | ||
j=(j+1)%i; | j=(j+1)%i; | ||
out(" | out(" | ||
- | out(" | ||
up_mutex(); | up_mutex(); | ||
out(" | out(" | ||
Line 144: | Line 144: | ||
</ | </ | ||
+ | variation of the same concept | ||
+ | |||
+ | <code c> | ||
+ | |||
+ | #define SEMAPHORE | ||
+ | #define PUT_COUNTER | ||
+ | #define GET_COUNTER | ||
+ | |||
+ | // | ||
+ | void counter_init(char * conterType) { | ||
+ | out(conterType, | ||
+ | } | ||
+ | |||
+ | |||
+ | int counter_next(char * conterType) { | ||
+ | int seq; | ||
+ | in(conterType,? | ||
+ | out(conterType, | ||
+ | return seq; | ||
+ | } | ||
+ | |||
+ | // | ||
+ | |||
+ | 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, | ||
+ | } | ||
+ | |||
+ | int get() { | ||
+ | int seq_id = counter_next(GET_COUNTER); | ||
+ | in(seq_id,? | ||
+ | semaphore_release(); | ||
+ | return data; | ||
+ | } | ||
+ | |||
+ | void real_main() | ||
+ | { | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | 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.1190384785.txt.gz · Last modified: 2007/09/21 14:26 (external edit)