[Linux] Process replacement

Process replacement

Replace the program that a process is scheduled to run.
After creating a child process with fork, it executes the same program as the parent process (but it may execute a different code branch). The child process often calls an exec function to execute another program. When a process calls an exec function, the user space code and data of the process are completely replaced by the new program, and execution starts from the startup routine of the new program.
Calling exec does not create a new process, so the id of the process does not change before and after calling exec.

6 kinds of process replacement functions

#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[]);
int execvp(const char *file, char *const argv[]);

Function explanation

If these functions are successfully called, a new program will be loaded and executed from the startup code, and will not return.
If the call fails, it returns -1, so the exec function only has an error return value and no successful return value.

Naming comprehension

l(list): Indicates the parameter list
v(vector): Array for parameters
p(path): Automatic search environment variable PATH with p
e(env): Indicates that you maintain environment variables

Function name Parameter format With path? Use current environment variables?
execl List Is not Yes
execlp List Yes Yes
execle List Is not No, you must assemble the environment variables yourself
execv Array Is not Yes
execvp Array Yes Yes
execve Array Is not No, you must assemble the environment variables yourself

Guess you like

Origin blog.csdn.net/weixin_43962381/article/details/115270434