Chapter 24: Process Creation

Chapter 24: Process Creation
Complete Tutorial Series  |  The Linux Programming Interface
7
Topics
20
Subtopics
60+
Code Examples

What You Will Learn

This series covers Chapter 24: Process Creation from The Linux Programming Interface (TLPI) by Michael Kerrisk. You will master fork(), vfork(), memory semantics (copy-on-write), file sharing, race conditions, and signal-based synchronization — all with hands-on C examples.

Series Keywords:

fork() vfork() execve() wait() copy-on-write file descriptors race conditions sigsuspend() process tree text segment

📚 Topic 1: Overview of Process Creation
Subtopic 1: What is Process Creation?

Process definition, parent-child relationships, process tree, real-world examples (server patterns)

Subtopic 2: The Four Core System Calls

fork(), exit(), wait(), execve() — lifecycle diagram, status macros, exec() family table

Subtopic 3: UNIX fork+exec vs spawn()

Why UNIX separates creation from execution, I/O redirection between fork and exec, posix_spawn()

🔨 Topic 2: fork() in Depth
Subtopic 1: How fork() Works Internally

Kernel steps, inherited vs unique attributes table, file locks not inherited, signal handler inheritance

Subtopic 2: fork() Return Values & the Switch Idiom

The three return cases (-1/0/+PID), switch/case pattern, multiple children, safe_fork() wrapper

Subtopic 3: Memory Segments After fork()

Stack/data/BSS/heap/text layout, t_fork.c from TLPI, heap independence, /proc memory viewing

Subtopic 4: Controlling Memory Footprint with fork()

footprint.c pattern, isolating leaky functions, returning results via exit status and pipes

📄 Topic 3: File Sharing After fork()
Subtopic 1: File Descriptors After fork()

3-layer file model diagram, shared vs unique properties, fork_file_sharing.c, coordinated writing

Subtopic 2: Shared File Offset & Status Flags

lseek() offset sharing, O_APPEND atomic writes, fcntl F_SETFL, independent re-open pattern

Subtopic 3: Closing Unused File Descriptors

Pipe EOF problem, O_CLOEXEC/FD_CLOEXEC, close_all_fds_except() utility

🔁 Topic 4: Memory Semantics & Copy-on-Write
Subtopic 1: Copy-on-Write (CoW)

CoW step-by-step, page table before/after diagram, fork timing benchmark, /proc RSS observation

Subtopic 2: Text Segment Sharing

Why text is always shared, r-xp permissions, /proc/PID/maps, shared library text pages

Subtopic 3: Memory Footprint: CoW in Practice

VSZ vs RSS explained, measuring CoW with /proc, Linux overcommit, minimize before fork

⚠ Topic 5: vfork()
Subtopic 1: Introduction to vfork()

Historical context, what vfork() guarantees, signature, the only correct usage pattern

Subtopic 2: vfork() vs fork() — Side by Side

Complete comparison table, t_vfork.c from TLPI, parent suspension proof, benchmark

Subtopic 3: vfork() Dangers & Obsolescence

Safe vs dangerous actions, stdio double-flush bug, stack corruption, POSIX obsolescence

🔃 Topic 6: Race Conditions After fork()
Subtopic 1: Race Conditions After fork()

Indeterminate execution order, race condition demo, shared file race, why sleep() doesn’t fix it

Subtopic 2: Scheduling Order After fork()

Linux kernel version history, sched_child_runs_first tunable, multicore implications

🔔 Topic 7: Signal-Based Synchronization
Subtopic 1: Synchronizing with Signals After fork()

SIGUSR1 pattern, sigsuspend() vs pause(), block-before-fork rule, mutual sync

Subtopic 2: Signal Sync: Full Implementation

fork_sig_sync.c from TLPI, pipe-based sync alternative, reusable sync helper, multiple children

📌 Quick Reference: Chapter 24 Key System Calls
Call Signature Returns Key Fact
fork() pid_t fork(void) 0 child, PID parent, -1 error Copy-on-write memory
vfork() pid_t vfork(void) Same as fork() Shares address space; parent suspended
wait() pid_t wait(int *status) PID of exited child Reaps zombie; use WIFEXITED/WEXITSTATUS
waitpid() pid_t waitpid(pid, status, opts) PID of waited child WNOHANG for non-blocking; -1 for any child
execve() int execve(path, argv, envp) -1 on error; never returns on success Replaces process image; PID unchanged
_exit() void _exit(int status) Never returns Use in forked child; no stdio flush
sigsuspend() int sigsuspend(const sigset_t*) Always -1 (EINTR) Atomic unblock + sleep; use for sync
Start Learning
Begin with Topic 1, Subtopic 1 below

Start: What is Process Creation? →

Leave a Reply

Your email address will not be published. Required fields are marked *