(1) exec Function Description
The fork function is used to create a child process, which is almost a copy of the parent process. Sometimes we want the child process to execute another program, the exec function family provides a method to start execution of another program in the process. It can locate the executable file based on the specified file name or directory name and use it to replace the data segment, code segment, and stack segment of the original calling process. After the execution, all the content of the original calling process except the process number is replaced by the content of the new program. In addition, the executable file can be either a binary file or any executable script file in Linux.
(2) in Linux, there are two main scenarios for using the exec function family:
When a process deems itself unable to make any contribution to the system and the user, it can call any exec function family to regenerate itself.
If a process wants to execute another program, it can call the fork function to create a new process, and then call any exec function to regenerate the child process.
(3) exec function family syntax
In fact, in Linux, there are no exec functions, but six Function Families starting with exec. The following table lists the syntax of the six member functions of the exec function family.
Header files |
# Include <unistd. h> |
Function Description |
Execution File |
Function prototype |
Int execl (const char * path, const char * Arg ,...) |
Int execv (const char * path, char * const argv []) |
Int execle (const char * path, const char * Arg,..., char * const envp []) |
Int execve (const char * path, char * const argv [], char * const envp []) |
Int execlp (const char * file, const char * Arg ,...) |
Int execvp (const char * file, char * const argv []) |
Function return value |
Success: The function will not return |
Error:-1 is returned. The cause of failure is recorded in error. |
These six functions have minor differences in terms of function names and syntax-based rules. The following describes the executable file search methods, parameter table transmission methods, and environment variables.
① Search method: in the preceding table, the first four functions are in the complete file directory path. The last two functions (two functions ending with P) can only provide the file name, the system automatically searches for the path specified by the environment variable "$ path.
② Parameter passing method: the parameter passing method of the exec function family can be either listed one by one, the other is to construct all parameters into a pointer array for transmission.
Here, the parameter passing method is distinguished by the 5th-bit letters of the function name. the letter "L" (list) represents the list one by one, and the letter is "v" (vertor) all parameters are constructed into a pointer array for transmission, and the first address of the array is passed as a parameter. The last pointer in the array must be null. Readers can observe the differences between the syntax of execl, execle, and execlp and that of execv, execve, and execvp.
③ Environment variable: the exec function family uses the system's default environment variable. You can also input the specified environment variable. Here, the two functions execle and execve ending with "E" (Environment) can specify in envp [] that the environment variables used by the current process Replace the environment variables inherited by the process.
(3) Description of Path Environment Variables
The path environment variable contains a directory table. The system searches for the Execution code using the path defined by the PATH environment variable. When the PATH environment variable is defined, directories must be separated by ":" and separated by ". "indicates the end. The path environment variable is defined in the user's. profile or. in bash_profile, the following is an example of the PATH environment variable definition. This path variable specifies the search Execution Code in the "/bin", "/usr/bin", and the current directory.
Path =/bin:/usr/bin :.
Export $ path
(4) Description of environment variables in processes
In Linux, shell processes are the parent processes of all execution codes. When an Execution code is executed, the shell process Fork sub-processes and then calls the exec function to execute the Execution Code. The shell process stack stores all the environment variables of the user. When the execl, execv, execlp, and execvp functions are used to regenerate the Execution Code, the shell process copies all environment variables to the generated new process. When execle and execve are used, the new process does not inherit the environment variables of any shell process, the envp [] array sets the environment variables by itself.
(5) exec Function Family Relationship
4th bits |
Unified: Exec |
5th bits |
L: the parameters are passed in the enumeration mode one by one. |
Execl, execle, execlp |
V: The parameter is passed to construct a pointer array. |
Execv, execve, execvp |
6th bits |
E: New process environment variables can be passed |
Execle, execve |
P: the executable file search method is file name. |
Execlp, execvp |
In fact, only execve is really called by the six functions, and the other five are database functions. They will eventually call the execve system call, as shown in the call relationship between 12-11:
Figure 12-11 exec function family Diagram
(6)exec
Call example:
Char * const ps_argv [] = {"Ps", "-o", "PID, ppid, pgrp, session, tpgid, comm", null };
Char * const ps_envp [] = {"Path =/bin:/usr/bin", "term = console", null };
Execl ("/bin/PS", "Ps", "-o", "PID, ppid, pgrp, session, tpgid, comm", null );
Execv ("/bin/PS", ps_argv );
Execle ("/bin/PS", "Ps", "-o", "PID, ppid, pgrp, session, tpgid, comm", null, ps_envp );
Execve ("/bin/PS", ps_argv, ps_envp );
Execlp ("Ps", "Ps", "-o", "PID, ppid, pgrp, session, tpgid, comm", null );
Execvp ("Ps", ps_argv );
Note the first two parameters when the exec function family parameters are expanded. The first parameter is an Execution Code with a path. (the first parameter of the execlp and execvp functions is non-path, the system will automatically find and synthesize the Execution Code with the path according to the path), the second is the Execution Code without the path, the Execution Code can be a binary Execution Code and shell script.
(7) Notes for using the exec function family
When using the exec function family, you must add an error judgment statement. Because exec is easy to fail, the most common causes are:
① The file or path cannot be found, and errno is set to enoent.
② Array argv and envp forget to end with null. errno is set to efault.
③ There is no operation permission for the executable file, and errno is set to eacces.
(8) after exec, the new process maintains the following features of the original process:
Environment variables (if execle and execve functions are used, the environment variables are not inherited );
Process ID and parent process ID;
Actual user ID and actual group ID;
Additional group ID;
Process Group ID;
Session ID;
Control terminal;
Current working directory;
Root directory;
File Permission shielding characters;
File lock;
Process signal shielding;
Pending signals;
Resource restrictions;
Tms_utime, tms_stime, tms_cutime, and tms_ustime values.
The process of opening a file is related to the exec close flag value of each descriptor. Each file descriptor in a process has an exec close flag (fd_cloexec). If this flag is set, disable the descriptor when executing exec. Otherwise, the descriptor is still open. Unless this flag is specifically set with fcntl, the default operation of the system is to keep this descriptor open after exec, which can be used to achieve I/O redirection.
(9) execlp function example
Execlp. C source code is as follows:
# Include <stdio. h>
# Include <unistd. h>
Int main ()
{
If (Fork () = 0 ){
If (execlp ("/usr/bin/ENV", "env", null) <0)
{
Perror ("execlp error! ");
Return-1;
}
}
Return 0;
}
Compile GCC execlp. C-o execlp.
Execute./execlp. The execution result is as follows:
Home =/home/test
Db2db = test
Shell =/bin/bash
......
The execution results show that the execlp function inherits all the environment variables of the shell process when the Execution Code is reborn. The other three functions do not end with E.
(10) execle function example
Execle is used to add environment variables to the newly created sub-process.
Execle. C source code is as follows:
# Include <unistd. h>
# Include <stdio. h>
Int main ()
{
/* Command parameter list, which must end with null */
Char * envp [] = {"Path =/tmp", "user = sun", null };
If (Fork () = 0 ){
/* Call the execle function. Note that the complete env path must be specified here */
If (execle ("/usr/bin/ENV", "env", null, envp) <0)
{
Perror ("execle error! ");
Return-1;
}
}
Return 0;
}
Compile: GCC execle. C-o execle.
Execute./execle. The execution result is as follows:
Path =/tmp
User = sun
It can be seen that execle and execve can pass environment variables to the execution process by themselves, but do not inherit the environment variables of the shell process, while the other four exec functions inherit all the environment variables of the shell process.
Excerpt from Linux tools and Programming