linux: process substitution

More knowledge points: linux directory index


concept

Process replacement refers to replacing the current process with a new program. The replacement process uses the exec family of functions. After the process is replaced by exec, the running program begins to execute the code in the new executable program specified by exec. The PID and PPID of the new process are exactly the same as the original process. Moreover, exec generally does not return, because the original process has been completely replaced, unless an error occurs, when an error occurs, exec returns -1.

The principle of process replacement

Fork creates a child process, and the child process copies all the contents of the parent process PCB, including the code segment and the data segment, so the child process and the parent process execute the same program (may execute different ones). When the child process wants to execute another program, it needs to replace its own program, so the exec function is called to execute another program. When the exec function is called, the data and code in the child process are completely replaced by the new process, and the child process executes the new process. But the exec function does not create a new process, so the id of the process before and after calling exec does not change

write picture description here

Process replacement function

Note: Among the six functions, only execve is a real system call (interface provided by the kernel), and other functions are encapsulated library functions on this basis.

#include <unistd.h>
int execl(const char *path, const char *arg, ..., (char*)0);
int execlp(const char *file, const char *arg, ..., (char*)0);
int execle(const char *path, const char *arg, ..., (char*)0, char * constenvp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execve(const char *file, char *const argv[], char *const envp[]);

Parameter introduction:

  1. l(list): List of parameter addresses, terminated by a null pointer.

  2. v(vector): The address of the pointer array that stores the address of each parameter. When using it, first construct a pointer array, and the pointer array stores the address of each parameter, and then award the pointer array as the parameter of the function

  3. p(path): Search for executable files according to the directory specified by the system environment variable PATH. The file name is specified by the first parameter file. When the specified file name contains /, it will be regarded as a path name, and the program will be executed directly in the specified path.

  4. e(environment): The first address of the pointer array that stores the address of the environment variable string. What execle and execve change is the environment variable of the program started by exec (the new environment variable is completely specified by environment). The programs launched by the other four functions use the default system environment variables.

Introduction to some functions

  1. execlp
int execlp(const char *file, const char *arg, .....);

char *const ps_argv[] = {"ps", "ax", NULL};
execlp("ps", "ps", "ax", NULL);

参数:
    第一个"ps"是可执行程序;
    后面"ps" "ax"NULL 是可执行程序的内容
  1. execvp
int execvp(const char* file,char* const argv[]);
//形参分别为(可执行程序名,main函数参数列表)
//第一个参数:可执行程序所在的绝对位置
//第二个参数:可执行程序运行时所需的参数,这里是一个可变参数,每个参数之间用逗号分割
//参数结束时,最后一个参数是:NULL

The explanation of "the last parameter is: NULL" here is purely personal opinion. If there is any error, please correct me : The reason why you don't need to manually add NULL to the variable parameter list in the main function is that "the first parameter of the main function The parameter represents the number of variable parameters in the back, so you only need to loop the number of times represented by the first parameter to stop", and the first parameter of execvp represents the absolute position of the executable program, not the variable parameters behind. The number of , so you need to manually add NULL as a stop condition.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324607308&siteId=291194637