Docsity

producer consumer problem solution, Lecture notes of Operating Systems

Toggle sidebar
bg1
Producer consumer problem is also known as bounded buffer problem. In
this problem we have two processes, producer and consumer, who share a
fixed size buffer. Producer work is to produce data or items and put
in buffer. Consumer work is to remove data from buffer and consume it.
We have to make sure that producer do not produce data when buffer is
full and consumer do not remove data when buffer is empty.
The producer should go to sleep when buffer is full. Next time when
consumer removes data it notifies the producer and producer starts
producing data again. The consumer should go to sleep when buffer is
empty. Next time when producer add data it notifies the consumer and
consumer starts consuming data. This solution can be achieved using
semaphores.
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is
full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is
empty!!");
break;
case 3:
exit(0);
break;
}
}
return 0;
}
int wait(int s)
pf2

Partial preview of the text

Download producer consumer problem solution and more Lecture notes Operating Systems in PDF only on Docsity!

Producer consumer problem is also known as bounded buffer problem. In this problem we have two processes, producer and consumer, who share a fixed size buffer. Producer work is to produce data or items and put in buffer. Consumer work is to remove data from buffer and consume it. We have to make sure that producer do not produce data when buffer is full and consumer do not remove data when buffer is empty.

The producer should go to sleep when buffer is full. Next time when consumer removes data it notifies the producer and producer starts producing data again. The consumer should go to sleep when buffer is empty. Next time when producer add data it notifies the consumer and consumer starts consuming data. This solution can be achieved using semaphores.

#include<stdio.h> #include<stdlib.h>

int mutex=1,full=0,empty=3,x=0;

int main() { int n; void producer(); void consumer(); int wait(int); int signal(int); printf("\n1.Producer\n2.Consumer\n3.Exit"); while(1) { printf("\nEnter your choice:"); scanf("%d",&n); switch(n) { case 1: if((mutex==1)&&(empty!=0)) producer(); else printf("Buffer is full!!"); break; case 2: if((mutex==1)&&(full!=0)) consumer(); else printf("Buffer is empty!!"); break; case 3: exit(0); break; } }

return 0; }

int wait(int s)

return (--s); }

int signal(int s) { return(++s); }

void producer() { mutex=wait(mutex); full=signal(full); empty=wait(empty); x++; printf("\nProducer produces the item %d",x); mutex=signal(mutex); }

void consumer() { mutex=wait(mutex); full=wait(full); empty=signal(empty); printf("\nConsumer consumes item %d",x); x--; mutex=signal(mutex); }