boundedbuffer
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
boundedbuffer [2007/09/20 01:25] – created franck | 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 61: | Line 61: | ||
How should get and put be completed? | How should get and put be completed? | ||
+ | ---- | ||
+ | A try for comment: | ||
+ | |||
+ | <code c> | ||
+ | |||
+ | void down_mutex() | ||
+ | { | ||
+ | in(" | ||
+ | out(" | ||
+ | } | ||
+ | |||
+ | void up_mutex() | ||
+ | { | ||
+ | in(" | ||
+ | out(" | ||
+ | } | ||
+ | |||
+ | int get() | ||
+ | { | ||
+ | int i,j, data; | ||
+ | in(" | ||
+ | down_mutex(); | ||
+ | rd(" | ||
+ | in(" | ||
+ | in(" | ||
+ | j=(j+1)%i; | ||
+ | out(" | ||
+ | up_mutex(); | ||
+ | out(" | ||
+ | return data; | ||
+ | } | ||
+ | |||
+ | void put(int data) | ||
+ | { | ||
+ | int i,j; | ||
+ | in(" | ||
+ | down_mutex(); | ||
+ | rd(" | ||
+ | in(" | ||
+ | out(" | ||
+ | j=(j+1)%i; | ||
+ | out(" | ||
+ | up_mutex(); | ||
+ | out(" | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | void consumer() | ||
+ | { | ||
+ | int data; | ||
+ | while (1) | ||
+ | { | ||
+ | data = get(); | ||
+ | consume(data); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void producer() | ||
+ | { | ||
+ | int data; | ||
+ | while (1) | ||
+ | { | ||
+ | data = produce(); | ||
+ | put(data); | ||
+ | } | ||
+ | } | ||
+ | void real_main() | ||
+ | { | ||
+ | #define Len 10 | ||
+ | #define Run 5 | ||
+ | for (int i =0 ; i<Len; i++) | ||
+ | out(" | ||
+ | out(" | ||
+ | out(" | ||
+ | out(" | ||
+ | out(" | ||
+ | |||
+ | for (int i =0 ; i<5; i++){ | ||
+ | eval(" | ||
+ | eval(" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 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.1190251524.txt.gz · Last modified: 2007/09/20 01:25 by franck