0% found this document useful (0 votes)
68 views3 pages

Producer-Consumer Problem Updated

Uploaded by

gaithmath
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)
68 views3 pages

Producer-Consumer Problem Updated

Uploaded by

gaithmath
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
You are on page 1/ 3

Producer-Consumer Problem

The producer-consumer problem is an example of a multi-process synchronization


problem. The problem describes two processes, the producer and the consumer
that shares a common fixed-size buffer use it as a queue.

The producer’s job is to generate data, put it into the buffer, and start again.

At the same time, the consumer is consuming the data (i.e., removing it from the
buffer), one piece at a time.

Problem: Given the common fixed-size buffer, the task is to make sure that the
producer can’t add data into the buffer when it is full and the consumer can’t remove
data from an empty buffer.

Solution: The producer is to either go to sleep or discard data if the buffer is full.
The next time the consumer removes an item from the buffer, it notifies the producer,
who starts to fill the buffer again. In the same manner, the consumer can go to sleep
if it finds the buffer to be empty. The next time the producer puts data into the buffer,
it wakes up the sleeping consumer.

#include <stdio.h>
#include <stdlib.h>
// Number of full slots as 0
int full = 0;
// Number of empty slots as size
// of buffer
int empty = 10, x = 0;
// Function to produce an item and
// add it to the buffer
void producer()
{
// Increase the number of full
// slots by 1
++full;
// Decrease the number of empty
// slots by 1
--empty;
// Item produced
x++;
printf("\nProducer produces"
"item %d",
x);
}

Producer-Consumer Problem 1
// Function to consume an item and
// remove it from buffer
void consumer()
{
// Decrease the number of full
// slots by 1
--full;
// Increase the number of empty
// slots by 1
++empty;
printf("\nConsumer consumes "
"item %d",
x);
x--;
}
// Driver Code
int main()
{
int n, i;
printf("\n1. Press 1 for Producer"
"\n2. Press 2 for Consumer"
"\n3. Press 3 for Exit");
for (i = 1; i > 0; i++) {

printf("\nEnter your choice:");


scanf("%d", &n);

// Switch Cases
switch (n) {
case 1:

// If mutex is 1 and empty


// is non-zero, then it is
// possible to produce
if (empty != 0) {
producer();
}

// Otherwise, print buffer


// is full
else {
printf("Buffer is full!");
}
break;

case 2:

// If mutex is 1 and full


// is non-zero, then it is
// possible to consume
if (full != 0) {
consumer();
}

// Otherwise, print Buffer


// is empty
else {
printf("Buffer is empty!");

Producer-Consumer Problem 2
}
break;

// Exit Condition
case 3:
exit(0);
break;
}
}
}

Producer-Consumer Problem 3

You might also like