Master the fundamental concepts of inter-process communication through this focused micro-challenge.
Shared mappings remove copy cost but introduce data races. Producers and consumers must synchronize access to indices and payloads with semaphores, mutexes, or atomics in the shared segment.
Shared control block:
For example, a ring in shared memory with two semaphores (items, spaces) mirrors kernel pipe backpressure without syscalls per byte.
PostgreSQL and Oracle's shared-memory buffer pools pair exactly this SysV semaphore pattern with shared segments because raw shared memory has zero built-in coordination, meaning concurrent writers can interleave byte-by-byte and corrupt a struct mid-update. The unmatched wait/signal bug this task guards against is a real, common source of production hangs: a process that crashes while holding a semaphore leaves every future waiter blocked forever unless the semaphore has SEM_UNDO semantics.
Before you call the implementation done, walk failure modes on purpose. Test empty structures, single-element edge cases, maximum concurrency, and errno paths that must not crash the program. OS code usually fails in production when happy-path tests pass but invariants break under contention or memory pressure.
Keep structures small and name fields after kernel counterparts when possible. That lets you read man pages and kernel source side by side while you work. Print observable events during development; remove noisy logs once tests pass reliably.
You will pair your shared memory segment with semaphores for a bounded buffer. This exercise requires proving no torn reads occur under concurrent load.
Implement synchronized shared memory with semaphores.
Requirements:
Test:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github