The Big Picture: Talking Through the Walls

Isolation (Section 1.4) puts each process in its own sealed room. That is good for safety, but real work needs cooperation. Your browser talks to a download helper. Your editor talks to a compiler. Services talk to apps. So Windows provides controlled doors between rooms. These are called IPC: Inter-Process Communication.

 +-----------+                          +-----------+
 | Process A |  <-- pipe / socket  -->  | Process B |
 |  (room)   |  <-- shared memory  -->  |  (room)   |
 |           |  <-- messages / RPC -->  |           |
 +-----------+                          +-----------+
       Sealed rooms, with a few agreed doors.

The doors differ in speed, ease and reach:

MethodBest forSame PC only?
PipeA stream of bytes between two programsAnonymous: yes. Named: can cross the network
SocketTalking over a network (or the same PC)No, works across PCs
Shared memoryVery fast sharing of big dataYes
MessagesTelling windows or threads "something happened"Yes
RPCCalling a function in another process as if it were localNo, can cross the network
OthersClipboard, files, events, mailslotsMostly yes

9.1 Pipes

What it is: A pipe is a one-way (or two-way) tube. One process puts bytes in one end; another takes them out of the other end, in the same order. Like a water pipe, or a conveyor belt.

  Process A  --->  [ b y t e s   i n   o r d e r ]  --->  Process B
   (writer)              the pipe                          (reader)

Windows has two kinds:

KindFeatures
Anonymous pipeNo name. Created by a parent and handed to a child through handle inheritance (Section 5.4). Works only between related processes on the same PC. One direction.
Named pipeHas a name such as \\.\pipe\mypipe. Any process with permission can connect by name. Can be two-way, can serve many clients, and can work across the network.

You use pipes every day in the command line:

dir | find "docs"

The | creates a pipe: dir's output becomes find's input. The shell starts both processes and connects them. In the same way, a redirect such as dir > list.txt uses a file instead of a pipe.

Pipes are easy and reliable, but data is copied: from the writer into a kernel buffer, then to the reader. If the buffer is full, the writer waits (it becomes blocked, Section 7.4) until the reader catches up. That is a nice, built-in way to keep both sides in step.

9.2 Sockets

What it is: A socket is an endpoint for network communication. Windows offers them through Winsock (Windows Sockets). Two programs each create a socket, connect, and exchange bytes. The two programs can be on the same PC or across the world.

Key terms:

TermPlain meaning
IP addressWhich computer (for example 192.168.1.10; 127.0.0.1 means "this PC")
PortWhich program on that computer (a number from 0 to 65535). Like an apartment number in a building
TCPReliable, ordered stream. Like a phone call. Web pages, email and file transfers use it
UDPFast, no guarantee. Like postcards. Video calls and games often use it
  Browser (any port) ---- TCP ----> web server (port 443)
        client                          server "listens"

A server listens on a port; a client connects to it. Once connected, both sides can send and receive.

Using sockets between two programs on the same PC is called loopback (talking to 127.0.0.1). It is easy and works the same way as network use, but is slower than a pipe or shared memory because the data goes through the network software.

See which programs hold open connections: netstat -ano shows a PID for each, and Get-NetTCPConnection in PowerShell shows OwningProcess. Match the PID in Task Manager (Section 5.1).

9.3 Shared Memory

What it is: Two processes ask Windows to make one piece of real RAM appear in both of their address spaces. Now, when A writes there, B sees it immediately. No copying. This is the fastest IPC method.

On Windows it is built with a file mapping object (sometimes called a section): the function CreateFileMapping makes it, and MapViewOfFile places a view of it in a process's address space. The mapping can be given a name so another process can open it.

 Process A's address space         Real RAM        Process B's address space
 +----------------------+      +-------------+      +----------------------+
 | 0x2000_0000 ---------+----->| SHARED PAGE |<-----+--------- 0x5000_0000 |
 +----------------------+      +-------------+      +----------------------+
   Different addresses, same real memory.

Notice the two processes may see the shared memory at different virtual addresses. That is fine, because the translation maps both to the same real RAM.

The catch: Windows does nothing to coordinate the two sides. If both write at the same time, data gets mixed up (a race condition). So they must use synchronization objects (mutexes, events, semaphores, covered in Chapter 10) to take turns.

DLL code (Section 8) and program code are shared in just this way: the same file mapped into many processes.

9.4 Message Passing

What it is: Instead of sharing memory, a sender packages a small note and sends it; the receiver reads it. Each message is a separate item, not a stream of bytes. Windows has several message systems.

Window messages. Every program with a window has a message queue. Windows (the system) sends messages to a window: "mouse clicked here", "key pressed", "please repaint", "close". A program's main loop picks messages from the queue and handles them. One program can also send a message to another program's window using SendMessage or PostMessage (with limits for security). A "Not responding" window is one that has stopped picking up its messages.

 +-------------+  PostMessage(hwnd, WM_CLOSE)  +-------------+
 | Program A   | ----------------------------> | Program B's |
 |             |                               | message     |
 +-------------+                               | queue       |
                                               +-------------+

Clipboard. Copy and paste is also message passing. When you press Ctrl+C, the program places data on the clipboard (a shared holding area managed by Windows). When you press Ctrl+V, another program asks Windows for it. Two processes exchange data without knowing each other.

Other message tools. WM_COPYDATA is a standard message for sending a small block of data to another window. Mailslots are simple one-way message boxes (old but still present). Message queues also exist at the thread level (PostThreadMessage).

How message passing compares with shared memory: messages are easier and safer (the system does the queuing), but slower for large data. Shared memory is faster but needs careful turn-taking.

9.5 RPC (Remote Procedure Call)

What it is: RPC lets one process call a function that lives in another process (or on another PC) as if it were an ordinary local call. The programmer writes result = DoWork(5). Behind the scenes, Windows packs up the request, sends it, waits, and unpacks the reply.

 CLIENT process                              SERVER process
 +------------------+                       +------------------+
 | DoWork(5)        |                       | DoWork(x) { ... }|
 |   |  (stub:      |  --- request: 5 --->  |   ^  (stub:      |
 |   |   packs 5)   |                       |   |   unpacks 5) |
 |   v              |  <-- reply: 25 -----  |   |              |
 | result = 25      |                       +------------------+
 +------------------+

Pieces:

PieceJob
Interface definitionA description (written in IDL, Interface Definition Language) of which functions exist and their inputs/outputs
StubsSmall generated code that packs (marshals) the inputs and unpacks the results, hiding all the plumbing
TransportThe road underneath: named pipes, TCP/IP, or a fast local path for same-PC calls (ALPC, Advanced Local Procedure Call, inside Windows)

RPC is everywhere inside Windows: many Windows services use it to talk to each other and to apps. Technologies built on top of it include COM (Component Object Model), which many Windows features and Office use for sharing functionality across processes.

Pros: easy for programmers, structured, secure (it can check who is calling). Cons: slower than shared memory, and a remote call can fail in ways a local call cannot (the network breaks).

Summary

MethodPlain meaningStrengthWeakness
Anonymous pipeTube between parent and childSimpleOne direction, related processes only
Named pipeTube with a nameTwo-way, many clientsData is copied
SocketNetwork-style connectionWorks across PCsMore overhead
Shared memoryOne RAM page seen by two processesFastestYou must add your own turn-taking
Window message / clipboardNotes posted to a window; copy and pasteEasy, built inSmall data, windows only
RPCCall a function in another processLooks like a normal callSlower, can fail remotely