Here you will learn about producer consumer problem in C.
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.
Also Read: Banker’s Algorithm in C
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.
Producer Consumer Problem in C
Below is the program to implement this problem.
#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:1
Producer produces the item 3
Enter your choice:1
Buffer is full!!
Enter your choice:3
tq sir for posting this
Hey why shouldn’t you publish readers writers using semaphores?
if we have to determine number of producer and consumer for example (3 producer & 3 consumer )
how we can do it ??
and if we have to use mutixes rather than semaphore how we can do it ?
Sir what is the prototype of exit? I have 1 error tat “‘exit should have a prototype”
Use stdlib.h, I forgot to add it.
Use stdlib.h headerfile
how to increase buffer size?
how to incress buffer size?
increase the value of empty !!!
you are wrong.If we want increase bufferer size decrease value of empty.
dont know
i don’t know
regards:vijaychaudary
why have you used mutex when it doesnt make a difference to the execution ????
to avoid multiple transactions at a time
Its toooo easy to understand and easy to compile…………
sir i want shell programming in c for os lab
ITS TOO GUD TO UNDERSTAND
@NeerajMishra actully the code and the video itself dont match. In the video the dude is explaining how to implement thye code using Semaphores. But the code that you provided us is the one which is implemented using Mutex.
1. You should provide us with the explanation of your code (using mutex)
or 2. You should provide us the code based on the video (using semaphores)
thanks anyways!
where is the video?? lol
pm me
i have more sunaplees
what is the importance of mutex??
i have more sunaplees
Bhai while(1)
Kyu laga hai
If Bounded-buffer size is 30 elements, how the code would be changed?
change the variable size as empty=30;
The functionality of variables full and empty be achieved using one variable, why are we using two here?
I want this program using thread
Why you declare mutex variable to increment at the beginning of producer and consumer and decrement at the end of producer and consumer? If you remove mutex variable it works fine.
Your program is fundamentally flawed!!
The whole idea is to carry out transactions (on the buffer) atomically, which is only possible using process synchronisation. Threads have not been used at all! The CPU scheduler is responsible for choosing which process to execute next.
nice program
Sir I need display option…. because to know how many items are left after consuming an item
What is use of printf(“n1.producer\n2.producer\n3.Exit”); statement?
Thank you for your explanation.
Is it possible to separate consumer and producer in two different classes, let them access to a shared array/buffer and compile as followed?
$ gcc producer.c -pthread -lrt -o producer
$ gcc consumer.c -pthread -lrt -o consumer
$ ./producer & ./consumer &