Operating systems · intermediate

Parent and child processes

A child process is created by an existing parent process and records that relationship through a parent process ID (PPID). On Unix-like systems, creation commonly begins with fork-like semantics, after which the child may execute another program.

Why it matters

Shells launch commands as children, pass them inherited resources, and collect their exit status. This relationship explains job control, environment inheritance, pipelines, orphan adoption, and zombie processes.

Mental model

How to reason about parent and child processes

Process creation starts with inherited state, not a blank slate. Parent and child then run independently: changes to ordinary memory in one do not rewrite the other's memory, while inherited kernel resources such as file descriptors may still refer to the same underlying open file description.

Analogy

A child process is like a branch made from a document template: it begins with a snapshot of the parent's setup, then becomes an independently edited document with its own identifier.

Examples

See the boundary, not just the happy path

Worked example · Observe shell parentage

sh -c 'printf "PID=%s PPID=%s\n" "$$" "$PPID"'

The current shell starts sh as a child. Inside that child, $$ names its own PID and $PPID usually names the shell that launched it, subject to the shell's implementation and process optimizations.

Worked example · Track a background child

sleep 30 & child=$!; ps -p "$child" -o pid=,ppid=,comm=; wait "$child"

$! captures the background child's PID. wait asks the parent shell to collect that child's termination status, preventing an unreaped child from remaining a zombie.

Useful contrast · Child process versus thread

A new thread normally shares its process's address space

A child process has a distinct process identity and address space. Threads are execution flows inside one process and normally share that process's memory and file-descriptor table.

Common mistakes

Misconceptions to remove early

Assuming parent and child share ordinary variables

After process creation they have separate address spaces. Copy-on-write can make initial memory sharing efficient, but a normal write in one process is not a variable update in the other.

Assuming every child dies with its parent

That is not the default Unix rule. An orphaned child can continue and be adopted by a subreaper or the system's init process; explicit supervision is needed when lifetimes must be coupled.

Quick check

Can you predict the result?

1. What does a shell's wait command do for one of its child processes?
  • It waits for the child and collects its termination status.
  • It converts the child into a thread.
  • It copies the child's modified memory into the parent.
Answer: It waits for the child and collects its termination status.
2. Why can inherited file descriptors still interact even though parent and child have separate memory?
Answer: Their descriptor entries can refer to the same kernel open file description, including its file offset and status flags.

Keep building

Authoritative references

Make the idea retrievable.

Study this concept with spaced repetition, next to the commands where you use it.

Get Terminaster