Producer Consumer Problem C Program

C Program For Producer Consumer Problem

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 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 is 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 Producer Consumer Issue can be solved by placing a Semaphore on 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.

Tushar Soni

I am Tushar Soni, Co - Founder of CodingAlpha. I am a computer science student from India and passionate about Web Development and Programming. Connect with me on Facebook | LinkedIn | Google Plus

7 thoughts on “Producer Consumer Problem C Program

  • July 29, 2016 at 12:41 am

    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.

  • July 30, 2016 at 8:13 am


    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

    • July 30, 2016 at 8:16 am

      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

      • July 31, 2016 at 7:51 pm

        There’s a macro defined as BUFFER_LIMIT. I think you should check with that!

  • September 20, 2016 at 1:02 am

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

    • September 20, 2016 at 1:05 am

      You can solve producer consumer problem using the following methods:

    • pThreads
    • Semaphores
    • Reply
  • November 22, 2016 at 7:32 am

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


Join The Discussion