Exec family 1.exe C has six functions:
(1) int execl (const char * path, const char * Arg ,......);
(2) int execle (const char * path, const char * Arg,..., char * const envp []);
(3) int execv (const char * path, char * const argv []);
(4) int execve (const char * filename, char * const argv [], char * const envp []);
(5) int execvp (const char * file, char * const argv []);
(6) int execlp (const char * file, const char * Arg ,......);
Among them, only execve is a real system call, and others are packaged library functions on this basis.
The exec function family finds an executable file based on the specified file name and uses it to replace the content of the calling process. In other words, it executes an executable file within the calling process. The executable file can be either a binary file or any script file that can be executed in Linux.
Unlike in general, functions in the exec function family are not returned after successful execution, because the entity of the calling process, including the code segment, data segment, and stack, has been replaced by new content, only some superficial information such as the process id remains unchanged, which is quite similar to the "golden shell" in the "Plan ". It looks like an old shell, but it has injected a new soul. Only when the call fails will they return a-1, which will be executed from the original program's call point.
2. Differences between them:
The first difference is:
The first four take the path name as the parameter, and the last two take the file name as the parameter. If the file name does not contain "/", search for the executable file from the PATH environment variable, if an executable file is found, but it is not an executable code file generated by the editing program, it is processed as a shell script.
The second difference:
The first two and last functions both contain the letter "L", and the other three contain the letter "V". "L" indicates list, that is, table, "V" represents vector, which is also given in the form of list for the parameters of the first three functions. However, a null pointer must be added at last, if the constant 0 is used to represent a null pointer, it must be forcibly converted to a character pointer; otherwise, an error may occur ., The other three are given in the form of vectors, that is, arrays.
Last difference:
It is related to passing environment variables to the new program. For example, if the second and fourth functions end with E, you can pass a pointer to the Environment string pointer array to the function. Each environment variable is defined, while the other four are environment variables in the process.
3. Examples:
(1) In normal programming, if the exec function family is used, you must add an error judgment statement. First, judge the return value of execl. If an error occurs, use the perror () function to print the error message.
For example, if (execl ("path", ".." (char *) 0) <0)
{
Perror ("execl Error !");
}
If an error occurs in the call, you can output: execl error! : The error cause allows you to easily find the cause of the error.
(2) Pay attention to the following writing format:
First define a pointer array: char * argv [] = {"ls", "-l", (char *) 0}
Use execv to call ls: execv ("/bin/ls", argv)
If you use execvp
Execvp ("ls", argv) // write ls directly.
Note:
Execl specifies the shell version used in the shell script when calling shell :#! /Bin/bash. Execute the shell script in the command line. The system automatically opens a shell for it. If there is no shell in the program, an error occurs when calling the shell script. Therefore, you must first open the shell in the shell script.
Reference:Http://blog.csdn.net/yuwenliang/article/details/5220650
(1) int execl (const char * path, const char * Arg ,......);
(2) int execle (const char * path, const char * Arg,..., char * const envp []);
(3) int execv (const char * path, char * const argv []);
(4) int execve (const char * filename, char * const argv [], char * const envp []);
(5) int execvp (const char * file, char * const argv []);
(6) int execlp (const char * file, const char * Arg ,......);
Among them, only execve is a real system call, and others are packaged library functions on this basis.
The exec function family finds an executable file based on the specified file name and uses it to replace the content of the calling process. In other words, it executes an executable file within the calling process. The executable file can be either a binary file or any script file that can be executed in Linux.
Unlike in general, functions in the exec function family are not returned after successful execution, because the entity of the calling process, including the code segment, data segment, and stack, has been replaced by new content, only some superficial information such as the process id remains unchanged, which is quite similar to the "golden shell" in the "Plan ". It looks like an old shell, but it has injected a new soul. Only when the call fails will they return a-1, which will be executed from the original program's call point.
2. Differences between them:
The first difference is:
The first four take the path name as the parameter, and the last two take the file name as the parameter. If the file name does not contain "/", search for the executable file from the PATH environment variable, if an executable file is found, but it is not an executable code file generated by the editing program, it is processed as a shell script.
The second difference:
The first two and last functions both contain the letter "L", and the other three contain the letter "V". "L" indicates list, that is, table, "V" represents vector, which is also given in the form of list for the parameters of the first three functions. However, a null pointer must be added at last, if the constant 0 is used to represent a null pointer, it must be forcibly converted to a character pointer; otherwise, an error may occur ., The other three are given in the form of vectors, that is, arrays.
Last difference:
It is related to passing environment variables to the new program. For example, if the second and fourth functions end with E, you can pass a pointer to the Environment string pointer array to the function. Each environment variable is defined, while the other four are environment variables in the process.
3. Examples:
(1) In normal programming, if the exec function family is used, you must add an error judgment statement. First, judge the return value of execl. If an error occurs, use the perror () function to print the error message.
For example, if (execl ("path", ".." (char *) 0) <0)
{
Perror ("execl Error !");
}
If an error occurs in the call, you can output: execl error! : The error cause allows you to easily find the cause of the error.
(2) Pay attention to the following writing format:
First define a pointer array: char * argv [] = {"ls", "-l", (char *) 0}
Use execv to call ls: execv ("/bin/ls", argv)
If you use execvp
Execvp ("ls", argv) // write ls directly.
Note:
Execl specifies the shell version used in the shell script when calling shell :#! /Bin/bash. Execute the shell script in the command line. The system automatically opens a shell for it. If there is no shell in the program, an error occurs when calling the shell script. Therefore, you must first open the shell in the shell script.
Reference:Http://blog.csdn.net/yuwenliang/article/details/5220650