C Program For Producer Consumer Problem
Let us learn how to solve producer consumer problem in C programming language. This C program to solve producer and consumer problem makes use of PThreads and Mutex. However, you can solve this problem by
However, you can solve this problem by Monitors and Semaphores as well. However, the given method below is one of the easiest one. pThreads stands for POSIX Threads.
What is Producer Consumer Problem?
The Producer-Consumer issue is a Classic Synchronisation Problem. It is also known as Bounded Buffer Problem. This problem focuses primarily on two different tasks: Producer and Consumer. Both of them share a fixed size and a common buffer.
- The producer creates data and puts it into the buffer and restarts it.
- The consumer consumes the data. In other words, the consumer removes the data from the buffer that the producer has created.

The producer and consumer problem are to ensure that the producer should not create data into the buffer memory once it gets full and simultaneously, the consumer should not remove data from a buffer memory that is empty. The
The Producer-Consumer problem can be resolved by placing a semaphore in the buffer.
Also Read: C Program For Banker’s Algorithm in OS
If you compile this program in Linux terminal in the normal way, you would get the following errors:
1 2 3 4 5 6 | /tmp/ccRs3Xvw.o: In function `main': test.c:(.text+0x110): undefined reference to `pthread_create' test.c:(.text+0x12b): undefined reference to `pthread_create' test.c:(.text+0x13c): undefined reference to `pthread_join' test.c:(.text+0x14d): undefined reference to `pthread_join' collect2: error: ld returned 1 exit status |
To overcome this error, you will have to link the pthread library file explicitly. The Linux command is as follows:
1 | gcc test.c -pthread |
Method 1: C Program To Implement Producer Consumer Problem using PThread
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include<stdio.h> #include<pthread.h> #include<stdlib.h> #define Buffer_Limit 10 int Buffer_Index_Value = 0; char *Buffer_Queue; pthread_mutex_t mutex_variable = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t Buffer_Queue_Not_Full = PTHREAD_COND_INITIALIZER; pthread_cond_t Buffer_Queue_Not_Empty = PTHREAD_COND_INITIALIZER; void *Consumer() { while(1) { pthread_mutex_lock(&mutex_variable); if(Buffer_Index_Value == -1) { pthread_cond_wait(&Buffer_Queue_Not_Empty, &mutex_variable); } printf("Consumer:%d\t", Buffer_Index_Value--); pthread_mutex_unlock(&mutex_variable); pthread_cond_signal(&Buffer_Queue_Not_Full); } } void *Producer() { while(1) { pthread_mutex_lock(&mutex_variable); if(Buffer_Index_Value == Buffer_Limit) { pthread_cond_wait(&Buffer_Queue_Not_Full, &mutex_variable); } Buffer_Queue[Buffer_Index_Value++] = '@'; printf("Producer:%d\t", Buffer_Index_Value); pthread_mutex_unlock(&mutex_variable); pthread_cond_signal(&Buffer_Queue_Not_Empty); } } int main() { pthread_t producer_thread_id, consumer_thread_id; Buffer_Queue = (char *) malloc(sizeof(char) * Buffer_Limit); pthread_create(&producer_thread_id, NULL, Producer, NULL); pthread_create(&consumer_thread_id, NULL, Consumer, NULL); pthread_join(producer_thread_id, NULL); pthread_join(consumer_thread_id, NULL); return 0; } |
Also Read: C Program For 8 Queens Algorithm Problem
Method 2: C Program For Producer-Consumer Problem using Mutex and Switch Case
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | #include <stdio.h> #include <stdlib.h> int temp = 0, overflow = 0, mutex = 1, underflow = 10; int wait_protocol(int counter) { return (--counter); } int signal_protocol(int counter) { return(++counter); } void producer() { mutex = wait_protocol(mutex); overflow = signal_protocol(overflow); underflow = wait_protocol(underflow); temp++; printf("\nProducer:\t Element %d\n", temp); mutex = signal_protocol(mutex); } void consumer() { mutex = wait_protocol(mutex); overflow = wait_protocol(overflow); underflow = signal_protocol(underflow); printf("\nConsumer:\t Element %d\n", temp); temp--; mutex = signal_protocol(mutex); } int main() { int choice; printf("\n1.Producer\t"); printf("2. Consumer\t"); printf("3. Quit\t"); while(1) { printf("\nEnter your choice:\t"); scanf("%d", &choice); switch(choice) { case 1: if((mutex == 1) && (underflow != 0)) { producer(); } else { printf("Buffer Overflow\n"); } break; case 2: if((mutex == 1) && (overflow != 0)) { consumer(); } else { printf("Buffer Underflow\n"); } break; case 3: exit(0); break; } } return 0; } |
Output

If you have any compilation errors or doubts in this C program to implement producer consumer problem using PThread, let us know about in the comment section below.
Finally, after a lot of errors and finding code from different websites, this program works. The pThreads in producer consumer problem seem to be comparatively easy than monitors and other methods.
Hello,
If i want to define the number of customers and number of producers and also custom production and consumption time, what has to be done, could you please help me with that, thanks
So what i think is if i put the pthread creation statements for producer and consumer inside a loop and create multiple pthread, will that do? Thanks
There’s a macro defined as BUFFER_LIMIT. I think you should check with that!
What are the different ways to solve producer consumer problem algorithm in c programming?
You can solve producer consumer problem using the following methods:
Solvibg producer consumer problem using Mutex is mych more efficient and logical than other methods. It is easy to code as well.
With Semaphores, producer consumer problem looks much better I feel.
gcc filename.c -pthread command is working but ./filename showing “there is no such file or directory” then how i am supposed to see the output?
The first program is not working correctly. It dumps core. Can you please post correct solution.
Did you find out the reason? Having the same problem
Why program goes in infinite loop
How to fix it