Part 5 of 7 · 2 min read
Producer and consumer
In plain words: Level 4 solved producer and consumer with a bounded buffer and semaphores. The same design works for two processes. The ring lives in shared memory, and the two semaphores are marked as shared by passing 1 as the second argument of sem_init (the argument means "shared between processes"):
struct ring { sem_t empty, full; char data[SLOTS][BLK]; };
sem_init(&r->empty, 1, SLOTS); sem_init(&r->full, 1, 0);
/* producer */ sem_wait(&r->empty); fill(r->data[i % SLOTS]); sem_post(&r->full);
/* consumer */ sem_wait(&r->full); use(r->data[i % SLOTS]); sem_post(&r->empty);
The producer waits for an empty slot, fills it and signals; the consumer waits for a full slot, uses it and signals. With 16 slots of 64 KiB, the ring holds 1 MiB.
How does it compare with a pipe? Each version moved 2 GiB in 64 KiB blocks from a producer to a consumer that adds up the words of each block:
| Method | Time | Rate |
|---|---|---|
| Pipe, run 1, 2, 3 | 0.75, 0.81, 0.78 s | 2.66, 2.48, 2.58 GiB/s |
| Shared ring, run 1, 2, 3 | 0.24, 0.22, 0.27 s | 8.35, 8.93, 7.38 GiB/s |
The shared ring was about three times as fast. A pipe copies each block twice, from the writer into the kernel buffer and out to the reader, and each copy needs system calls. The ring needs one write by the producer, one read by the consumer, and a semaphore operation for each block (which is a system call only when a process must sleep). In this test the producer even filled each block with memset, which the pipe's writer did not do, so the comparison favoured the pipe if anything. For small messages, the gap is smaller, and a pipe is simpler and safer. Shared memory pays off for large data, high rates and repeated access to the same data.
Shared ring buffer
A shared ring buffer is a bounded buffer stored in shared memory, used as a circular array of slots, in which a producer process and a consumer process coordinate with two process-shared semaphores that count empty and full slots.
Example: with 16 slots, the producer can run up to 16 blocks ahead of the consumer, and then waits for an empty slot.
Check yourself:
1. What does the second argument of sem_init change?
2. Why is the shared ring faster than a pipe for large blocks?
3. How many slots can the producer fill before it must wait for the consumer?