Parent process

On this page 7

A parent process is a running process that creates another process, called its child, and remains responsible for that child until the child terminates and its exit status is collected. In the study of processes & scheduling within operating systems, the parent-child relationship is the backbone of the process tree: every process except the very first one has exactly one parent at any moment.

The relationship matters far beyond bookkeeping. The parent decides what the child inherits, such as environment variables, open file handles and security credentials, and it controls cleanup after termination. A parent that neglects its children leaves behind zombie entries, leaks resources or loses track of failures in the programs it started.

How a parent process creates a child

A parent process creates a child through a process creation system call, and the exact mechanism differs between operating system families. On Unix-like systems such as Linux and macOS, the classic pattern is `fork()` followed by `exec()`: fork() duplicates the calling process, and the new copy then replaces its program image with exec(). On Windows, a single call, `CreateProcess`, builds the new process and loads the target program in one step.

Modern kernels make duplication cheap through copy-on-write: parent and child initially share the same physical memory pages, and a page is copied only when one side writes to it. Variants such as vfork(), posix_spawn() and Linux clone() give finer control over what is shared and what is copied, which is why threads and containers are built on the same primitives.

The following table compares the main creation interfaces that a parent process uses on common platforms.

InterfacePlatformWhat the parent gets backTypical use
fork()Unix-likeChild PID in parent, 0 in childShells, servers that pre-fork workers
posix_spawn()POSIX systemsChild PIDLaunching programs without a full copy
clone()LinuxChild thread or process IDThreads, containers, namespaces
CreateProcessWindowsProcess and thread handlesLaunching any executable
Process creation interfaces available to a parent

What the child inherits from its parent

A child process starts with a set of attributes copied or derived from its parent, and knowing this set prevents many security and resource bugs. Inheritance is deliberate: it lets a shell pass its working directory and environment to every command, and it lets a server hand an already open socket to a worker.

The table below summarizes typical inheritance behavior on Unix-like systems, with Windows noted where it differs.

AttributeInherited on Unix-like systemsNotes
Environment variablesYes, copiedWindows also copies the environment block by default
Open file descriptorsYes, unless marked close-on-execWindows inherits only handles marked inheritable
User and group IDsYesSetuid programs can change them after exec
Working directoryYesChild may change it independently
Signal handlersReset to default after execIgnored signals stay ignored
Memory contentsCopied after fork, replaced by execCopy-on-write keeps the copy cheap
Attributes passed from parent to child

Leaked file descriptors are a common failure: a parent that opens a log file or a network connection without the close-on-exec flag passes it to every child, which can keep ports busy or expose sensitive files to an untrusted program.

Waiting, exit status and zombie processes

A parent process is expected to reap its children by calling wait(), waitpid() or, on Windows, by waiting on the process handle and reading the exit code. When a child exits, the kernel keeps a small record in the process control block that holds the exit status until the parent reads it.

If the parent never collects that status, the finished child stays in the process table as a zombie process. A zombie uses no CPU time and almost no memory, but each one occupies a process ID, and thousands of them can exhaust the PID limit and block new process creation. On Unix-like systems, the kernel sends the parent a SIGCHLD signal when a child changes state, so well-designed parents reap children asynchronously in a signal handler or event loop.

The following table describes how the parent-child state combinations play out.

SituationResulting stateWho cleans up
Child exits, parent waitsNormal terminationParent reaps immediately
Child exits, parent never waitsZombie processNobody until the parent exits
Parent exits firstOrphan processAdopted by init or a subreaper
Parent ignores SIGCHLD explicitlyNo zombie createdKernel discards the status
Parent and child state combinations

The parent role is relative, not absolute: almost every process is both a child of one process and a potential parent of others. On Linux the root of the tree is PID 1, usually systemd or another init system, while on Windows the tree begins with system processes such as smss.exe and wininit.exe. The term is also distinct from a process group or session, which group processes for job control and terminal signals rather than for creation and cleanup.

When a parent dies before its children, those children become orphan processes and are re-parented. Linux adopts them to PID 1 or to the nearest ancestor marked as a child subreaper with prctl(PR_SET_CHILD_SUBREAPER), a feature that container runtimes and service managers use to keep reaping under control. Windows behaves differently: it records only the parent process ID at creation time and does not re-parent, so the stored ID can later point to an unrelated process that reused the number.

Practical responsibilities of a well-behaved parent

Software that launches other programs, from shells and build tools to web servers and process supervisors, takes on a set of parent responsibilities that determine its reliability. The following decision table summarizes the main design choices.

RequirementRecommended practiceReason
Avoid zombiesReap in a SIGCHLD handler or event loopFrees PIDs promptly
Avoid leaked handlesSet close-on-exec on every descriptorChild sees only what it needs
Detect child failureCheck the exit status and signalCrashes do not go unnoticed
Stop children cleanlySend SIGTERM, then SIGKILL after a timeoutGives children time to flush data
Run as PID 1 in a containerUse a minimal init that reaps orphansPrevents zombie buildup
Design choices for programs that spawn children
  • Check return values of fork and spawn calls, because creation fails when PID or memory limits are reached.
  • Avoid fork in multithreaded parents unless exec follows immediately, since only the calling thread is duplicated and locks may stay held.
  • Limit child fan-out with a pool or semaphore, so a burst of work cannot trigger a fork bomb effect.

Stripped to the essentials

A parent process is a program that starts another program. Think of a manager who hires a temporary worker: the manager gives the worker some tools and instructions, and when the job is done, the manager signs off on the result.

If the manager never signs the final paperwork, the worker is gone but still listed on the payroll. Computers have the same problem, which is why a parent should always collect the result of each child it started.

Where it is applied

A web hosting team runs an older application server that pre-forks eight worker processes from one master process on a Linux machine. The master is the parent: it opens the listening socket on port 443 once, forks the workers so that each inherits the socket, and then monitors them.

When a worker crashes after a memory error, the master receives SIGCHLD, calls waitpid() to read the exit signal, logs the failure and forks a replacement within milliseconds. Because the master reaps every child immediately, the process table stays clean even after weeks of uptime, and visitors see no interruption in service while individual workers come and go.