0% found this document useful (0 votes)
54 views2 pages

Lab 7

This document describes a C program that implements the producer-consumer problem using semaphores. The program uses mutex, full, and empty semaphores to control access to a shared buffer between a producer and consumer. The producer adds items to the buffer when it is not full, and the consumer removes items when the buffer is not empty. The output shows the producer and consumer taking turns accessing the buffer and printing messages to indicate producing and consuming items from the shared buffer.

Uploaded by

Life Pubg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views2 pages

Lab 7

This document describes a C program that implements the producer-consumer problem using semaphores. The program uses mutex, full, and empty semaphores to control access to a shared buffer between a producer and consumer. The producer adds items to the buffer when it is not full, and the consumer removes items when the buffer is not empty. The output shows the producer and consumer taking turns accessing the buffer and printing messages to indicate producing and consuming items from the shared buffer.

Uploaded by

Life Pubg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

*****Producer/Consumer problem using Semaphore*****

C CODE :
#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);
}

OUTPUT :
1.Producer
2.Consumer
3.Exit
Enter your choice:1

Producer produces the item 1


Enter your choice:2

Consumer consumes item 1


Enter your choice:2
Buffer is empty!!
Enter your choice:1

Producer produces the item 1


Enter your choice:1

Producer produces the item 2


Enter your choice:2

Consumer consumes item 2


Enter your choice:2

Consumer consumes item 1


Enter your choice:2
Buffer is empty!!
Enter your choice:1

Producer produces the item 1


Enter your choice:1

Producer produces the item 2


Enter your choice:1

Producer produces the item 3


Enter your choice:1
Buffer is empty!!
Enter your choice:3

You might also like