Syntax
#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[ ]);Description
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: compact break=fit.
Letter
Note: In earlier releases of C Set ++, all of the exec functions began with an underscore (_getpid). 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, The Developer's Toolkit 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=valuewhere 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.
Returns
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: compact break=fit.
Value
This example calls four of the eight exec routines. When invoked without arguments, the program first runs the code for case PARENT. It then calls execle() to load and run a copy of itself. The instructions for the child are blocked to run only if argv[0] and one parameter were passed (case CHILD). In its turn, the child runs its own child as a copy of the same program. This sequence is continued until four generations of child processes have run. Each of the processes prints a message identifying itself.
#include <stdio.h>#include <stdlib.h> #include <process.h> #define PARENT 1 #define CHILD 2 char *args[3]; int main(int argc, char **argv, char **envp) { switch(argc) { case PARENT: { /* No argument: run a child */ printf("Parent process began.\n"); execle(argv[0],argv[0],"1",NULL,envp); abort(); /* Not executed because parent was overlaid. */ }
case CHILD: { /* One argument: run a child's child */ printf("Child process %s began.\n", argv[1]); if ('1' == *argv[1]) { /* generation one */ execl(argv[0], argv[0], "2", NULL); abort(); /* Not executed because child was overlaid */ } if('2' == *argv[1]) { /* generation two */ args[0] = argv[0]; args[1] = "3"; args[2] = NULL; execv(argv[0],args); abort(); /* Not executed because child was overlaid */ } if ('3' == *argv[1]) { /* generation three */ args[0] = argv[0]; args[1] = "4"; args[2] = NULL; execve(argv[0], args, _environ); abort(); /* Not executed because child was overlaid */ } if ('4' == *argv[1]) /* generation four */ printf("Child process %s", argv[1]); } } printf(" ended.\n"); return 55; /* The output should be similar to: Parent process began. Child process 1 began. Child process 2 began. Child process 3 began. Child process 4 began. Child process 4 ended. */ }Related Information