因为每个进程都有一个隔离的地址空间,它们不能直接共享变量——它们需要进程间通信(inter-process communication, IPC)。操作系统提供了几种机制,它们在速度、方向以及是否能跨机器工作上各不相同。
主要机制
text
Pipe / FIFO → byte stream, one direction; anonymous pipe = parent↔child, named FIFO = any procs
e.g. ls | grep foo (the shell wires stdout→stdin via a pipe)
Shared memory → both map the SAME physical pages → FASTEST (no copy), but you must
synchronize access yourself (semaphore/mutex)
Message queue → kernel-managed queue of discrete messages; decoupled, preserves boundaries
Socket → bidirectional stream/datagram; UNIX-domain (same host) or TCP/UDP (across network)
Signal → tiny async notification (a number), e.g. SIGINT, SIGTERM — control, not data transfer
