Pipes, signals and IPC
Quick answer
Interprocess communication (IPC) lets programs exchange data or coordinate actions. Pipes transmit bytes, signals notify processes of events, and Unix-domain sockets support local communication. Choose the mechanism according to the data and synchronization requirements.
A reader gets end-of-file only when all write descriptors for a pipe are closed and buffered data is exhausted. An inherited but unused write descriptor can keep the reader waiting. In a private test, close unneeded ends and check the child termination status.
Prepare the practice environment
Run python3 lab-fixtures/syscalls/setup.py from the extracted root as an ordinary user. Enter the printed fresh private path and check pwd. Use fresh fixtures, Bash with set -e -o pipefail, LC_ALL=C and umask 077. Run each snippet in a separate Bash process, not your interactive shell. Examples use Python 3 standard-library wrappers on Linux, private files, bounded child processes and local socketpairs. No root or external network is needed. Optional strace inspection requires an authorized environment. Review the lab guide.
Prerequisites
Review: Process creation and exec.
How it works
A pipe has a read end and write end. It is a byte stream, not a message queue; reads can split or combine writes. EOF occurs after all write references close and buffered bytes are drained. Writing without readers raises SIGPIPE or returns EPIPE when that signal is ignored. Signals carry limited notification information; ordinary signals can coalesce. Signal handlers in C have async-signal-safety constraints; the Python example uses synchronous sigwait instead. Unix-domain SOCK_STREAM sockets support two-way local byte streams; SOCK_DGRAM preserves datagram boundaries. System V message queues, semaphores and shared memory are separate IPC facilities; ipcs reports those objects in the applicable IPC namespace, not anonymous pipes or POSIX shared memory objects. Shared memory needs separate synchronization. See pipe(7), signal(7), unix(7), sysvipc(7).
Try the examples
python3 - <<'PYCODE'import osr, w = os.pipe()try: try: assert os.write(w, b'lab') == 3 finally: os.close(w) chunks = [] while True: chunk = os.read(r, 2) if not chunk: break chunks.append(chunk) assert b''.join(chunks) == b'lab' print('pipe data verified; EOF')finally: os.close(r)PYCODEThe tiny payload fits in the pipe. Closing its only writer permits the final read to return EOF. Larger transfers need concurrent readers.
Expected stdout:
pipe data verified; EOFpython3 - <<'PYCODE'import os, signalsignals = {signal.SIGUSR1}old = signal.pthread_sigmask(signal.SIG_BLOCK, signals)try: os.kill(os.getpid(), signal.SIGUSR1) assert signal.sigwait(signals) == signal.SIGUSR1 print('SIGUSR1 consumed synchronously')finally: signal.pthread_sigmask(signal.SIG_SETMASK, old)PYCODEBlock SIGUSR1, send it only to this process, consume it with sigwait and restore the mask.
Expected stdout:
SIGUSR1 consumed synchronouslypython3 - <<'PYCODE'import socketa, b = socket.socketpair(socket.AF_UNIX, socket.SOCK_DGRAM)with a, b: a.settimeout(1); b.settimeout(1) a.send(b'ping') assert b.recv(16) == b'ping' b.send(b'pong') assert a.recv(16) == b'pong' print('local ping/pong verified')PYCODEDatagram socketpair creates connected endpoints without a public listener or filesystem socket path.
Expected stdout:
local ping/pong verifiedCommon mistakes
Do not assume a pipe read corresponds to one write. Close unused ends, avoid signal handlers that do arbitrary work, and never signal unrelated processes.
Practice
Change the pipe chunk size to 1; verify the assembled bytes. Explain why a pipe is useful for a one-way stream, a signal for notification and a socketpair for two-way exchange.
Related references
Study: ipcs. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Pipes, signals and IPC — Knowledge Check
Apply Linux systems programming interfaces to concrete local examples.
When does pipe EOF occur?
What does a pipe preserve?
What is sigwait useful for?
What does ipcs report?