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:

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 / shmget built-ins.

What this tutorial does not cover#

  • Signals as a coordination mechanism beyond the basics. Sending a signal with kill and catching it through %SIG is covered where each recipe needs it; the full signal-disposition table lives in the %SIG reference.

  • 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 socket layer so the mechanics are visible. The recipes are the foundation a higher-level wrapper would build on.