Orphan process


An orphan process is a computer process whose parent process has finished or terminated, though it remains running itself.

Unix-like

In a Unix-like operating system any orphaned process will be immediately adopted by the special init system process: the kernel sets the parent to init. This operation is called re-parenting and occurs automatically. Even though technically the process has the "init" process as its parent, it is still called an orphan process since the process that originally created it no longer exists. In other systems orphaned processes are immediately terminated by the kernel. In modern Linux systems, an orphan process may be reparented to a "subreaper" process instead of init.
A process can be orphaned unintentionally, such as when the parent process terminates or crashes. The process group mechanism in most Unix-like operating systems can be used to help protect against accidental orphaning, where in coordination with the user's shell will try to terminate all the child processes with the "hangup" signal, rather than letting them continue to run as orphans. More precisely, as part of job control, when the shell exits, because it is the "session leader", the corresponding login session ends, and the shell sends SIGHUP to all its jobs.
It is sometimes desirable to intentionally orphan a process, usually to allow a long-running job to complete without further user attention, or to start an indefinitely running service or agent; such processes are known as daemons, particularly if they are indefinitely running. A low-level approach is to fork twice, running the desired process in the grandchild, and immediately terminating the child. The grandchild process is now orphaned, and is not adopted by its grandparent, but rather by init. Higher-level alternatives circumvent the shell's hangup handling, either telling the child process to ignore SIGHUP, or removing the job from the job table or telling the shell to not send SIGHUP on session end. In any event, the session id does not change, and the process id of the session that has ended is still in use until all orphaned processes either terminate or change session id.
To simplify system administration, it is often desirable to use a service wrapper so that processes not designed to be used as services respond correctly to system signals. An alternative to keep processes running without orphaning them is to use a terminal multiplexer and run the processes in a detached session, so the session is not terminated and the process is not orphaned.
A server process is also said to be orphaned when the client that initiated the request unexpectedly crashes after making the request while leaving the server process running.
These orphaned processes waste server resources and can potentially leave a server starved for resources. However, there are several solutions to the orphan process problem:
  1. Extermination is the most commonly used technique; in this case the orphan is killed.
  2. Reincarnation is a technique in which machines periodically try to locate the parents of any remote computations; at which point orphaned processes are killed.
  3. Expiration is a technique where each process is allotted a certain amount of time to finish before being killed. If need be a process may "ask" for more time to finish before the allotted time expires.