Monday, 19 November 2012

OS PRODUCER CONSUMER PROBLEM USING SEMAPHORES



#include<stdio.h>
#define BUFFER_SIZE 3
int buffer[BUFFER_SIZE],item;
int fillCount = 0; // items produced
int emptyCount = BUFFER_SIZE; // remaining space
producer() {
    while (1) {
        item = produceItem();
      if(item==0)
      return;
      if(emptyCount==0)
      {printf("\nbuffer full");
      break;
      }
        emptyCount--;
           buffer[emptyCount]=item;
      printf("\n %d put into the buffer..",item);

        fillCount++;
     
    }
}

consumer() {
    while (1) {
      if(fillCount==0)
      {printf("\nbuffer empty");
      break;
      }
        fillCount--;
      item=buffer[emptyCount];
            printf("\n %d removed from buffer..",item);
        emptyCount++;
        printf("\n %d consumed..",item);
     
    }
}
int produceItem()
{
int n;
printf("\n Enter the number for production (0 to stop)..\n");
scanf("%d",&n);
return n;
}
int main()
{
char i;
while(i!='y' && i!='Y')
{
producer();
consumer();
printf("\nDo you want to exit...(Y or N)....");
scanf("%c",&i);
}
return 1;
}

No comments:

Post a Comment