Running new code in the process
The EXECX series function can run a new program in the current child process. When a process invokes any function in the series, the process's user-space resources are completely replaced by the new program.
The difference between these functions: Indicates whether the location of the new program uses a path or a file name, and if the file name searches for the program in the path described by the system's $PATH environment variable.
How to use the parameter list or use the argv[] array when using parameters
functions &NBSP ; |
using the filename |
using the pathname |
using the Parameter table (function appears letter L) |
using argv (function appears letter V) |
execl |
|
√ |
√ |
|
execlp |
√ |
|
Yes |
|
execle |
|
√ |
√ |
|
execv |
|
√ |
|
√ |
execvp |
√ |
|
|
√ |
execve |
|
√ |
|
√ |
int execl (__const char *__path, __const char * __arg, ...): Executes the program pointed to by the path string, followed by a parameter list. Finally, you must pass in a null pointer .
#include <unistd.h>#include<stdio.h>#include<sys/types.h>intMainintargcChar*argv[]) {pid_t pid; if(PID = fork ()) <0) printf ("error\n"); Else if(PID = =0) {execl ("/bin/ls","ls","- L"," /Home",(Char*)0);//child Process Execution new function } Elseprintf ("Father ok!\n"); return 0;}
int execle (__const char * __path, __const char *__arg, ...): The last parameter must point to a new array of environment variables , the environment variable for the new execution program.
#include <unistd.h>intMainintargcChar*argv[],Char*env[]) {Execl ("/bin/ls","ls","- L"," /Home",(Char *)0 ,Env);//child Process Execution new function return 0;}
int EXECLP (__const char * __file, __const char * __arg, ...): Finds the file in the $PATH environment variable and executes, the last argument must be null with a null pointer
#include <unistd.h>intMainintargcChar*argv[],Char*env[]) {EXECLP ("ls","ls","- L"," /Home",(Char*)0);//child Process Execution new function return 0;}
int Execv (__const char * __path, char * __const __argv[]): The second parameter is a list of program parameters maintained by the array pointer. the last member of the array must be null.
#include <unistd.h>intMain () {Char* argv[] = {"ls","- L"," /Home",(Char*)0}; EXECV ("/bin/ls", argv);//child Process Execution new function return 0;}
int EXECVP (__const char * file, char * __const __argv[]): Using the file name, the last member of the second argument must be null.
#include <unistd.h>int main () { char * argv[] = { " ls , " -L , " /home , (char *) 0 }; EXECVP ( ls , argv); // child process Execute new function return 0 ;}
int System (__const char * __command): Creates a new process and runs until the new process finishes running before the parent process continues.
#include <stdlib.h>#include<sys/wait.h>#include<stdio.h>intMainintargcChar*argv[]) { intstatus; Status= System ("pwd"); if(!wifexited (status)) printf ("Abnormal exit\n"); Elseprintf ("The exit status is%d\n", status); return 0;}
Linux Advanced Programming (chapter eighth) process management and program development 2