KERNEL1st2nd3rdin fd[1]fd[0] outKERNEL ONLYKERNEL ONLYmsghi\nshared[0]0bufx1write(fd[1], "hi\n", 3);2shared[0] = 42;1read(fd[0], buf, 3);2wait(NULL);3x = shared[0];hi42zZ
Back: the kernel's side (the kernel, the pipe buffer)Front: two address spaces (process A, process B) and the shared page between themTokens: bytes copied through the kernel; 42 stored and loaded directlyThe kernel's side at the back: the kernel and a pipe buffer in kernel memory. In front, one tray per process: A and its memory on the left, B and its memory on the right, and between them a shared page wired into both. The pipe: B's read sleeps on the empty pipe, A's write copies hi into the kernel and wakes B, and the kernel copies it out to B. Shared memory: A stores 42 straight into the page, B waits for A to finish, then loads 42, with no kernel and no copy. Which process runs first after fork is up to the scheduler. The figure shows B reaching read first, so it blocks; if A writes first, B finds the bytes waiting and read returns at once. The program prints the same either way.

Separate memories

  1. Process A and process B are two running programs. Each has its own memory, its address space, and the kernel keeps them apart: A cannot read or change B's memory. To work together they need inter-process communication, or IPC.

A pipe

Shared memory

What happens

;; step 1/11 · walls
process A   own memory   (its address space)
process B   own memory   (its address space)

fork(2): "The child process and the parent process
          run in separate memory spaces."

A writes its memory  ->  B sees nothing
IPC = inter-process communication: a channel
      the kernel sets up between them

Which process runs first after fork is up to the scheduler. The figure shows B reaching read first, so it blocks; if A writes first, B finds the bytes waiting and read returns at once. The program prints the same either way. The cards show only the lines each process runs after fork. The whole program is real C99 with POSIX calls: it creates the pipe and the shared page, forks, and prints the bytes read and x.