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.