C Program For Producer Consumer Problem

3 min read

A C program for the producer consumer problem is a practical way to learn how threads share data safely in an operating system environment. So this classic synchronization problem uses a bounded buffer where one or more producer threads create items and one or more consumer threads remove them. The main challenge is preventing race conditions, ensuring that producers do not write into a full buffer, and ensuring that consumers do not read from an empty buffer. A well-written C program for producer consumer problem demonstrates how semaphores, mutexes, and thread coordination work together to protect shared memory.

Introduction

The producer consumer problem is one of the most important examples in operating systems and concurrent programming. It models real-world systems where data is produced faster than it is consumed, such as network packet processing, task queues, video streaming, and database logging. In a C program for producer consumer problem, the shared buffer is usually fixed in size, which means the system must carefully control access to that buffer.

Without synchronization, multiple threads can access the same memory at the same time. This can cause incorrect results, corrupted data, or program crashes. The goal is to build a program where producers and consumers operate efficiently but safely But it adds up..

Understanding the Producer Consumer Problem

The producer consumer problem involves three main components:

  1. Producers
    Producers generate data items and place them into a shared buffer And it works..

  2. Consumers
    Consumers take data items from the buffer and process them.

  3. Shared Buffer
    The buffer has a limited number of slots. It must be accessed in a controlled way But it adds up..

The basic rules are simple:

  • A producer cannot add an item if the buffer is full.
  • A consumer cannot remove an item if the buffer is empty.
  • Only one thread should modify the buffer at a time.
  • The program should continue running until all producers and consumers finish.

In a C program for producer consumer problem, these rules are usually enforced using synchronization primitives.

Synchronization Tools Used in the C Program

The C program below uses POSIX threads and semaphores. These tools are common on Linux and macOS systems.

Threads

Threads allow the program to run producer and consumer tasks at the same time. The pthread library provides functions to create and manage threads.

Semaphores

A semaphore is a counting synchronization object. In this program, three semaphores are used:

  • empty: tracks how many empty slots are available.
  • full: tracks how many filled slots are available.
  • mutex: protects the buffer index and count from simultaneous modification.

The mutex semaphore acts like a lock. Only one thread can enter the critical section at a time Worth knowing..

Complete C Program

The following C program for producer consumer problem uses a buffer of size 5. On the flip side, it creates two producer threads and two consumer threads. Each producer produces five items, and each consumer consumes five items.

#include 
#include 
#include 
#include 

#define BUFFER_SIZE 5
#define ITEMS_PER_THREAD 5

int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int count = 0;

sem_t empty;
sem_t full;
sem_t mutex;

void *producer(void *arg) {
    int id = (int)arg;

    for (int i = 0; i < ITEMS_PER_THREAD; i++) {
        int item = rand() % 100;

        sem_wait(&empty);
        sem_wait(&mutex);

        buffer[in] = item;
        in = (in + 1) % BUFFER_SIZE;
        count++;

        printf("Producer %d produced item %d, buffer count = %d\n",
               id, item, count
Latest Drops

Hot Right Now

Worth the Next Click

What Others Read After This

Thank you for reading about C Program For Producer Consumer Problem. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home