A shell runs generate | compress. Both programs finish their useful work, but compress waits forever for EOF. The parent retained one copy of the pipe’s write descriptor. No process will write another byte, yet the kernel must treat writing as still possible while any write end remains open. The defect is not in read; it is an ownership leak created during process construction.
Process APIs are easiest to reason about as transformations of four graphs:
- process and parent-child relationships;
- process images and threads;
- descriptor edges to kernel open-file objects; and
- asynchronous event state: mask, pending set, and disposition.
Prediction — who still owns the write end?
Start with parent descriptors r and w from pipe(). The parent forks a producer and consumer. No close calls occur. The producer exits after writing; the consumer reads until EOF. List every process that retains w after both forks and explain why producer exit is insufficient.
HintExpandCollapse
fork duplicates descriptor-table edges; it does not move them.
Worked solutionExpandCollapse
After both forks, parent, producer, and consumer each retain a descriptor referring to the pipe’s write-end open file description. Producer exit closes only its copy. Parent and consumer still keep the write end open, so an empty-pipe read blocks instead of returning zero. The consumer must close its inherited write descriptor, the producer must close its read descriptor, and the parent must close both ends once it no longer needs them. The missed concept is reachability/lifetime, not byte buffering.
fork, exec, and wait form a constructor protocol
The operations do not all “create a process.” Their state transformations differ.
| Operation | Process identity | Address space / image | Threads | Descriptor effect | Return behavior |
|---|---|---|---|---|---|
fork() | creates a child with a new PID and parent link | child begins as a specified copy of parent state | child contains only the calling thread in a multi-threaded parent | copied descriptors refer to the same corresponding open file descriptions | returns child PID in parent, 0 in child, -1 on failure |
execve() family | preserves process identity | replaces current process image | successful call leaves the initial thread of new image; other old threads end | descriptors normally survive unless close-on-exec applies | does not return on success |
_exit(status) | enters terminal lifecycle state | image no longer executes | no live execution remains | descriptors close as process resources are released | never returns |
waitpid() | parent selects eligible child status | does not replace parent image | caller may block | no descriptor transformation by itself | reports child/status or error; consumes waitable status as specified |
The classic shell sequence is fork to create a distinct process identity, modify descriptors in the child, then exec to replace only that child’s image. Performing descriptor redirection before fork would also redirect the parent. Calling exec without fork would replace the shell itself.
Retrieval — return paths are evidence
Why must code after a successful execve() be treated as an error path, while code after fork() must branch on three outcomes?
Worked solutionExpandCollapse
Successful execve() overlays the image and does not return to the old call site; reaching the following statement means failure and errno is meaningful. fork() returns in two processes with different values and can fail in the original process: negative for failure, zero in the child, positive child PID in the parent. A branch that handles only parent/child silently treats resource exhaustion as a child path or continues with an invalid PID.
Descriptor number is not open-file identity
open() creates an open file description containing status flags and file offset, then creates a process-local file descriptor that refers to it. dup2(old, new) makes new refer to the same open file description as old; fork() creates child descriptors referring to the corresponding same descriptions. Descriptor flags such as close-on-exec belong to descriptor edges, while file status and offset belong to the shared open-file description.
Closing a descriptor removes one edge. The kernel object remains alive while another edge or internal reference exists. Reusing the same small integer later does not resurrect the old object.
Application — shared offset trace
Parent opens a regular file at offset 0, forks, then parent reads 4 bytes and child reads 4 bytes through the inherited descriptor. Assume the reads occur in that order and both succeed. What offsets can each operation use? How would reopening the pathname in the child change the answer?
Worked solutionExpandCollapse
The inherited descriptors refer to one open file description. Parent consumes bytes at offsets 0–3 and advances the shared offset to 4; child then consumes offsets 4–7 and advances it to 8. If the child independently calls open, it receives a new open file description with its own initial offset, normally 0, so both may read bytes 0–3. A pathname identifies a file for lookup; it is not the runtime open-file object.
File Descriptor and Description Sharing Matrix
A parent opens a regular file, forks a child, and both read via the inherited descriptor. Evaluate each statement regarding file descriptor sharing:
Classify each option as correct or incorrect, or leave uncertain options unanswered (“I don't know”) and verify at any time.
Worked trace: sort < raw | uniq -c > counts
Let pipe ends be p[0] (read) and p[1] (write). Let infd and outfd denote opened files. The target post-exec descriptors are:
| Process image | fd 0 | fd 1 | Descriptors that must be closed before exec |
|---|---|---|---|
sort | infd’s open file description | pipe write end | infd, outfd, p[0], original p[1] after dup2 |
uniq -c | pipe read end | outfd’s open file description | infd, outfd, original p[0] after dup2, p[1] |
| parent shell | unchanged | unchanged | infd, outfd, p[0], p[1] after both children are created |
One robust order is:
- create the pipe and open both redirection files using close-on-exec where available;
- fork the
sortchild; in it, atomically duplicateinfd→0andp[1]→1, close all originals and unrelated ends, thenexec; - fork the
uniqchild; in it, duplicatep[0]→0andoutfd→1, close all originals and unrelated ends, thenexec; - parent closes every pipeline descriptor and waits for both child PIDs, retrying waits interrupted before status is obtained as required by its policy.
The construction must handle failures between forks. If the second fork fails, the parent still closes its pipe ends, terminates or drains policy-consistently, and reaps the first child. Error paths are part of descriptor ownership.
Failure injection — close one edge at a time
Predict behavior for each mutation.
- consumer retains
p[1]; - parent closes
p[0]before forking the consumer; - producer forgets to close original
p[1]afterdup2(p[1],1); - parent waits for producer before creating consumer while producer writes more than pipe capacity.
Worked solutionExpandCollapse
- Consumer waits forever for EOF after draining data because it is itself a writer reference.
- The consumer cannot inherit the read end;
dup2fails or an accidentally reused descriptor is redirected. - This is usually harmless for EOF after producer exit because both edges close together, but it leaks a descriptor during the image lifetime and becomes harmful if fd 1 closes earlier while the extra remains. Tests must cover staged closes, not only process exit.
- Producer can block on a full pipe while parent waits for it; consumer does not yet exist to drain, producing a construction deadlock. Create all pipeline stages before waiting.
Pipe semantics are state-dependent
For a blocking read on an empty pipe:
| Writer references | Buffer | Result |
|---|---|---|
| at least one | empty | block until data, signal interruption, or all writers close |
| zero | empty | return 0 (EOF) |
| any | non-empty | return available bytes subject to interface rules |
EOF is therefore a statement about future possibility, not a byte stored in the pipe. A leaked descriptor changes the kernel’s proof about future writes.
Transfer — request/reply over two pipes
Two processes use pipe A for requests and pipe B for replies. Each writes a request larger than pipe capacity before reading the reply. Construct the deadlock and give two redesigns with different trade-offs.
Worked solutionExpandCollapse
If both large writes fill their outbound pipe and block waiting for readers, neither reaches its read. Redesign 1 uses an event loop with nonblocking descriptors and bounded output queues; it adds readiness and partial-write state. Redesign 2 assigns a reader thread or process per direction; it adds concurrency and synchronization. A protocol-level redesign can bound each frame below available capacity and enforce alternating request/reply, but that couples correctness to capacity and excludes streaming. “Pipes are full duplex” is also wrong here: each POSIX pipe is one directed channel in the model.
waitpid closes the lifecycle loop
An exited child may retain minimal status and identity so its parent can observe termination. This zombie state is not runnable. waitpid selects status from an eligible child set; a parent that never waits can accumulate unreaped children until another system policy intervenes.
Robust supervisors record PIDs and associate status with the correct stage. Waiting “for any child” without a mapping can attribute failure to the wrong pipeline command. A pipeline’s overall exit policy—last stage, first failure, or aggregate—is an application decision, not a fact supplied by waitpid.
Application — status attribution
Three children exit in order C, A, B with statuses 0, 7, 0. The parent calls waitpid(-1, ...) three times. What extra state is necessary to report “stage A failed with 7,” and what race does the negative PID argument not create?
Worked solutionExpandCollapse
The parent needs a PID-to-stage mapping recorded at each successful fork and must decode each returned status. waitpid(-1, ...) can return any eligible child in completion order; it does not lose identity because it returns the PID. The race is in application bookkeeping if the mapping is missing or reused, not in the kernel confusing children. Waiting each specific PID simplifies attribution but may delay observing already-finished siblings.
Signals are asynchronous state transitions, not messages with queues by default
Separate three moments:
- generation: an event targets a process or thread;
- pending: the signal has been generated but is blocked or not yet selected for delivery;
- delivery/acceptance: the kernel applies the current disposition to a selected thread or a waiting interface.
A per-thread signal mask blocks delivery, not necessarily generation. A disposition can be default action, ignore, or a handler installed with sigaction. For ordinary non-realtime signals, repeated generation while already pending need not produce one delivery per occurrence; using a handler invocation as an event counter is therefore unsound. Realtime signals have different queuing rules and are deliberately outside this core pattern.
Counterexample — printf in a handler
The main thread is inside printf, holding an internal stdio lock, when a signal handler calls printf again. The handler can deadlock or corrupt library state. The same reasoning applies to malloc and arbitrary application data structures. volatile does not make a general library operation reentrant and is not thread synchronization.
The safest handler often records the smallest possible notification through an async-signal-safe operation, then lets ordinary code perform work. A self-pipe pattern is one option:
static int notify_fd;
static void on_sigchld(int signo) {
int saved_errno = errno;
unsigned char byte = (unsigned char)signo;
(void)write(notify_fd, &byte, 1); /* write end configured nonblocking */
errno = saved_errno;
}
The ordinary event loop drains the pipe and repeatedly calls waitpid(-1, &status, WNOHANG) until no more completed children are available. If the nonblocking pipe is full, dropping a notification byte is acceptable only because the loop treats it as a level-triggered prompt to reap all available children; signal counts are not inferred from bytes.
Signal trace — two children, one pending SIGCHLD
- Parent blocks
SIGCHLDwhile installing its handler and constructing the notification pipe. - Child A exits;
SIGCHLDbecomes pending. - Child B exits before the signal is delivered. The implementation need not queue a second ordinary
SIGCHLDoccurrence. - Parent unblocks; handler runs once and writes one byte.
- Event loop drains the byte and loops
waitpid(..., WNOHANG), reaping A and B.
The loop is correct because child status is the durable source of truth; signal delivery is only a wakeup hint.
Transfer — handler installation race
A supervisor forks a fast child, then installs its SIGCHLD handler. Sometimes the child exits first. Give a race-free ordering and explain why installing the handler earlier may still be incomplete in a multi-threaded process.
Worked solutionExpandCollapse
Block SIGCHLD in the constructing thread before creating the child, install the disposition and notification machinery, record the child’s PID, then unblock or use a synchronous signal-wait design. In a multi-threaded process, masks are per-thread and another unblocked thread may receive the process-directed signal; establish the mask policy in all relevant threads, commonly by blocking before creating worker threads. Even with a lost notification, a supervisor can perform an initial nonblocking reap after registration, but that must be part of the protocol.
The multi-threaded fork boundary
POSIX specifies that a child of a multi-threaded fork() contains only the calling thread but inherits an address-space image that may contain mutexes held by vanished threads. Until a successful exec, the child is sharply constrained; calling code that acquires such a lock can deadlock. The portable construction should use only permitted async-signal-safe operations in that interval, or use posix_spawn under a separately studied contract.
Deep Practice
Solve these problems to test and consolidate your understanding. Complete worked solutions and diagnostic hints are available for each problem.
Exercise 2.2.1: Distinguishing Integer File Descriptors from Open File Descriptions
Explain the architectural distinction between a per-process integer file descriptor, a system-wide open file description (struct file), and an underlying filesystem inode. Trace how file offset updates are shared across processes after fork().
HintExpandCollapse
Trace the three-tier pointer chain from fd table index to struct file to struct inode.
Worked solutionExpandCollapse
An integer file descriptor is merely an index into the per-process file descriptor table (proc->ofile[fd]). That entry points to a system-wide open file description (struct file), which holds the current read/write byte offset, access mode flags (O_RDONLY, O_WRONLY), and a reference count. The open file description in turn points to an inode representing the physical file on storage. When a process calls fork(), the child inherits a copy of the parent’s descriptor table; corresponding entries point to the same struct file instances with incremented reference counts. When either parent or child reads or writes, it updates the offset stored inside the shared struct file, so sequential operations naturally interleave across the single advancing stream.
Exercise 2.2.2: Descriptor Closing Invariants for Pipeline EOF Propagation
In a Unix pipeline cmd1 | cmd2, the shell creates a pipe int p[2], forks both children, duplicates descriptors, and executes both commands. Explain why the shell itself must close p[1] after forking both children, and identify the failure mode if it remains open in the shell.
HintExpandCollapse
Recall that read() on a pipe returns EOF (0 bytes) only when the write reference count reaches zero.
Worked solutionExpandCollapse
A pipe read end (p[0]) returns end-of-file (read() == 0) if and only if the pipe buffer is empty AND the kernel reference count for the write end of the pipe reaches zero. If the parent shell fails to close its inherited write descriptor p[1], the write reference count remains at least 1 even after cmd1 completes and closes its write end. Consequently, cmd2 blocks indefinitely waiting for further input on p[0], resulting in a pipeline hang / deadlock.
Exercise 2.2.3: Bidirectional Pipe Deadlocks and SIGPIPE Generation
Two processes and communicate over two unidirectional pipes and Construct a deadlock schedule where both processes block, and explain what happens if process crashes while process attempts to write to
HintExpandCollapse
Consider finite pipe buffer capacities and write operations to broken pipes without readers.
Worked solutionExpandCollapse
Deadlock occurs if both processes fill their outgoing pipe buffers without draining their incoming pipes. For example, writes 64 KiB to (filling the kernel buffer) and then blocks attempting to write more before reading from Concurrently, writes 64 KiB to and blocks attempting to write before reading from Both processes sleep waiting for write buffer space that will never be drained.
If process crashes, all its descriptors (including the read end of are closed by the kernel. When process attempts to write to with zero active readers, the kernel generates a SIGPIPE signal to and sets errno = EPIPE. If SIGPIPE is ignored, the write() returns -1 with EPIPE.
Exercise 2.2.4: Standard Signal Coalescing and Non-Blocking Waitpid Loops
A parent process spawns 5 child processes. All 5 children exit almost simultaneously. Explain why a signal handler that executes wait(NULL) exactly once per SIGCHLD delivery can orphan child processes, and formulate the correct reaping loop.
HintExpandCollapse
Standard POSIX signals represent pending bits that coalesce multiple arrivals into a single flag.
Worked solutionExpandCollapse
Standard POSIX signals (signals 1–31) are represented in the process struct as a single bitmask of pending signals without a FIFO queue. If multiple children exit while SIGCHLD is blocked or pending, the bit is set once; additional terminations are coalesced and lost. If the handler executes wait(NULL) only once, only the first child is reaped, leaving the remaining 4 children as permanent zombies until process termination. The correct handler implementation must reap in a non-blocking loop:
void sigchld_handler(int sig) {
int saved_errno = errno;
while (waitpid(-1, NULL, WNOHANG) > 0) {}
errno = saved_errno;
}This reaps all exited children until waitpid() returns 0 (children still running) or -1 with ECHILD (no children left).
Exercise 2.2.5: Async-Signal-Safety Constraints in Multi-Threaded Fork
POSIX specifies that after fork() in a multi-threaded process, the child process contains only the calling thread, but inherits all user-space heap memory and lock states. Explain why calling malloc() or printf() in the child before exec() can cause an unrecoverable deadlock.
HintExpandCollapse
Consider a background thread that was inside a heap allocator mutex at the instant fork() was called.
Worked solutionExpandCollapse
In a multi-threaded process, standard library functions like malloc() and printf() acquire internal mutexes protecting the heap allocator free lists and stdio output buffers. If another thread in the parent process happened to hold the allocator lock at the precise instant fork() executed, the child process inherits that memory image with the lock marked held. Because other parent threads do not exist in the child, no thread will ever release that lock. If the child calls malloc(), it attempts to acquire the held lock and deadlocks permanently. Between fork() and exec(), the child must only execute async-signal-safe system calls (such as close(), dup2(), and execve()).
Exercise 2.2.6: Multi-Stage Pipeline Launcher with Diagnostic Failure Handling
Design a robust Unix pipeline launcher for cmd1 | cmd2 | cmd3 that detects and cleanly recovers from pre-exec redirection errors in child processes without leaking open file descriptors or leaving zombie processes.
HintExpandCollapse
Use unidirectional diagnostic pipes with O_CLOEXEC to communicate exec errors back to the parent.
Worked solutionExpandCollapse
- Create inter-process data pipes
p1[2]andp2[2]withpipe2(..., O_CLOEXEC). - Create an auxiliary diagnostic pipe
diag[2]withO_CLOEXEC. - In each child:
- Use
dup2()to bindp[0]toSTDIN_FILENOandp[1]toSTDOUT_FILENO. - Call
execvp(). Ifexecvp()returns, an error occurred. The child writes theerrnointeger todiag[1]and immediately invokes_exit(127).
- Use
- If
execvp()succeeds, the kernel automatically closesdiag[1]due toO_CLOEXEC, delivering EOF to the parent ondiag[0]. - The parent closes all data pipe write ends and reads from
diag[0]. If it reads a non-zero integer, a child failed pre-exec; the parent terminates surviving children viakill(SIGTERM)and loops overwaitpid()to reap all children cleanly.
References & Further Reading
- The Open Group, POSIX.1-2024
fork(),exec, andwait()/waitpid(). - The Open Group, POSIX.1-2024
open(),dup()/dup2(),pipe(), andread(). - The Open Group, POSIX.1-2024
sigaction()and General Information §2.4, for signal generation, delivery, masks, and async-signal-safety. - MIT PDOS, xv6: a simple, Unix-like teaching operating system, Chapters 1 and 7, plus rev5
kernel/proc.c,kernel/file.c, andkernel/pipe.c, as a compact implementation contrast rather than a complete POSIX implementation.