0% found this document useful (0 votes)
91 views4 pages

Producer-Consumer Problem with Semaphores

The document outlines an experiment focused on implementing the Producer-Consumer problem using semaphores, highlighting the importance of process synchronization. It includes a program code example, explaining the roles of the producer and consumer, as well as semaphore functionality. The experiment concludes with the successful implementation of the program and provides references for further reading.

Uploaded by

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

Producer-Consumer Problem with Semaphores

The document outlines an experiment focused on implementing the Producer-Consumer problem using semaphores, highlighting the importance of process synchronization. It includes a program code example, explaining the roles of the producer and consumer, as well as semaphore functionality. The experiment concludes with the successful implementation of the program and provides references for further reading.

Uploaded by

Ria Dcosta-036
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Experiment-6

Name Roll No Branch


Leander Braganza 05 COMPS Batch A

Experiment - 6

Producer Consumer using Semaphores

Learning Write a program to implement solution of Producer consumer


Objective: problem through Semaphore

Learning Students will be able to understand process synchronization


Outcome:

Course Outcome: CSL403.4

Program (PO 3) Design/ development of solutions: Breadth and uniqueness of


Outcome: engineering problems i.e. the extent to which problems are original and to
which solutions have previously been identified or codified

(PO 12) Life Long Learning

Bloom's Analysis,Create
Taxonomy Level:
Theory:

In the producer-consumer problem, there is one Producer who


produces things, and there is one Consumer who consumes the
products which are produced by the producer. The producers and
consumers share the same fixed-size memory buffer.
The Producer-Consumer problem is a classic multi-process
synchronization problem, which implies we're aiming to synchronize
many processes.
When the consumer is consuming an item from the buffer, the producer
should not add items into the buffer, and vice versa. As a result, only
one producer or consumer should access the buffer at a time. This
scenario seems to be a problem, let’s discuss all the scenarios which
can cause problems to the system.
 Producer:- In operating System Producer is a process which is
able to produce data/item.

 Consumer:- Consumer is a Process that is able to consume the


data/item produced by the Producer.

 Semaphore:- Semaphores are variables used to indicate the


number of resources available in the system at a particular time.
semaphore variables are used to achieve `Process
Synchronization

Algorithm : -

Data Set : -

Program Code: #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("\[Link]\[Link]\[Link]");
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);
}

Outcome : Producer-Consumer Output


Conclusion : The given program is successfully implemented

References: 1. [Link]/codestudio/library/producer-consumer-
problem-using-semaphores
2. [Link]
consumer-problem-in-os/

You might also like