Bounded buffer problem solution using semaphores by drg59916

VIEWS: 0 PAGES: 1

									// Bounded buffer problem solution using semaphores – pclab5.cm
#include "bddbuff.inc"
const int ValueRange = 20;           // 0-19 will be produced and consumed
semaphore to;                        // exclusive access to output
semaphore s;                         // mutual exclusion for buffer
semaphore n;                         // # consumable items in buffer
semaphore e;                         // # empty spaces in buffer
int produce(char id)        {
          int tmp;
          tmp = random(ValueRange);
          wait(to);
          cout << "Producer " << id << " produces " << tmp << endl;
          signal(to);
          return tmp;
                            }
void consume(char id, int i)         {
          wait(to);
          cout << "Consumer " << id << " consumes " << i << endl;
          signal(to);
                                     }
void producer(char id)      {
          int i;
          for (;;) {        //loop forever - can't use for loop here!
           i = produce(id);
// FILL IN TWO SEMAPHORE OPERATIONS:
          append(i);
//FILL IN TWO MORE SEMAPHORE OPERATIONS:
                  }
                           }
void consumer(char id)     {                                                 //bddbuff.inc
        int i;                                                               const int SizeOfBuffer = 5;
        for (;;) {         //loop forever - can't use for loop here!         int buffer[SizeOfBuffer];
                                                                             int in = 0;       //index to use for next append
// FILL IN TWO SEMAPHORE OPERATIONS:                                         int out = 0;      //index to use for next take
          i = take();
                                                                             void append(int v)
//FILL IN TWO MORE SEMAPHORE OPERATIONS:                                     //add v to buffer
          consume(id,i);                                                     {
                }                                                                      buffer[in] = v;
                          }                                                            in = (in + 1) % SizeOfBuffer;
main()                                                                       }
{                                                                            int take()
// INITIALIZE EACH SEMAPHORE PROPERLY                                        //return an item from the buffer
        initialsem(s,?);                                                     {
        initialsem(n,?);                                                               int tmp;
        initialsem(e,?);                                                               tmp = buffer[out];
        initialsem(to,?);                                                              out = (out + 1) % SizeOfBuffer;
        cobegin {                                                                      return tmp;
                  producer('A'); producer('B');                              }
                  consumer('x'); consumer('y'); consumer('z');
                  }
}

								
To top