Skip to content

Child process exit status

"exit status"指的是child process的exit status,通过"retrieve this value using the wait system call".

wikipedia Exit status

The exit status of a process in computer programming is a small number passed from a child process (or callee) to a parent process (or caller) when it has finished executing a specific procedure or delegated task. In DOS, this may be referred to as an errorlevel.

NOTE: parent process如何获取child process的exit status呢?参见下面的POSIX这段,其中提及了the wait system call

When computer programs are executed, the operating system creates an abstract entity called a process in which the book-keeping for that program is maintained. In multitasking operating systems such as Unix or Linux, new processes can be created by active processes. The process that spawns another is called a parent process, while those created are child processes. Child processes run concurrently with the parent process. The technique of spawning child processes is used to delegate some work to a child process when there is no reason to stop the execution of the parent. When the child finishes executing, it exits by calling the exit system call. This system call facilitates passing the exit status code back to the parent, which can retrieve this value using the wait system call.

NOTE:

通过 wait system call 来 fetch exit status。

Semantics

The parent and the child can have an understanding about the meaning of the exit statuses. For example, it is common programming practice for a child process to return (exit with) zero to the parent signifying success. Apart from this return value from the child, other information like how the process exited, either normally or by a signal may also be available to the parent process.

NOTE: other information如何传递到parent process呢?

The specific set of codes returned is unique to the program that sets it. Typically it indicates success or failure. The value of the code returned by the function or program may indicate a specific cause of failure. On many systems, the higher the value, the more severe the cause of the error.[1] Alternatively, each bit may indicate a different condition, which are then evaluated by the or operator together to give the final value; for example, fsck does this.

Sometimes, if the codes are designed with this purpose in mind, they can be used directly as a branch index upon return to the initiating program to avoid additional tests.

Shell and scripts

Shell scripts typically execute commands and capture their exit statuses.

For the shell’s purposes, a command which exits with a zero exit status has succeeded. A nonzero exit status indicates failure.

C language

The C programming language allows programs exiting or returning from the main function to signal success or failure by returning an integer, or returning the macros EXIT_SUCCESS and EXIT_FAILURE. On Unix-like systems these are equal to 0 and 1 respectively.[3] A C program may also use the exit() function specifying the integer status or exit macro as the first parameter.

The return value from main is passed to the exit function, which for values zero, EXIT_SUCCESS or EXIT_FAILURE may translate it to “an implementation defined form” of successful termination or unsuccessful termination.

Apart from zero and the macros EXIT_SUCCESS and EXIT_FAILURE, the C standard does not define the meaning of return codes. Rules for the use of return codes vary on different platforms (see the platform-specific sections).

POSIX

In Unix and other POSIX-compatible systems, the parent process can retrieve the exit status of a child process using the wait() family of system calls defined in wait.h. [6]. Of these, the waitid() [7] call retrieves the full 32-bit exit status, but the older wait() and waitpid() [8] calls retrieve only the least significant 8 bits of the exit status.

The wait() and waitpid() interfaces set a status value of type int packed as a bitfield with various types of child termination information. If the child terminated by exiting (as determined by the WIFEXITED() macro; the usual alternative being that it died from an uncaught signal), SUS specifies that the low-order 8 bits of the exit status can be retrieved from the status value using the WEXITSTATUS() macro.

Convention

POSIX-compatible systems typically use a convention of zero for success and nonzero for error.[10] Some conventions have developed as to the relative meanings of various error codes; for example GNU recommend that codes with the high bit set be reserved for serious errors,[3].