Producer Consumer Problem C Program

By | July 27, 2016

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.
Implement Producer Consumer Problem in C Programming LanguageImage Source: Android SRC

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:

To overcome this error, you will have to link the pthread library file explicitly. The Linux command is as follows:


Method 1: C Program To Implement Producer Consumer Problem using PThread

Also Read: C Program For 8 Queens Algorithm Problem

Method 2: C Program For Producer-Consumer Problem using Mutex and Switch Case


C Program For Producer Consumer Problem using PThread

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.

13 thoughts on “Producer Consumer Problem C Program

  1. Pankaj Dhende

    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.

  2. Hari


    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

    1. Hari

      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

  3. Ritesh Yadav

    What are the different ways to solve producer consumer problem algorithm in c programming?

  4. Ujjwal Singh

    Solvibg producer consumer problem using Mutex is mych more efficient and logical than other methods. It is easy to code as well.

  5. Puja Mehta

    With Semaphores, producer consumer problem looks much better I feel.

  6. uroosamanzoor

    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?

  7. Sacheen Birhade

    The first program is not working correctly. It dumps core. Can you please post correct solution.


Let's Discuss