Interprocess communication - a tutorial#
Two programs that need to talk have a handful of channels to choose from: a pipe, a named FIFO on disk, a socket over the network, a socket pair in memory, or a slab of System V shared memory. This tutorial is a cookbook. Each page hands you a complete, runnable program - a producer and a consumer, or a client and a server - that you can copy, run, and adapt.
The mechanics of each channel - what socket returns, how fork splits a process, what %SIG does - are already documented in the per-built-in reference and the signals chapter. This tutorial does not restate them. It assembles them into working IPC programs and links each call back to its reference page.
PetaPerl runs only on Linux, so everything here is POSIX. There is no Win32 IPC, no named-pipe-on-Windows alternative, no Win32::Process. The recipes use the system calls a Linux kernel actually provides.
Choosing a channel#
Pick the lightest channel that carries what you need:
A child running a command - you want the output of
sortor the input ofgzip. Use a pipe open, orIPC::Open2/IPC::Open3when you need both directions. See Subprocesses and co-processes.Two related processes, one machine - a parent and a child it just forked, sharing a byte stream. Use
pipeorsocketpair. See Subprocesses and UNIX-domain and UDP sockets.Unrelated processes, one machine - two programs started independently that rendezvous on a known path. Use a FIFO or a UNIX-domain socket. See FIFOs and UNIX-domain and UDP sockets.
Processes on different machines - a network client and server. Use a TCP socket. See TCP sockets.
A datagram, fire-and-forget - no connection, message boundaries preserved. Use a UDP socket. See UNIX-domain and UDP sockets.
A shared counter or buffer with no copying - System V semaphores and shared memory. Use
semgetandshmget. See System V IPC.
How this tutorial is organised#
Each page stands on its own. Read the one that answers your question.
Subprocesses and co-processes - running a command and talking to it, in one or both directions, with the pipe open and with
IPC::Open2/IPC::Open3.FIFOs - a named pipe on the filesystem that lets two unrelated programs meet on a path.
TCP sockets - a complete client and a forking echo server over the loopback interface.
UNIX-domain and UDP sockets - local stream sockets via
socketpair, and connectionless UDP datagrams.System V IPC - semaphores and shared memory with the raw
semget/shmgetbuilt-ins.
What this tutorial does not cover#
Signals as a coordination mechanism beyond the basics. Sending a signal with
killand catching it through%SIGis covered where each recipe needs it; the full signal-disposition table lives in the%SIGreference.In-process parallelism. Running work across CPU cores inside a single program is auto-parallelization, a different subject. See the Concurrent Execution guide.
High-level networking modules. This tutorial stays at the built-in
socketlayer so the mechanics are visible. The recipes are the foundation a higher-level wrapper would build on.