1. function declaration:
# Include <unistd. h>
Int execl (const char * pathname, const char * arg0,.../* (char *) 0 */);
Int execv (const char * pathname, char * const argv []);
Int execle (const char * pathname, const char * arg0 ,...
/* (Char *) 0, char * const envp [] */);
Int execve (const char * pathname, char * const argv [], char * const envp []);
Int execlp (const char * filename, const char * arg0,.../* (char *) 0 */);
Int execvp (const char * filename, char * const argv []);
If an error occurs, the value-1 is returned. if the error is successful, the value is not returned.
The first difference between these functions is that the first four take the path name as the parameter, and the last two take the file name as the parameter. When the filename is specified as the parameter:
• If filename contains/, it is regarded as the path name.
• Otherwise, search for executable files in related directories Based on the path environment variable.
The second difference is related to the parameter table transmission (L indicates the table (list), V indicates the vector (vector )). the execl, execlp, and execle functions require that each command line parameter of the new program be described as a separate parameter. this parameter table ends with a null pointer.
For the other three functions execv, execvp and execve, we should first construct a pointer array pointing to each parameter, and then use the array address as the parameters of the three functions.
2. Among the six functions, only execve is called by the system, and other functions are just library functions. Different implementations still need to call execve.
All three functions of execl are called by constructing argv to call the three different functions of execv, while execvp tries to call execv with the path prefix, and execv calls the execve system call using the environment variable.
3. the exec function is mainly used for subprocess calling after fork to implement multi-process operations.
# Include <sys/types. h>
# Include <sys/Wait. H>
Char * env [] = {"user = unknown", "Path =/tmp", null };
Int main ()
{
Pid_t PID;
If (pid = fork () <0)
/* Error handler */
Else if (pid = 0)
Execle...
If (waitpid (PID, null, 0) <0)
/* Error handler */
If (pid = fork () <0)
/* Error handler */
Else if (pid = 0)
Execlp...
Return 0;
}
In this program, the parameters required by execle are a program path and a custom specific environment;
The parameter required by execlp is a file name and receives system environment variables. The call here inherits the Environment added by execle.
4. Exec considerations:
The last command line parameter must be followed by a null pointer. If it is 0, it must be forcibly converted to (char *). Otherwise, an error will be reported.
5. Easy to remember:
To make these six functions easy to remember, the following describes the characters in the function name:
L: This function obtains a parameter table, which is mutually exclusive with v.
V: returns an argv [] vector.
E: indicates that the function obtains an envp [] array without using the current environment variable.
P: This function takes filename as the parameter and uses the PATH environment variable to find the executable file.