IPC models and the lifetime of shared state

See why processes cannot share variables, how data crosses by a kernel copy or a shared region, and how long the channel and the data live. IPC and device I/O · Operating systems.

A cashier process has finished a sale and holds the receipt number RCT204 in a variable. A separate printer process has to print it, and the cashier cannot tell the printer where the variable is. How does RCT204 get across, and what happens to the channel and to the data if the cashier exits a moment later?

Private memory is the problem

Every process has its own address space. The addresses a process uses are translated to memory that belongs to that process, so address 4096 in the cashier and address 4096 in the printer ordinarily name different bytes unless the processes deliberately share a mapping. The printer has no address that names the cashier's variable: an address it uses reaches its own memory, or faults if nothing is mapped there, and nothing crosses. (A real fault normally ends the process with SIGSEGV. The animation lets the printer carry on so that it can still receive.)

This lesson compares two ways they can cooperate: a kernel-buffered channel and a region mapped into both address spaces. Other facilities exist; for example, Linux permits a suitably authorized process to transfer bytes directly from another process using process_vm_readv.

Copy through the kernel

In this kernel-buffered message-passing example, the cashier makes a system call such as send or write. The kernel copies the 6 bytes of RCT204 from the cashier's memory into a buffer that the kernel owns, which is the first copy. When the printer calls receive or read, the kernel copies them from that buffer into the printer's memory, which is the second copy. Delivering 6 bytes therefore takes 2 copies, 12 bytes in all.

In return the kernel does the bookkeeping: a blocking receive on an empty channel can wait for data, and neither process directly loads the other's private memory. Empty-channel behavior also depends on the channel and its settings: a nonblocking operation can return an error, while a pipe with no writers returns end of file.

One region both processes map

With shared memory, each process asks the kernel to map the same region into its address space, which makes the same bytes appear at an address of its own. Setting that up costs one map call in each process. After it, the cashier stores RCT204 into the region and the printer loads it from the very same bytes, so the kernel copies nothing for the transfer, which can reduce copying for large or frequent exchanges, though setup and synchronization also have costs. The cashier's one store into the region is not a kernel copy.

The price is that the kernel no longer sees the transfer. Nothing tells the printer that the data is ready, and nothing stops two processes writing at once.

Copy through the kernel or share a region

Read the steps as text

  1. Cashier and Printer each have private memory, and the kernel sits between them. Shared is a block of memory that both can map. Both runs follow the same schedule.
  2. Through the kernel: Printer tries to read Cashier's msg directly, but nothing in its own address space maps it, so it faults and nothing crosses. Shared region: Printer maps Shared into its address space.
  3. Through the kernel: Cashier copies RCT204 from msg into the kernel buffer. Shared region: Cashier maps Shared, the same block Printer has mapped.
  4. Through the kernel: Cashier does other work. Shared region: Cashier writes RCT204 into Shared with no copy.
  5. Through the kernel: Printer copies RCT204 from the kernel buffer into inbox (the second copy). Shared region: Printer reads RCT204 from Shared with no copy.

The first run needed a kernel copy in and a kernel copy out, and the printer's direct read faulted. The second run had the kernel copy nothing after the setup, and it is the processes that must now make sure the data is complete when it is read.

Shared memory moves the safety job to the processes

Suppose a sales counter lives in the shared region and holds 7. Process A and process B each record one sale, which takes three steps in a private register r: load the counter into r, add 1 to r, store r back. If both load 7 before either stores, both compute 8 and both store 8. The counter ends at 8 although two sales happened, and nothing reports the lost update. This is an illustrative load/add/store interleaving; in languages such as C, unsynchronized conflicting accesses can instead have undefined behavior. A real program needs synchronization or suitable atomic operations.

Two processes and a lost update

Read the steps as text

  1. Process A and process B are both about to run. The sales counter in shared memory is 7.
  2. Process A loads 7 from the sales counter in shared memory into r.
  3. Process B loads 7 from the sales counter in shared memory into r.
  4. Process A adds 1 to r, giving 8.
  5. Process B adds 1 to r, giving 8.
  6. Process A stores 8 into the sales counter in shared memory.
  7. Process B stores 8 into the sales counter in shared memory. That overwrites a write another process made since process B loaded the value. One update is lost: the sales counter should hold 9.

The cure is also the same: a lock that both processes can reach. Examples are a POSIX mutex marked process-shared and placed in the region, or a semaphore that the kernel keeps. Process A locks it first. Process B finds it held and sleeps. A loads 7, stores 8 and unlocks, and that wakes B, which locks it, loads 8 and stores 9. Both sales count. A mutex that each process creates for itself in its own private memory would be two different mutexes, so neither process would ever wait for the other.

A mutex both processes can reach

Read the steps as text

  1. Process A and process B are both about to run. The sales counter in shared memory is 7.
  2. Process A locks the mutex and becomes its owner.
  3. The mutex is held by process A, so process B sleeps on it.
  4. Process A loads 7 from the sales counter in shared memory into r. Process A adds 1 to r, giving 8.
  5. Process A stores 8 into the sales counter in shared memory. Process A unlocks the mutex. Process B wakes and will try to lock it again.
  6. Process B locks the mutex and becomes its owner. Process B loads 8 from the sales counter in shared memory into r.
  7. Process B adds 1 to r, giving 9. Process B stores 9 into the sales counter in shared memory.
  8. Process B unlocks the mutex.

How long the channel and the data live

A channel has a lifetime of its own, governed by the kind of object and its remaining references rather than by the creator alone.

A pipe made with pipe() has no name. It exists while at least one of its ends is open in any process, and a child created by fork inherits copies of the ends. When the last end closes, the pipe and any unread bytes are gone.

On Linux, a named POSIX message queue or shared-memory object can outlive its creator and, if not unlinked, persists until shutdown. Unlinking removes the name: a later open cannot reach that old object by name, though creating a new object with the same name is possible. A queue already open remains usable until its last descriptor closes. For shared memory, an existing mapping remains usable even after its descriptor is closed or its name unlinked; the backing object is released after its remaining references, including mappings, are gone.

POSIX queues deliver higher-priority records first. Records of equal priority are delivered in send order.

The last animation uses a queue named jobs that holds 2 records. The producer sends A and B, and its third send, C, finds the queue full, so it sleeps until the consumer receives A, which frees a slot and wakes it. Then the producer exits. In the first run it never unlinks the name. The consumer exits too, and B and C are still in the queue after both processes have gone. In the second run the producer unlinks the name before it exits, but the consumer still has the queue open, so the queue lives on until the consumer exits. Then the kernel destroys it and drops the 2 unread records.

The queue outlives the producer

Read the steps as text

  1. Producer and Consumer share a queue named jobs that holds up to 2 records. Both runs follow the same schedule.
  2. Both runs: Producer sends A (queue 1 of 2).
  3. Both runs: Producer sends B (queue 2 of 2).
  4. Both runs: Producer tries to send C, finds the queue full and blocks.
  5. Both runs: Consumer receives A (queue 1 of 2); Producer wakes because the queue has room.
  6. Both runs: Producer sends C (queue 2 of 2).
  7. Name kept: Producer does other work. Name unlinked: Producer unlinks the queue name.
  8. Both runs: Producer exits and the queue stays with 2 records.
  9. Name kept: Consumer exits and the queue stays with 2 records. Name unlinked: Consumer exits, and the queue is destroyed, dropping 2 unread records.

This has a practical consequence. If a producer crashes while its named queue is still linked, unread records remain; a later run must decide whether to process or discard those stale records.

Recap

  • An ordinary load addresses the caller’s memory. In the illustrated buffered channel, 6 bytes cost 12 bytes of kernel copying; a shared region needs no kernel copy for each transfer.
  • A shared region leaves ordering to the processes: two that both load 7 each store 8 and lose a sale until a lock they both reach makes the second wait and the count reach 9.
  • The kernel object sets the lifetime: a pipe ends with its last open end, while a named queue and its unread records outlive the producer until the name is unlinked and the last user closes it.
The channel can outlive the data's meaning

Keeping a channel alive does not keep its contents correct. A shared region that is still mapped holds whatever bytes were last written, including a frame that a producer was writing when it was killed, and nothing in the mapping says the frame is complete. Processes that share a region usually add their own marker, such as an atomic ready flag or sequence number published with appropriate synchronization after the data. A POSIX message queue preserves each successfully sent record as a whole; the sender still needs to decide whether its record is complete before sending it.

The next lesson follows pipes and queues in detail: how a pipe's ends are inherited, how end of file arrives, and when a read waits.