The Bounded-Buffer Synchronization Problem
When a producer process generates data and a consumer process retrieves it from a fixed-size circular buffer in shared memory, three concurrency invariants must hold at all times:
- No Overflow: The producer must block when all $N$ slots in the buffer are occupied.
- No Underflow: The consumer must block when the buffer holds $0$ unconsumed items.
- Mutual Exclusion: Concurrent updates to shared buffer state must never interleave or corrupt memory.
Polling the buffer in a while loop (busy-waiting) wastes 100% of a CPU core. Instead, we coordinate the processes through the kernel using three POSIX named semaphores:
empty (initialized to $N$): Counts free slots available for writing. The producer decrements empty (sem_wait) before inserting and the consumer increments it (sem_post) after removing.full (initialized to $0$): Counts populated slots ready for reading. The producer increments full after inserting and the consumer decrements it before removing.mutex (initialized to $1$): A binary semaphore guarding the critical section where the buffer and its indices are mutated.
Why Acquisition Order Prevents Deadlock
Before writing a single line of C, notice the strict lock ordering required on both sides:
$$\text{Producer:} \quad \texttt{sem_wait(empty)} ;\longrightarrow; \texttt{sem_wait(mutex)} ;\longrightarrow; \text{insert} ;\longrightarrow; \texttt{sem_post(mutex)} ;\longrightarrow; \texttt{sem_post(full)}$$
$$\text{Consumer:} \quad \texttt{sem_wait(full)} ;\longrightarrow; \texttt{sem_wait(mutex)} ;\longrightarrow; \text{remove} ;\longrightarrow; \texttt{sem_post(mutex)} ;\longrightarrow; \texttt{sem_post(empty)}$$
What happens if a programmer swaps the first two calls in the producer, acquiring mutex before waiting on empty?
- The buffer fills up ($\text{empty} = 0$).
- The producer calls
sem_wait(mutex) (succeeds) and then blocks inside sem_wait(empty) while holding mutex. - The consumer arrives to free a slot, calls
sem_wait(full) (succeeds), and then blocks forever on sem_wait(mutex).
Both processes sleep indefinitely—a textbook deadlock. By always acquiring the capacity semaphore (empty or full) outside the mutex critical section, a process never sleeps for capacity while holding the lock.
Clean, Signal-Safe C Implementation
A common flaw in textbook POSIX examples is calling printf(), sem_unlink(), or exit() directly inside a SIGINT signal handler—none of which (except _exit) are async-signal-safe according to POSIX.1-2008, risking heap deadlocks if the signal interrupts printf.
Instead, the idiomatic pattern is:
- Define the shared memory layout and IPC names in a single header (
ipc_channel.h). - Use a
volatile sig_atomic_t flag set by sigaction (without SA_RESTART) so sem_wait() returns -1 with errno == EINTR immediately on Ctrl+C, allowing the main loop to exit and clean up deterministically.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| #pragma once
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <fcntl.h>
#include <semaphore.h>
#include <signal.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
#define BUFFER_CAPACITY 10
#define SHM_PATH "/pc_ring_shm"
#define SEM_EMPTY_PATH "/pc_sem_empty"
#define SEM_FULL_PATH "/pc_sem_full"
#define SEM_MUTEX_PATH "/pc_sem_mutex"
typedef struct {
size_t write_idx;
size_t read_idx;
int32_t slots[BUFFER_CAPACITY];
} SharedQueue;
static volatile sig_atomic_t g_stop = 0;
static void on_termination_signal(int signo) {
(void)signo;
g_stop = 1;
}
static inline void install_signal_handlers(void) {
struct sigaction sa = {0};
sa.sa_handler = on_termination_signal;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0; // Do not set SA_RESTART: let sem_wait wake on EINTR
sigaction(SIGINT, &sa, nullptr);
sigaction(SIGTERM, &sa, nullptr);
}
|
2. The Producer (producer.c)
The producer creates and sizes the shared memory segment, initializes the three semaphores, and unlinks the kernel-persistent names on graceful exit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
| #include "ipc_channel.h"
int main(void) {
install_signal_handlers();
int shm_fd = shm_open(SHM_PATH, O_CREAT | O_RDWR, 0600);
if (shm_fd == -1 || ftruncate(shm_fd, sizeof(SharedQueue)) == -1) {
perror("Failed to initialize shared memory");
return EXIT_FAILURE;
}
SharedQueue *q = mmap(nullptr, sizeof(SharedQueue),
PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
close(shm_fd);
if (q == MAP_FAILED) {
perror("mmap failed");
return EXIT_FAILURE;
}
q->write_idx = 0;
q->read_idx = 0;
sem_t *empty = sem_open(SEM_EMPTY_PATH, O_CREAT, 0600, BUFFER_CAPACITY);
sem_t *full = sem_open(SEM_FULL_PATH, O_CREAT, 0600, 0);
sem_t *mutex = sem_open(SEM_MUTEX_PATH, O_CREAT, 0600, 1);
if (empty == SEM_FAILED || full == SEM_FAILED || mutex == SEM_FAILED) {
perror("sem_open failed");
return EXIT_FAILURE;
}
printf("[Producer] Online (capacity = %d). Press Ctrl+C to stop.\n", BUFFER_CAPACITY);
for (int32_t item = 1; !g_stop; ++item) {
if (sem_wait(empty) == -1) {
if (errno == EINTR) break;
perror("sem_wait(empty)");
break;
}
if (sem_wait(mutex) == -1) {
sem_post(empty); // Roll back slot reservation on interruption
if (errno == EINTR) break;
perror("sem_wait(mutex)");
break;
}
const size_t slot = q->write_idx;
q->slots[slot] = item;
q->write_idx = (slot + 1) % BUFFER_CAPACITY;
sem_post(mutex);
sem_post(full);
printf("[Producer] Enqueued %d at slot %zu\n", item, slot);
sleep(1);
}
printf("\n[Producer] Shutting down and unlinking IPC objects...\n");
sem_close(empty);
sem_close(full);
sem_close(mutex);
munmap(q, sizeof(SharedQueue));
sem_unlink(SEM_EMPTY_PATH);
sem_unlink(SEM_FULL_PATH);
sem_unlink(SEM_MUTEX_PATH);
shm_unlink(SHM_PATH);
return EXIT_SUCCESS;
}
|
3. The Consumer (consumer.c)
Notice that because read_idx is stored inside SharedQueue and protected by mutex, multiple consumer processes can run this exact binary concurrently without colliding on the same slot:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
| #include "ipc_channel.h"
int main(void) {
install_signal_handlers();
int shm_fd = shm_open(SHM_PATH, O_RDWR, 0600);
if (shm_fd == -1) {
perror("shm_open failed (start producer first)");
return EXIT_FAILURE;
}
SharedQueue *q = mmap(nullptr, sizeof(SharedQueue),
PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
close(shm_fd);
if (q == MAP_FAILED) {
perror("mmap failed");
return EXIT_FAILURE;
}
sem_t *empty = sem_open(SEM_EMPTY_PATH, 0);
sem_t *full = sem_open(SEM_FULL_PATH, 0);
sem_t *mutex = sem_open(SEM_MUTEX_PATH, 0);
if (empty == SEM_FAILED || full == SEM_FAILED || mutex == SEM_FAILED) {
perror("sem_open failed");
return EXIT_FAILURE;
}
printf("[Consumer] Attached. Waiting for items...\n");
while (!g_stop) {
if (sem_wait(full) == -1) {
if (errno == EINTR) break;
perror("sem_wait(full)");
break;
}
if (sem_wait(mutex) == -1) {
sem_post(full);
if (errno == EINTR) break;
perror("sem_wait(mutex)");
break;
}
const size_t slot = q->read_idx;
const int32_t item = q->slots[slot];
q->read_idx = (slot + 1) % BUFFER_CAPACITY;
sem_post(mutex);
sem_post(empty);
printf("[Consumer] Dequeued %d from slot %zu\n", item, slot);
sleep(2);
}
printf("\n[Consumer] Detaching...\n");
sem_close(empty);
sem_close(full);
sem_close(mutex);
munmap(q, sizeof(SharedQueue));
return EXIT_SUCCESS;
}
|
Building and Observing Backpressure
Compile both binaries with C23/C11 standard flags and POSIX threads enabled:
1
2
| cc -std=c2x -Wall -Wextra -O2 producer.c -o producer -pthread
cc -std=c2x -Wall -Wextra -O2 consumer.c -o consumer -pthread
|
(On older glibc versions prior to 2.34, append -lrt for shm_open).
When you run ./producer in one terminal and ./consumer in another, the producer generates items twice as fast as a single consumer processes them (1s vs 2s). Once the 10-slot ring fills up, $\text{val}(\text{empty})$ hits 0 and the kernel automatically puts the producer to sleep inside sem_wait(empty) until the consumer calls sem_post(empty)—enforcing natural backpressure with zero wasted CPU cycles. If you launch a second ./consumer in a third terminal, aggregate consumption matches production and the queue drains smoothly.