Waitpid()
   HOME

TheInfoList



OR:

In
computer A computer is a machine that can be Computer programming, programmed to automatically Execution (computing), carry out sequences of arithmetic or logical operations (''computation''). Modern digital electronic computers can perform generic set ...
operating system An operating system (OS) is system software that manages computer hardware and software resources, and provides common daemon (computing), services for computer programs. Time-sharing operating systems scheduler (computing), schedule tasks for ...
s, a
process A process is a series or set of activities that interact to produce a result; it may occur once-only or be recurrent or periodic. Things called a process include: Business and management * Business process, activities that produce a specific s ...
(or task) may wait for another process to complete its execution. In most systems, a
parent process In computing, a parent process is a process that has created one or more child processes. Unix-like systems In Unix-like operating systems, every process except (the swapper) is created when another process executes the fork() system call. T ...
can create an independently executing
child process A child process (CP) in computing is a process created by another process (the parent process). This technique pertains to multitasking operating systems, and is sometimes called a subprocess or traditionally a subtask. There are two major proce ...
. The parent process may then issue a ''wait''
system call In computing, a system call (syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, accessing a hard disk drive ...
, which suspends the execution of the parent process while the child executes. When the child process terminates, it returns an
exit status In computing, the exit status (also exit code or exit value) of a terminated process is an integer number that is made available to its parent process (or caller). In DOS, this may be referred to as an errorlevel. When computer programs ar ...
to the operating system, which is then returned to the waiting parent process. The parent process then resumes execution. Modern operating systems also provide system calls that allow a process's thread to create other threads and wait for them to terminate ("
join Join may refer to: * Join (law), to include additional counts or additional defendants on an indictment *In mathematics: ** Join (mathematics), a least upper bound of sets orders in lattice theory ** Join (topology), an operation combining two topo ...
" them) in a similar fashion. An operating system may provide variations of the ''wait'' call that allow a process to wait for any of its child processes to
exit Exit(s) may refer to: Architecture and engineering * Door * Portal (architecture), an opening in the walls of a structure * Emergency exit * Overwing exit, a type of emergency exit on an airplane * Exit ramp, a feature of a road interchange A ...
, or to wait for a single specific child process (identified by its process ID) to exit. Some operating systems issue a
signal A signal is both the process and the result of transmission of data over some media accomplished by embedding some variation. Signals are important in multiple subject fields including signal processing, information theory and biology. In ...
( SIGCHLD) to the parent process when a child process terminates, notifying the parent process and allowing it to retrieve the child process's exit status. The
exit status In computing, the exit status (also exit code or exit value) of a terminated process is an integer number that is made available to its parent process (or caller). In DOS, this may be referred to as an errorlevel. When computer programs ar ...
returned by a child process typically indicates whether the process terminated normally or abnormally. For normal termination, this status also includes the exit code (usually an integer value) that the process returned to the system. During the first 20 years of UNIX, only the low 8 bits of the exit code were available to the waiting parent. In 1989 with SVR4, a new call ''waitid'' was introduced that returns all bits from the ''exit'' call in a structure called ''siginfo_t'' in the structure member ''si_status''. Waitid has been a mandatory part of the POSIX standard since 2001.


Zombies and orphans

When a child process terminates, it becomes a ''
zombie process On Unix and Unix-like computer operating systems, a zombie process or defunct process is a process that has completed execution (via the exit system call) but still has an entry in the process table: it is a process in the " terminated stat ...
,'' and continues to exist as an entry in the system
process table In computing, a process is the instance of a computer program that is being executed by one or many threads. There are many different process models, some of which are light weight, but almost all processes (even entire virtual machines) are ro ...
even though it is no longer an actively executing program. Under normal operation it will typically be immediately waited on by its parent, and then reaped by the system, reclaiming the resource (the process table entry). If a child is not waited on by its parent, it continues to consume this resource indefinitely, and thus is a
resource leak In computer science, a resource leak is a particular type of resource consumption by a computer program where the program does not release resources it has acquired. This condition is normally the result of a bug in a program. Typical resource lea ...
. Such situations are typically handled with a special "reaper" process that locates zombies and retrieves their exit status, allowing the operating system to then deallocate their resources. Conversely, a child process whose parent process terminates before it does becomes an ''
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 an implementation-defined s ...
''. Such situations are typically handled with a special "root" (or "init") process, which is assigned as the new parent of a process when its parent process exits. This special process detects when an orphan process terminates and then retrieves its exit status, allowing the system to deallocate the terminated child process. If a child process receives a signal, a waiting parent will then continue execution leaving an orphan process behind. Hence it is sometimes needed to check the argument set by wait, waitpid or waitid and, in the case that WIFSIGNALED is true, wait for the child process again to deallocate resources.


See also

*
exit (system call) On many computer operating systems, a computer process terminates its execution by making an exit system call. More generally, an exit in a multithreading environment means that a thread of execution has stopped running. For resource man ...
*
fork (system call) In computing, particularly in the context of the Unix operating system and Unix-like, its workalikes, fork is an operation whereby a Computer process, process creates a copy of itself. It is an interface which is required for compliance with the P ...
*
Spawn (computing) Spawn in computing refers to a function that loads and executes a new child process. The current process may wait for the child to terminate or may continue to execute concurrent computing. Creating a new subprocess requires enough memory in whic ...
*
Wait (command) In Unix shells, wait is a command which pauses until execution of a background process has ended. Usage wait ... where n... is a list of pids or job IDs of a currently executing background process (job). If no ids are provided, the comma ...


References

{{DEFAULTSORT:Wait (System Call) Process (computing) C POSIX library System calls