Search Knowledge

© 2026 LIBREUNI PROJECT

Operating Systems Engineering/Processes and CPU time
Page 5 of 29

Process API, IPC, and Signals

Derive POSIX process construction, descriptor sharing, pipe lifetime, child reaping, and asynchronous signal handling from object and ownership traces.

Lesson briefProcesses and CPU time · 5 h 30 min · 4 outcomes

Required knowledge

  • Processes, threads, address spaces, and process state
  • System-call entry, checked user memory, and blocking/wakeup semantics
  • C file descriptors, function pointers, and error handling with errno

Learning outcomes

  • Trace fork, exec, _exit, and waitpid across process images, thread sets, descriptors, and lifecycle state.
  • Distinguish a file descriptor from an open file description and predict offset, pipe-end, and close-on-exec behavior after duplication.
  • Construct a deadlock-free pipeline by closing every unreachable pipe end and proving when EOF becomes observable.
  • Design a sigaction-based notification path that respects masks, pending state, coalescing, and async-signal-safety.

Assessment target

Construct a three-process POSIX pipeline with redirection, error reporting, cancellation, and child reaping; verify descriptor/process traces that prove EOF and demonstrate a safe asynchronous notification path.

Scope boundary

  • Sessions, job-control terminals, process groups, posix_spawn, shared memory, sockets, message queues, and full shell parsing are outside the core construction.
  • Linux-specific clone, pidfd, signalfd, namespaces, and subreapers are extension directions rather than portable POSIX guarantees.
  • The lesson specifies robust patterns but does not claim that signals provide lossless event counting.

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:

  1. process and parent-child relationships;
  2. process images and threads;
  3. descriptor edges to kernel open-file objects; and
  4. 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.

HintExpand

fork duplicates descriptor-table edges; it does not move them.

Worked solutionExpand

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.

OperationProcess identityAddress space / imageThreadsDescriptor effectReturn behavior
fork()creates a child with a new PID and parent linkchild begins as a specified copy of parent statechild contains only the calling thread in a multi-threaded parentcopied descriptors refer to the same corresponding open file descriptionsreturns child PID in parent, 0 in child, -1 on failure
execve() familypreserves process identityreplaces current process imagesuccessful call leaves the initial thread of new image; other old threads enddescriptors normally survive unless close-on-exec appliesdoes not return on success
_exit(status)enters terminal lifecycle stateimage no longer executesno live execution remainsdescriptors close as process resources are releasednever returns
waitpid()parent selects eligible child statusdoes not replace parent imagecaller may blockno descriptor transformation by itselfreports 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 solutionExpand

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.

Code
rectangle "parent descriptor table" as P {
rectangle "fd 3" as P3
rectangle "fd 4" as P4
}
rectangle "child descriptor table" as C {
rectangle "fd 3" as C3
rectangle "fd 1 after dup2" as C1
}
rectangle "open file description\noffset = 128\nstatus = O_WRONLY" as OFD
rectangle "pipe write-end description\nwriter references = 2" as PW
database "regular file inode" as I
queue "pipe buffer" as B

P3 --> OFD : descriptor edge
C3 --> OFD : inherited edge
OFD --> I : open object
P4 --> PW : write edge
C1 --> PW : duplicated write edge
PW --> B : endpoint

note bottom of OFD
A read/write through either fd 3
observes one shared file offset.
end note
note bottom of PW
EOF requires every write edge closed,
not merely an empty buffer.
end note
parent descriptor tablechild descriptor tablefd 3fd 4fd 3fd 1 after dup2open file descriptionoffset = 128status = O_WRONLYpipe write-end descriptionwriter references = 2regular file inodepipe bufferA read/write through either fd 3observes one shared file offset.EOF requires every write edgeclosed,not merely an empty buffer.descriptor edgeinherited edgeopen objectwrite edgeduplicated write edgeendpoint

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 solutionExpand

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

0 / 4 answered

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.

1
If the child independently calls open() on the pathname, it obtains a distinct file description and private offset.
2
The current read/write byte offset is stored directly inside the per-process integer file descriptor table index.
3
Both child and parent descriptors refer to the same open file description, sharing one advancing file offset.
4
Executing fork() duplicates the underlying file content in storage, giving the child an isolated copy of data.

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 imagefd 0fd 1Descriptors that must be closed before exec
sortinfd’s open file descriptionpipe write endinfd, outfd, p[0], original p[1] after dup2
uniq -cpipe read endoutfd’s open file descriptioninfd, outfd, original p[0] after dup2, p[1]
parent shellunchangedunchangedinfd, outfd, p[0], p[1] after both children are created

One robust order is:

  1. create the pipe and open both redirection files using close-on-exec where available;
  2. fork the sort child; in it, atomically duplicate infd→0 and p[1]→1, close all originals and unrelated ends, then exec;
  3. fork the uniq child; in it, duplicate p[0]→0 and outfd→1, close all originals and unrelated ends, then exec;
  4. parent closes every pipeline descriptor and waits for both child PIDs, retrying waits interrupted before status is obtained as required by its policy.
Code
participant "shell parent" as P
participant "sort child" as S
participant "pipe kernel object" as K
participant "uniq child" as U

P -> K : pipe() creates read/write descriptors
P -> S : fork()
S -> S : dup2(infd,0); dup2(pipe_w,1)
S -> S : close inherited extras; exec sort
P -> U : fork()
U -> U : dup2(pipe_r,0); dup2(outfd,1)
U -> U : close inherited extras; exec uniq
P -> K : close parent pipe_r and pipe_w
S -> K : write sorted bytes; close on exit
K -> U : bytes in FIFO order
K --> U : read returns 0 after final writer closes
U --> P : exit status becomes waitable
S --> P : exit status becomes waitable
P -> P : waitpid each recorded PID
shell parentsort childpipe kernel objectuniq childshell parentshell parentsort childsort childpipe kernel objectpipe kernel objectuniq childuniq childpipe() creates read/writedescriptorsfork()dup2(infd,0);dup2(pipe_w,1)close inherited extras;exec sortfork()dup2(pipe_r,0);dup2(outfd,1)close inherited extras;exec uniqclose parent pipe_r andpipe_wwrite sorted bytes; closeon exitbytes in FIFO orderread returns 0 after finalwriter closesexit status becomeswaitableexit status becomeswaitablewaitpid each recordedPID

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.

  1. consumer retains p[1];
  2. parent closes p[0] before forking the consumer;
  3. producer forgets to close original p[1] after dup2(p[1],1);
  4. parent waits for producer before creating consumer while producer writes more than pipe capacity.
Worked solutionExpand
  1. Consumer waits forever for EOF after draining data because it is itself a writer reference.
  2. The consumer cannot inherit the read end; dup2 fails or an accidentally reused descriptor is redirected.
  3. 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.
  4. 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 referencesBufferResult
at least oneemptyblock until data, signal interruption, or all writers close
zeroemptyreturn 0 (EOF)
anynon-emptyreturn 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 solutionExpand

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 solutionExpand

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:

  1. generation: an event targets a process or thread;
  2. pending: the signal has been generated but is blocked or not yet selected for delivery;
  3. 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.

Code
state "not pending" as N
state "pending + blocked" as PB
state "pending + deliverable" as PD
state "handler active with mask extended" as H
state "default/ignore action" as D

[*] --> N
N --> PB : generate while blocked
N --> PD : generate while unblocked
PB --> PD : target thread unmasks
PB --> PB : ordinary signal generated again; count not guaranteed
PD --> H : select disposition = handler
PD --> D : select default or ignore
H --> N : handler returns; saved context restored
D --> N : action completes if process survives

note right of H
Only async-signal-safe operations are portable.
Save and restore errno when the handler may change it.
end note
not pendingpending + blockedpending + deliverablehandler active with maskextendeddefault/ignore actionOnly async-signal-safeoperations are portable.Save and restore errno when thehandler may change it.generate while blockedgenerate whileunblockedtarget thread unmasksordinary signalgenerated again; countnot guaranteedselect disposition =handlerselect default or ignorehandler returns; savedcontext restoredaction completes ifprocess survives

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

  1. Parent blocks SIGCHLD while installing its handler and constructing the notification pipe.
  2. Child A exits; SIGCHLD becomes pending.
  3. Child B exits before the signal is delivered. The implementation need not queue a second ordinary SIGCHLD occurrence.
  4. Parent unblocks; handler runs once and writes one byte.
  5. 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 solutionExpand

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.

Section Exercises

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

Descriptionapplication

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().

HintExpand

Trace the three-tier pointer chain from fd table index to struct file to struct inode.

Worked solutionExpand

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

Constructionapplication

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.

HintExpand

Recall that read() on a pipe returns EOF (0 bytes) only when the write reference count reaches zero.

Worked solutionExpand

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

Verificationtransfer

Two processes AA and BB communicate over two unidirectional pipes P1P_1 A→BA \to B and P2P_2 B→AB \to A Construct a deadlock schedule where both processes block, and explain what happens if process BB crashes while process AA attempts to write to P1P_1

HintExpand

Consider finite pipe buffer capacities and write operations to broken pipes without readers.

Worked solutionExpand

Deadlock occurs if both processes fill their outgoing pipe buffers without draining their incoming pipes. For example, AA writes 64 KiB to P1P_1 (filling the kernel buffer) and then blocks attempting to write more before reading from P2P_2 Concurrently, BB writes 64 KiB to P2P_2 and blocks attempting to write before reading from P1P_1 Both processes sleep waiting for write buffer space that will never be drained. If process BB crashes, all its descriptors (including the read end of P1P_1 are closed by the kernel. When process AA attempts to write to P1P_1 with zero active readers, the kernel generates a SIGPIPE signal to AA 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

Formalisationtransfer

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.

HintExpand

Standard POSIX signals represent pending bits that coalesce multiple arrivals into a single flag.

Worked solutionExpand

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

Descriptionsynthesis

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.

HintExpand

Consider a background thread that was inside a heap allocator mutex at the instant fork() was called.

Worked solutionExpand

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

Constructionsynthesis

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.

HintExpand

Use unidirectional diagnostic pipes with O_CLOEXEC to communicate exec errors back to the parent.

Worked solutionExpand
  1. Create inter-process data pipes p1[2] and p2[2] with pipe2(..., O_CLOEXEC).
  2. Create an auxiliary diagnostic pipe diag[2] with O_CLOEXEC.
  3. In each child:
    • Use dup2() to bind p[0] to STDIN_FILENO and p[1] to STDOUT_FILENO.
    • Call execvp(). If execvp() returns, an error occurred. The child writes the errno integer to diag[1] and immediately invokes _exit(127).
  4. If execvp() succeeds, the kernel automatically closes diag[1] due to O_CLOEXEC, delivering EOF to the parent on diag[0].
  5. 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 via kill(SIGTERM) and loops over waitpid() to reap all children cleanly.

References & Further Reading

  1. The Open Group, POSIX.1-2024 fork(), exec, and wait()/waitpid().
  2. The Open Group, POSIX.1-2024 open(), dup()/dup2(), pipe(), and read().
  3. The Open Group, POSIX.1-2024 sigaction() and General Information §2.4, for signal generation, delivery, masks, and async-signal-safety.
  4. MIT PDOS, xv6: a simple, Unix-like teaching operating system, Chapters 1 and 7, plus rev5 kernel/proc.c, kernel/file.c, and kernel/pipe.c, as a compact implementation contrast rather than a complete POSIX implementation.