execl - _execvpe -- Load and Run Child Process

Format

#include <process.h>
int execl(char *pathname, char *arg0, char *arg1,...,
             char *argn, NULL);
int execle(char *pathname, char *arg0, char *arg1,...,
             char *argn, NULL, char *envp[ ]);
int execlp(char *pathname, char *arg0, char *arg1,...,
             char *argn, NULL);
int _execlpe(char *pathname, char *arg0, char *arg1,...,
             char *argn, NULL, char *envp[ ]);
int execv(char *pathname, char *argv[ ]);
int execve(char *pathname, char *argv[ ],char *envp[ ]);
int execvp(char *pathname, char *argv[ ]);
int _execvpe(char *pathname, char *argv[ ], char *envp[ ]);

Language Level: XPG4 (except _execlpe and _execvpe), Extension
The exec functions load and run new child processes. The parent process is ended after the child process has started. Sufficient storage must be available for loading and running the child process.

All of the exec functions are versions of the same routine; the letters at the end determine the specific variation:

Letter Variation
p Uses PATH environment variable to find the file to be run.
l Passes a list of command line arguments separately.
v Passes to the child process an array of pointers to command-line arguments.
e Passes to the child process an array of pointers to environment strings.

Note: In earlier releases of the C/C++ run-time library, all of the exec functions began with an underscore (_execl). Because they are defined by the X/Open standard, the underscore has been removed. _execlpe and _execvpe retain the initial underscore because they are not included in the X/Open standard. For compatibility, IBM C and C++ Compilers will map the _exec functions to the correct exec function.

The pathname argument specifies the file to run as the child process. The pathname can specify a full path from the root, a partial path from the current working directory, or a file name. If pathname does not have a file name extension or does not end with a period, the exec functions will add the .EXE extension and search for the file. If pathname has an extension, the exec function uses only that extension. If pathname ends with a period, the exec functions search for pathname with no extension. The execlp, _execlpe, execvp, and _execvpe functions search for the pathname in the directories that the PATH environment variable specifies.

You pass arguments to the new process by giving one or more pointers to character strings as arguments in the exec call. These character strings form the argument list for the child process.

The compiler can pass the argument pointers as separate arguments (execl, execle, execlp, and _execlpe) or as an array of pointers (execv, execve, execvp, and _execvpe). You should pass at least one argument, either arg0 or argv[0], to the child process. If you do not, an argument will be returned that points to the same file as the path name argument you specified. This argument may not be exactly identical to the path name argument you specified. A different value does not produce an error.

Use the execl, execle, execlp, and _execlpe functions for the cases where you know the number of arguments in advance. The arg0 argument is usually a pointer to pathname. The arg1 through argn arguments are pointers to the character strings forming the new argument list. There must be a NULL pointer following argn to mark the end of the argument list.

Use the execv, execve, execvp, and _execvpe functions when the number of arguments to the new process is variable. Pass pointers to the arguments of these functions as an array, argv[ ]. The argv[0] argument is usually a pointer to pathname. The argv[1] through argv[n] arguments are pointers to the character strings forming the new argument list. If argv[n] is the last parameter, then argv[n+1] must be NULL.

Files that are open when you make an exec call remain open in the new process. In the execl, execlp, execv, and execvp calls, the child process receives the environment of the parent. The execle, _execlpe, execve, and _execvpe functions let you change the environment for the child process by passing a list of environment settings through the envp argument. The envp argument is an array of character pointers, each element of which points to a string ending with a null character that defines an environment variable. Such a string usually has the following form:

   NAME=value

where NAME is the name of an environment variable, and value is the string value to which the exec function sets that variable.

Note: Do not enclose the value in double quotation marks.

The final element of the envp array should be NULL. When envp itself is NULL, the child process receives the environment settings of the parent process.

The exec functions do not preserve signal settings in child processes created by calls to exec functions. Calls to exec functions reset the signal settings to the default in the child process.

Return Value
The exec functions do not normally return control to the calling process. They are equivalent to the corresponding _spawn functions with P_OVERLAY as the value of modeflag. If an error occurs, the exec functions return -1 and set errno to one of the following values:

Value Meaning
EACCESS The specified file has a locking or sharing violation.
EMFILE There are too many open files. The system must open the specified file to tell whether it is an executable file.
ENOENT The file or pathname was not found or was specified incorrectly.
ENOEXEC The specified file cannot run or has an incorrect executable file format.
ENOMEM One of the following conditions exists:
  • Not enough storage is available to run the child process.
  • Not enough storage is available for the argument or environment strings.

Example



abort -- Stop a Program
_cwait -- Wait for Child Process
exit -- End Program
_exit -- End Process
_spawnl - _spawnvpe -- Start and Run Child Processes
system -- Invoke the Command Processor
<process.h>