C Programming/POSIX Reference/unistd.h/exec

The exec collection of functions of Unix-like operating systems cause the running process to be completely replaced by the program passed as an argument to the function. As a new process is not created, the process identifier (PID) does not change, but the data, heap and stack of the original process are replaced by those of the new process.

In the execl, execlp, execv, and execvp calls, the new process image inherits the current environment variables.

Files open when an exec call is made remain open in the new process. This aspect is used to specify the standard streams (stdin, stdout and stderr) of the new process.

In MS-DOS environments, a program executed with one of the exec functions is always loaded into memory as if the "maximum allocation" in the program's executable file header is set to default value 0xFFFF. The EXEHDR utility can be used to change the maximum allocation field of a program. However, if this is done and the program is invoked with one of the exec functions, the program might behave differently from a program invoked directly from the operating-system command line or with one of the spawn functions.

Many Unix shells also offer an exec built-in command that replaces the shell process with the specified program. Wrapper scripts often use this command to run a program (either directly or through an interpreter or virtual machine) after setting environment variables or other configuration. By using exec, the resources used by the shell program do not need to stay in use after the program is started.

Prototypes
The functions are declared in the unistd.h header for the POSIX standard and in process.h for DOS, OS/2, and Windows.



Some implementations provide these functions named with a leading underscore (e.g. _execl).

Function names
The base of each function is exec, followed by one or more letters:
 * e - An array of pointers to environment variables is explicitly passed to the new process image.
 * l - Command-line arguments are passed individually to the function.
 * p - Uses the PATH environment variable to find the file named in the path argument to be executed.
 * v - Command-line arguments are passed to the function as an array of pointers.

path
The path argument specifies the path name of the file to execute as the new process image. Arguments beginning at arg0 are pointers to arguments to be passed to the new process image. The argv value is an array of pointers to arguments.

arg0
The first argument arg0 should be the name of the executable file. Usually it is the same value as the path argument. Some programs may incorrectly rely on this argument providing the location of the executable, but there is no guarantee of this nor is it standardized across platforms.

envp
Argument envp is an array of pointers to environment settings. The exec calls named ending with an e alter the environment for the new process image by passing a list of environment settings through the envp argument. This argument is an  array  of  pointers  to  null-terminated strings. The envp array must be terminated by a NULL pointer.

Each null-terminated string has the form:

where name is the environment variable name, and value is the value of that that variable. The final element of the envp array must be null. If envp itself is null, the new process inherits the current environment settings.

Return value
Normally the exec function will replace the current process, so it cannot return anything to the original process. Processes do have an exit status, but that value is collected by the parent process.

If an exec function does return to the calling process, an error occurred, the return value is −1, and errno is set to one of the following values: