Skip to content
doubtly

2 answers

Best answer

Answer: Use a bounded buffer with three synchronisation objects: semaphore empty (initial value = buffer size), semaphore full (initial value 0) and a mutex. The producer does wait(empty), lock, insert, unlock, signal(full); the consumer does wait(full), lock, remove, unlock, signal(empty).

Why it works

  • empty counts free slots, so the producer blocks when the buffer is full.
  • full counts filled slots, so the consumer blocks when the buffer is empty.
  • The mutex ensures only one thread changes buffer, in and out at a time (no race condition).

Program (C with POSIX threads and semaphores; compile on Linux with gcc prod_cons.c -o prod_cons -pthread)

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

#define BUFFER_SIZE 5
#define ITEMS 8

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

sem_t empty;              /* counts empty slots, starts at BUFFER_SIZE */
sem_t full;               /* counts filled slots, starts at 0 */
pthread_mutex_t mutex;    /* only one thread touches the buffer at a time */

void *producer(void *arg)
{
    int item;
    for (item = 1; item <= ITEMS; item++) {
        sem_wait(&empty);                 /* wait(empty) */
        pthread_mutex_lock(&mutex);       /* wait(mutex) */
        buffer[in] = item;
        printf("Producer produced item %d at slot %d\n", item, in);
        in = (in + 1) % BUFFER_SIZE;
        pthread_mutex_unlock(&mutex);     /* signal(mutex) */
        sem_post(&full);                  /* signal(full) */
        usleep(100000);
    }
    return NULL;
}

void *consumer(void *arg)
{
    int i, item;
    for (i = 1; i <= ITEMS; i++) {
        sem_wait(&full);                  /* wait(full) */
        pthread_mutex_lock(&mutex);
        item = buffer[out];
        printf("Consumer consumed item %d from slot %d\n", item, out);
        out = (out + 1) % BUFFER_SIZE;
        pthread_mutex_unlock(&mutex);
        sem_post(&empty);                 /* signal(empty) */
        usleep(250000);                   /* slower consumer, so buffer fills up */
    }
    return NULL;
}

int main()
{
    pthread_t prod, cons;

    sem_init(&empty, 0, BUFFER_SIZE);
    sem_init(&full, 0, 0);
    pthread_mutex_init(&mutex, NULL);

    pthread_create(&prod, NULL, producer, NULL);
    pthread_create(&cons, NULL, consumer, NULL);
    pthread_join(prod, NULL);
    pthread_join(cons, NULL);

    sem_destroy(&empty);
    sem_destroy(&full);
    pthread_mutex_destroy(&mutex);
    printf("All %d items produced and consumed.\n", ITEMS);
    return 0;
}

Sample run (gcc on Linux)

Producer produced item 1 at slot 0
Consumer consumed item 1 from slot 0
Producer produced item 2 at slot 1
Producer produced item 3 at slot 2
Consumer consumed item 2 from slot 1
Producer produced item 4 at slot 3
Producer produced item 5 at slot 4
Consumer consumed item 3 from slot 2
Producer produced item 6 at slot 0
Producer produced item 7 at slot 1
Producer produced item 8 at slot 2
Consumer consumed item 4 from slot 3
Consumer consumed item 5 from slot 4
Consumer consumed item 6 from slot 0
Consumer consumed item 7 from slot 1
Consumer consumed item 8 from slot 2
All 8 items produced and consumed.

The exact interleaving can change from run to run (it depends on thread scheduling), but every item is consumed exactly once and in order, and the producer never gets more than 5 items ahead. Note: macOS does not support unnamed semaphores (sem_init), so run it on Linux.

Doubtly · 24 Apr 2023 Report
Was this answer helpful?

Answer: Use three semaphores: empty (free slots, starts at buffer size), full (filled slots, starts at 0) and mutex (starts at 1). The producer and consumer run as two threads.

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>

#define SIZE 5        /* buffer size */
#define ITEMS 10      /* items to produce */

int buffer[SIZE], in = 0, out = 0;
sem_t empty;          /* counts free slots,   starts at SIZE */
sem_t full;           /* counts filled slots, starts at 0    */
sem_t mutex;          /* binary semaphore for the buffer     */

void *producer(void *arg) {
    for (int item = 1; item <= ITEMS; item++) {
        sem_wait(&empty);             /* wait(empty) */
        sem_wait(&mutex);             /* wait(mutex) */
        buffer[in] = item;
        printf("Producer produced %d\n", item);
        in = (in + 1) % SIZE;
        sem_post(&mutex);             /* signal(mutex) */
        sem_post(&full);              /* signal(full)  */
    }
    return NULL;
}

void *consumer(void *arg) {
    for (int k = 1; k <= ITEMS; k++) {
        sem_wait(&full);              /* wait(full)  */
        sem_wait(&mutex);
        int item = buffer[out];
        printf("Consumer consumed %d\n", item);
        out = (out + 1) % SIZE;
        sem_post(&mutex);
        sem_post(&empty);             /* signal(empty) */
    }
    return NULL;
}

int main(void) {
    pthread_t p, c;
    sem_init(&empty, 0, SIZE);
    sem_init(&full, 0, 0);
    sem_init(&mutex, 0, 1);
    pthread_create(&p, NULL, producer, NULL);
    pthread_create(&c, NULL, consumer, NULL);
    pthread_join(p, NULL);
    pthread_join(c, NULL);
    sem_destroy(&empty);
    sem_destroy(&full);
    sem_destroy(&mutex);
    return 0;
}

Compile and run on Linux: gcc pc.c -o pc -pthread then ./pc

How the semaphores solve the problem

  • Producer: wait(empty) blocks it when the buffer is full. wait(mutex) gives it exclusive access while it puts an item in. Then signal(mutex) releases the buffer and signal(full) tells the consumer that one more item is available.
  • Consumer: wait(full) blocks it when the buffer is empty. It takes an item inside the mutex section, then signal(empty) frees a slot for the producer.
  • The order matters: a thread must wait on empty or full before taking mutex. Otherwise it could hold the mutex while blocked, and the system would deadlock.

The output shows produced and consumed items interleaved. Items are always consumed in the order they were produced, the producer never overwrites a full buffer, and the consumer never reads an empty one.

The earlier answer on this page simulates the buffer with a menu but uses no semaphores (no wait/signal operations), which is what the question asks for.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.