Under the Windows platform, we can run the executable program by double-clicking, let this executable program become a process, and on the Linux platform, we can pass ./ Run, let an executable program become a process.
However , if we are running a program (process), how do we start an external program inside the process, and the kernel reads the external program into memory so that it executes as a process? Here we implement through the EXEC function family.
The EXEC function family, as the name implies, is a cluster of functions, in Linux, there is no exec () function, exec refers to a set of functions, a total of 6:
#include <unistd.h>int execl (const char *path, const char *arg, ...); int EXECLP (const char *file, const char *arg, ...); int execle (const char *path, const char *arg, ..., char * const envp[]); int execv (const char *path, char *const argv[]); in T EXECVP (const char *file, char *const argv[]); int execve (const char *path, char *const argv[], char *const envp[]);
only Execve () is a real system call, and the others are packaged library functions on this basis.
The EXEC function family provides six ways to start another program in the process . the function of the EXEC function family is to find the executable file based on the specified file name or directory name, and use it to replace the contents of the calling process, in other words , execute an executable file inside the calling process.
When a process calls an EXEC function, the process is completely replaced by the new program, and the new program executes from its main function. Because calling exec does not create a new process, the process ID of the front and back (and of course the parent process number, the process group number, the current working directory ...). ) has not changed. exec simply replaces the body, data, heap, and stack (process substitution) of the current process with another new program.
The 6 functions of the EXEC function family seem to be complex, but in fact they are very similar in function and usage, with only a small difference.
L (list): The parameter address list, ending with a null pointer.
V (vector): The address of an array of pointers that hold the address of each parameter.
p (path): Searches the executable file by the directory specified by the PATH environment variable.
e (Environment): The address of an array of pointers containing the environment variable string addresses.
The EXEC function family loads and runs the executable program Path/file and passes the parameter arg0 (arg1, Arg2, argv[], envp[]) to the program.
The EXEC function family differs from the normal function in that the function of the EXEC function family does not return after successful execution, and thecode below the EXEC function family does not execute . only If the call fails, they return 1, and then execute from the original program's point of invocation after the failure.
EXECL () Sample code:
#include <stdio.h> #include <unistd.h>int main (int argc, char *argv[]) {printf ("before exec\n\n");/*/bin/ LS: External program, here is the LS executable of the/bin directory, must take the path (relative or absolute) ls: There is no point, if you need to send this external program, here must write the string, as for the string content arbitrary -a,-l,-h: to the external program LS Passed parameter NULL: This must be written on, represented to the external program ls parameter end */execl ("/bin/ls", "ls", "-a", "-L", "-H", NULL);//If the EXECL () execution succeeds, the following cannot be performed, Because the current process has been executed, LS has replaced the perror ("Execl");p rintf ("after exec\n\n"); return 0;}
The results of the operation are as follows:
EXECV () Sample code:
The usage of EXECV () and execl () is basically the same, instead of using a pointer array to pass the list to the parameter.
#include <stdio.h> #include <unistd.h>int main (int argc, char *argv[]) {//Execv () and execl () are used essentially the same way, simply by passing the list of arguments , instead of using an array of pointers//EXECL ("/bin/ls", "ls", "-a", "-L", "-H", NULL);/* pointer array ls: no sense, if you need to pass this external program, you must write the string, as the string content arbitrary -a,-l,-h: Parameter NULL for external program LS : This must be written on, represented to the external program ls parameter end */char *arg[]={"ls", "-a", "-L", "-H", null};///bin/ls: External program, here is/ The bin directory of the LS executable must be carried on the path (relative or absolute)//ARG: The pointer array defined above address Execv ("/bin/ls", Arg);p error ("Execv"); return 0;}
EXECLP () or EXECVP () sample code:
The difference between EXECLP () and Execl () is that the executable program specified by EXECLP () can be executed without a pathname and, without a pathname, the executable program is found in the directory specified by the environment variable path, and Execl () The specified executable program must be brought with the path name.
#include <stdio.h> #include <unistd.h>int main (int argc, char *argv[]) {//first parameter "ls", without path name, in environment variable path Find this executable program//other parameter usage and EXECL () as EXECLP ("ls", "ls", "-a", "-L", "-H", NULL);/*char *arg[]={"ls", "-a", "-L", "-H", Null};exe CVP ("LS", arg); */perror ("EXECLP"); return 0;}
Execle () or EXECVE () sample code:
Execle () and Execve () change the environment variables of the exec-initiated program (which only alters the environment variables of the process, does not affect the environment variables of the system ), and the other four function-initiated programs use the default system environment variables.
Execle () Sample code:
#include <stdio.h> #include <unistd.h> #include <stdlib.h>//getenv () int main (int argc, char *argv[]) { Getenv () Gets the value of the specified environment variable printf ("Before exec:user=%s, home=%s\n", getenv ("USER"), getenv ("HOME"));//Pointer data char *env[]={" User=mike "," home=/tmp ", null};/*./mike: An external program, the current path of the Mike program, compiled by GCC mike.c-o Mike: There is no meaning here NULL: give MIKE the program to end env: Change mi KE program environment variable, correctly, let Mike program only keep ENV environment variable */execle ("./mike", "Mike", NULL, env);/*char *arg[]={"Mike", Null};execve ("./mike" , ARG, env); */perror ("execle"); return 0;}
external Program, MIKE.C sample code:
#include <stdio.h> #include <stdlib.h> #include <unistd.h>int main (int argc, char *argv[]) {printf ("\ Nin The Mike Fun, after exec: \ n ");p rintf (" user=%s\n ", getenv (" USER "));p rintf (" home=%s\n ", getenv (" HOME "); return 0;}
The results of the operation are as follows:
For this tutorial sample code download please click here.
Linux system Programming-process substitution: Exec function family