CSE Roadmap

Core tier · about 5 hours · 7 parts

Part 6 of 7 · 3 min read

Choosing a method

When to choose shared memory

In plain words: Shared memory removes the kernel from the data path, and with it the kernel's protection. Four duties fall on the programmer:

  • Synchronise. The kernel does not order the accesses. The program needs locks, atomic operations or semaphores, as in sections 2 and 4.
  • Use offsets, not pointers. Each process may map the region at a different address (section 3).
  • Clean up. A named object stays until shm_unlink. A crash leaves it behind, and leftover objects take memory. A start-up check or a cleanup tool is wise.
  • Do not trust the data. If another process can write the region, its content can be wrong or hostile. A reader must check lengths and values, just as for input from a network. Set the permission mode (0600 above) so that only the intended user can open the object.

A decision list:

Need Choice
A stream of bytes, simple, moderate rate pipe or socket (Level 1)
Messages between machines socket
Large data, high rate, or the same data read often shared memory
Shared data that must survive a restart MAP_SHARED file mapping (page 4)

Linux has an older family of shared memory calls from System V (shmget, shmat), which does the same job with numeric keys. New programs use the POSIX calls shown here, and the older ones are met in existing code.

Offset-based reference

An offset-based reference is a link stored inside a shared region as a distance from the start of the region instead of as a pointer, so that it stays correct in every process whatever address the region is mapped at.

Example: a list node in shared memory stores next = 128 (the node's position), and each process adds that to its own base address.

Interview angle

"How can two processes share data, and what are the trade-offs?" A strong answer lists pipes, sockets, files and shared memory, says that shared memory avoids copies by mapping the same frames, and names the duties: synchronisation, offsets, clean-up and trust.

Check yourself: 1. Why must a reader of shared memory check what it reads? 2. What happens to a POSIX shared memory object if its creator crashes? 3. Which method would you pick for a small stream between a parent and a child?

Words you will meet

Word Meaning
Shared memory Frames mapped into several address spaces
MAP_SHARED The mapping kind whose writes others see
shm_open Create or open a named shared memory object
ftruncate Set the size of the object
shm_unlink Remove the name of the object
/dev/shm The memory-backed directory that holds the objects
PTHREAD_PROCESS_SHARED The setting that lets processes use one mutex
Lost update One of two overlapping increments is lost
Ring buffer A circular array of slots
Offset A distance from the start of a region

Quick notes

  • Shared memory is a region of memory that the page tables of two or more processes map to the same physical frames, so that a store by one process is seen by the others through ordinary loads.
  • Process-shared synchronisation means a lock, semaphore or condition variable that is stored in shared memory and marked so that processes, and not only threads of one process, can use it to coordinate their access to the shared data.
  • A POSIX shared memory object is a named region of memory created with shm_open, sized with ftruncate, mapped with mmap(MAP_SHARED) by any process that knows the name, and removed with shm_unlink.
  • 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.
  • An offset-based reference is a link stored inside a shared region as a distance from the start of the region instead of as a pointer, so that it stays correct in every process whatever address the region is mapped at.