Sub topic 12
Producer-Consumer with a Bounded Buffer
Assume you know process and thread basics, context switching, preemption, and C-like pseudocode. This chapter uses semaphores. A semaphore is an integer-based synchronisation object with two atomic operations:
wait(S)decreasesS; if the value is not positive, the caller blocks.signal(S)increasesSand may wake a blocked caller.
The bounded-buffer model
A producer creates items and places them in a shared buffer. A consumer removes items from that buffer. The buffer has a fixed capacity, so it is called a bounded buffer.
Two constraints must always hold:
- A producer must not insert when the buffer is full.
- A consumer must not remove when the buffer is empty.
The buffer itself is shared data, so only one thread should modify its indexes or slots at a time.
| Semaphore | Initial value | Meaning |
|---|---|---|
| `empty` | `N` | Number of unused buffer slots |
| `full` | `0` | Number of occupied buffer slots |
| `mutex` | `1` | Permission for one thread to access the buffer |
empty and full are counting semaphores. mutex is a binary semaphore used as a mutual-exclusion lock.
Correct operation order
A producer first waits for an empty slot. It then acquires the mutex, inserts the item, releases the mutex, and announces that one more item is available.
A consumer first waits for an available item. It then acquires the mutex, removes the item, releases the mutex, and announces that one more slot is free.
flowchart TD
P[Producer] -->|wait empty| M1[wait mutex]
M1 --> I[Insert item]
I --> S1[signal mutex]
S1 --> F[signal full]
F --> P
C[Consumer] -->|wait full| M2[wait mutex]
M2 --> R[Remove item]
R --> S2[signal mutex]
S2 --> E[signal empty]
E --> Ccaption: Semaphore ordering in the bounded-buffer solution alt: Producer waits for an empty slot before locking the buffer; consumer waits for a full slot before locking it.
The following complete POSIX C program uses the same pattern. Compile it with cc buffer.c -pthread.
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#define N 4
#define ITEMS 8
int buffer[N], in = 0, out = 0;
sem_t empty, full, mutex;
void *producer(void *arg) {
for (int item = 1; item <= ITEMS; item++) {
sem_wait(&empty);
sem_wait(&mutex);
buffer[in] = item;
in = (in + 1) % N;
sem_post(&mutex);
sem_post(&full);
}
return NULL;
}
void *consumer(void *arg) {
for (int i = 0; i < ITEMS; i++) {
sem_wait(&full);
sem_wait(&mutex);
int item = buffer[out];
out = (out + 1) % N;
sem_post(&mutex);
sem_post(&empty);
printf("consumed %d\n", item);
}
return NULL;
}
int main(void) {
pthread_t p, c;
sem_init(&empty, 0, N);
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;
}Tracing and common errors
Initially, (empty, full, mutex) = (N, 0, 1). After a successful production, the counts become (N - 1, 1, 1). After a successful consumption, they return to (N, 0, 1).
The most common error is acquiring mutex before waiting on empty or full. If the buffer is full, a producer may hold mutex while waiting for space. The consumer then cannot acquire mutex to remove an item. Both threads wait forever. This is deadlock caused by incorrect semaphore ordering.
Exercise
With a buffer capacity of 3 and initial values empty = 3, full = 0, how many successful productions can occur before the next producer blocks, assuming no consumer runs?
Show answer
Three. Each production decreases empty by one, so after three productions the values are empty = 0 and full = 3.