--- name: interprocess communication tutorial --- # 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`](../../p5/core/perlfunc/socket) returns, how [`fork`](../../p5/core/perlfunc/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 `sort` or the input of `gzip`. Use a [pipe open](../io-streams/pipes), or `IPC::Open2` / `IPC::Open3` when you need both directions. See [Subprocesses and co-processes](subprocesses). - **Two related processes, one machine** - a parent and a child it just forked, sharing a byte stream. Use [`pipe`](../../p5/core/perlfunc/pipe) or [`socketpair`](../../p5/core/perlfunc/socketpair). See [Subprocesses](subprocesses) and [UNIX-domain and UDP sockets](sockets-unix-udp). - **Unrelated processes, one machine** - two programs started independently that rendezvous on a known path. Use a [FIFO](fifos) or a UNIX-domain socket. See [FIFOs](fifos) and [UNIX-domain and UDP sockets](sockets-unix-udp). - **Processes on different machines** - a network client and server. Use a TCP socket. See [TCP sockets](sockets-tcp). - **A datagram, fire-and-forget** - no connection, message boundaries preserved. Use a UDP socket. See [UNIX-domain and UDP sockets](sockets-unix-udp). - **A shared counter or buffer with no copying** - System V semaphores and shared memory. Use [`semget`](../../p5/core/perlfunc/semget) and [`shmget`](../../p5/core/perlfunc/shmget). See [System V IPC](sysv-ipc). ## How this tutorial is organised Each page stands on its own. Read the one that answers your question. ```{toctree} :maxdepth: 1 subprocesses fifos sockets-tcp sockets-unix-udp sysv-ipc ``` - **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`](../../p5/core/perlfunc/socketpair), and connectionless UDP datagrams. - **System V IPC** - semaphores and shared memory with the raw [`semget`](../../p5/core/perlfunc/semget) / [`shmget`](../../p5/core/perlfunc/shmget) built-ins. ## What this tutorial does not cover - **Signals as a coordination mechanism beyond the basics.** Sending a signal with [`kill`](../../p5/core/perlfunc/kill) and catching it through `%SIG` is covered where each recipe needs it; the full signal-disposition table lives in the [`%SIG`](../../p5/core/perlvar/signals) reference. - **In-process parallelism.** Running work across CPU cores inside a single program is auto-parallelization, a different subject. See the [Concurrent Execution](../../guide/concurrent-execution/index) 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.