Orphan process

On this page 7

An orphan process is a process that keeps running after its parent process has terminated. In operating systems with a Unix-style process model, the kernel does not leave such a process without a parent: it reparents the orphan to init (process ID 1) or to a designated subreaper, which becomes responsible for collecting its exit status when it finally ends.

Within processes and scheduling, orphan processes matter because the parent-child relationship governs who reaps a finished process, who receives signals and how resources are cleaned up. Orphaning is a normal, deliberate technique for daemons, but unintended orphans can leak memory, hold ports and files, and keep consuming CPU time long after the program that started them has disappeared.

How an orphan process comes into existence

A process becomes an orphan the moment its parent exits before it does. On Unix-like systems a parent creates a child with fork(), and the kernel records the parent’s process ID in the child’s process control block. When the parent terminates, for any reason, the kernel walks the list of its children and assigns each surviving child a new parent.

Traditionally the new parent is init, process ID 1. Modern Linux also supports the PR_SET_CHILD_SUBREAPER option of prctl(), which lets a process such as systemd user managers or container init programs adopt orphaned descendants instead. The orphan itself is not notified in any special way; a call to getppid() simply starts returning the new parent’s ID.

The sequence below shows the typical lifecycle of an orphan from creation to final cleanup.

StageWhat happensWho is responsible
CreationParent calls fork() and the child starts runningOriginal parent
Parent exitParent terminates while the child is still aliveKernel detects the event
ReparentingChild receives init or a subreaper as its new parentKernel
Independent executionOrphan continues its work without the original parentScheduler treats it normally
Termination and reapingOrphan exits and its new parent calls wait()Init or the subreaper
Stages in the life of an orphan process

Orphan process versus zombie process

An orphan and a zombie are opposite situations that are often confused. An orphan is alive with a dead parent, while a zombie is dead with a living parent that has not yet called wait() to read its exit status. A zombie consumes no CPU time and holds no memory beyond its process table entry, but an orphan is a fully running process.

The two states interact in one important way: reparenting prevents permanent zombies. If an orphan exits, init or the subreaper reaps it promptly, so its entry never lingers. A zombie whose parent then dies is likewise adopted by init and immediately cleaned up.

PropertyOrphan processZombie process
Process stateRunning, sleeping or stoppedTerminated (defunct)
Parent statusOriginal parent has exitedParent is alive but has not reaped it
Resource useNormal CPU, memory and file useOnly a process table slot
Typical causeParent crash, deliberate daemonizationParent ignores SIGCHLD or never calls wait()
ResolutionRuns to completion, reaped by new parentParent reaps it or parent dies
Comparison of orphan and zombie processes

Intentional orphaning and daemons

Orphaning is the classic way to create a daemon on Unix. The traditional double-fork technique has the process fork, let the first child call setsid() to start a new session, fork again, and then let the intermediate process exit. The grandchild becomes an orphan adopted by init, detached from any controlling terminal, so closing the terminal session cannot kill it with SIGHUP.

Shells create similar orphans when a user runs a command with nohup or disown and then logs out. Service managers have changed this practice: systemd prefers services that stay in the foreground and lets the manager supervise them through control groups, which track every descendant even when processes are reparented.

  • Double fork guarantees the daemon cannot reacquire a controlling terminal.
  • nohup makes the command ignore SIGHUP so it survives the end of the session.
  • disown removes a job from the shell’s job table so the shell does not signal it.
  • Subreapers let supervisors keep track of grandchildren that would otherwise go to init.

How different platforms handle orphans

The concept is strongest in Unix-like systems, where the parent link is mandatory and enforced by the kernel. Windows records a parent process ID at creation time but does not reparent children or require parents to reap them, so a Windows child simply continues with a stale parent ID that may later be reused by an unrelated process.

PlatformAdopting parentNotable mechanism
LinuxInit (PID 1) or nearest subreaperprctl() with PR_SET_CHILD_SUBREAPER
macOSlaunchd (PID 1)launchd supervises and reaps adopted processes
FreeBSDInit or a reaper set with procctl()PROC_REAP_ACQUIRE for process reapers
WindowsNo reparentingJob objects can terminate all related processes
Linux containersThe container’s PID 1Requires a minimal init that reaps children
Orphan handling across operating systems

Containers deserve special attention. Inside a PID namespace the first process becomes the adopter of every orphan. If that process is an ordinary application that never calls wait(), exited orphans accumulate as zombies, which is why many images run a small init such as tini as PID 1.

Risks, detection and prevention

Unintended orphans are a common operational problem. A crashed supervisor, a killed build tool or a test runner that exits early can leave workers that keep running, holding network ports, file locks and memory. Because the orphan no longer has its original parent, nobody may notice until a restart fails with an address already in use.

Administrators detect orphans by looking for long-running processes whose parent ID is 1 or a subreaper but which are not legitimate services. Tools such as ps -ef, pstree and systemd-cgls show the tree, and control groups reveal which service originally launched a stray process.

SymptomLikely causeRecommended fix
Port still bound after restartWorker survived the crashed parentRun workers in one cgroup or job object
Zombies pile up in a containerPID 1 never reaps adopted childrenUse a minimal init as PID 1
Background job dies at logoutSIGHUP sent to the sessionUse nohup, setsid or a service unit
Children outlive a killed parentSignals sent only to the parentSignal the whole process group
CPU load from unknown processesForgotten orphaned helpersInspect the cgroup and terminate the tree
Common orphan problems and their fixes

On Linux a child can also request PR_SET_PDEATHSIG, which tells the kernel to send it a chosen signal when its parent dies. This is the most direct way to prevent accidental orphans in helper processes, although it must be combined with a check of getppid() to avoid a race at startup.

A plain-language summary

Every running program on a computer has a parent, the program that started it. An orphan process is a program that is still working after its parent has finished or crashed.

The operating system does not let such a program wander alone. It hands it to a built-in caretaker, usually the very first process on the system, which will tidy up after it when it finishes. Sometimes programmers create orphans on purpose so that a task keeps running in the background; sometimes they appear by accident and quietly waste resources until someone stops them.

An example from practice

A development team runs an API server that starts four worker processes to handle requests. During a deployment, an engineer stops the main server with kill -9, which gives it no chance to shut down its workers. The four workers become orphans adopted by PID 1 and keep listening on port 8080.

When the new version starts, it fails with an error saying the address is already in use. Running ps -ef shows four processes with a parent ID of 1 and the old binary path. The engineer terminates them, and the team then moves the server into a systemd service unit, which places every worker in the same control group.

From then on, stopping the service terminates the whole process tree, and deployments no longer leave orphaned workers holding the port.