Child process

On this page 7

A child process is a process that another running process, its parent process, creates through an operating system call, receiving its own process identifier, its own address space and a copy or subset of the parent’s resources. In operating systems, child processes are the basic mechanism by which programs launch other programs, and they sit at the center of the topic of processes & scheduling because every new process except the very first one is a child of some other process.

The relationship matters beyond creation. The kernel records who created whom, delivers termination status from child to parent, and applies rules about inherited resources such as open files, environment variables, security credentials and resource limits. Understanding these rules explains common problems such as zombie entries, leaked file handles and runaway process trees.

How an operating system creates a child process

A child process is created when a parent asks the kernel to allocate a new process control block, assign a unique process ID and prepare an execution context. The exact interface differs by platform, but every design must answer the same questions: which program the child runs, what memory it starts with, and which resources it inherits.

On Unix-like systems the classic sequence is fork followed by exec. The fork() call duplicates the calling process, and both copies continue from the same instruction; the call returns the child’s PID to the parent and zero to the child. The child then usually calls one of the exec functions, which replaces its memory image with a new program while keeping the PID and most open descriptors. Modern kernels implement fork with copy-on-write pages, so the duplication costs little until either process writes to memory.

Windows takes a different route. The CreateProcess function builds a new process and loads the target executable in one combined step, with explicit parameters for handle inheritance, environment block, working directory and creation flags. POSIX also offers posix_spawn, which combines creation and program loading and avoids duplicating a large parent.

The following table compares the main process creation interfaces and their typical use.

InterfacePlatformWhat it doesTypical use
fork()Unix, Linux, macOSDuplicates the caller with copy-on-write memoryServers that split work, shells before exec
exec familyUnix, Linux, macOSReplaces the current image with a new programRunning a different binary after fork
vfork()Unix, LinuxShares parent memory until exec or exitLegacy speed optimization, now rarely needed
posix_spawn()POSIX systemsCreates and loads a program in one callLaunching tools from large applications
CreateProcessWindowsCreates a process and loads an executableStarting any program on Windows
clone()LinuxCreates a task with selectable shared resourcesContainers, threads, custom runtimes
Common interfaces for creating a child process

What a child process inherits from its parent

A child process inherits a defined set of attributes and starts fresh in others. This boundary is where many security and reliability bugs appear, because developers often assume that a child starts clean when it actually holds copies of the parent’s file descriptors and credentials.

The next table summarizes typical behavior on POSIX systems after fork and exec. Windows is broadly similar, but handle inheritance is opt-in for each handle and for each call.

AttributeAfter forkAfter execPractical consequence
Open file descriptorsCopied, sharing file offsetsKept unless close-on-exec is setLeaked sockets or files in helpers
Environment variablesCopiedKept or replaced by the callerSecrets in the environment travel along
User and group IDsCopiedKept, except for setuid programsChild runs with the same privileges
Memory contentsCopy-on-write duplicateDiscarded and replacedNo memory shared after exec
Signal handlersCopiedReset to defaultsCustom handlers do not survive exec
Resource limitsCopiedKeptLimits apply to the whole process tree
Pending signals and timersClearedClearedChild does not receive parent alarms
Inherited and reset attributes after fork and exec

The safest habit is to mark descriptors with the close-on-exec flag when opening them, to pass a minimal environment, and to drop privileges before running untrusted code. Programming languages that wrap process creation usually apply these defaults, but low-level code must set them explicitly.

Lifecycle, termination and reaping

A child process moves through the usual process states, ready, running, blocked and terminated, and it is scheduled independently of its parent. The scheduler does not favor the parent or the child after creation; on Linux, either may run first depending on configuration and load.

When a child exits, the kernel keeps a small record holding its exit status until the parent collects it with wait() or waitpid(), or with WaitForSingleObject and GetExitCodeProcess on Windows. A finished child whose status has not been collected is a zombie process. If the parent dies first, the child becomes an orphan process and is adopted by init or a designated subreaper, which reaps it later.

SituationResulting stateWho cleans upRisk
Child exits, parent waitsNormal terminationParent through waitNone
Child exits, parent ignores itZombie entry remainsNobody until the parent exitsPID table exhaustion
Parent exits firstOrphan adopted by initInit or a subreaperUnsupervised background work
Parent killed, group signaledWhole group terminatesKernel and initLost unsaved work in children
Parent and child situations after termination

Parents learn about finished children through the SIGCHLD signal on Unix-like systems. A robust server either installs a handler that calls waitpid in a loop or sets SIGCHLD to be ignored so the kernel reaps children automatically.

Child processes compared with threads

A child process and a thread both provide concurrent execution, but they differ in isolation. A child has its own address space, so a crash or memory corruption in the child does not damage the parent. Threads share one address space, which makes communication cheap and failures contagious.

Because children do not share memory by default, they communicate through inter-process communication: pipes, sockets, shared memory regions or message passing. This cost is the price of fault isolation, which is why web browsers place each site in a separate renderer process and why databases often run one backend process per client connection.

  • Choose a child process when the task runs a different program, needs separate privileges or must survive or contain crashes.
  • Choose a thread when tasks share large data structures and need fast, frequent coordination.
  • Choose a process pool when creation cost matters and work items are independent.

Common mistakes when managing child processes

Most problems with child processes come from incomplete cleanup or unexpected inheritance rather than from creation itself. Experienced engineers watch for the following failure patterns.

  • Forgetting to reap children, which leaves zombies and eventually prevents new processes from starting.
  • Forking a multithreaded parent, which copies only the calling thread and can leave locks held forever in the child.
  • Leaking descriptors into long-lived helpers, which keeps sockets open and blocks port reuse.
  • Building command lines from user input, which enables command injection when a shell interprets the string.
  • Assuming a fixed run order between parent and child, which produces race conditions.

In simple terms

A child process is a program that another program started. When you open a terminal and type a command, the terminal is the parent and the command becomes its child. The child gets its own workspace, so if it fails, the parent usually keeps running.

Think of a manager handing a task to a new team member. The new member receives copies of some tools and notes, works independently, and reports back when finished. The key point is that the parent must collect that final report, or the system keeps an empty desk reserved for someone who has already left.

A practical example

A web server running on Linux handles uploaded images. For each upload, the main server process starts a child process that runs an image conversion tool with a strict memory limit of 512 MB and a time limit of 30 seconds. The server opens all its sockets with the close-on-exec flag, so the converter cannot accidentally hold client connections open.

One day a malformed file crashes the converter. Because the work runs in a separate child, only that single conversion fails; the server receives the exit status through waitpid, logs the error and returns a clear message to the user. The main service stays online for every other visitor, which would not be guaranteed if the conversion code ran as a thread inside the server.