当前位置:网站首页>Use of exec series functions (EXECL, execlp, execle, execv, execvp)
Use of exec series functions (EXECL, execlp, execle, execv, execvp)
2022-06-26 10:09:00 【Ruo_ Xiao】
One 、exec Replace process image
In process creation Unix It took a unique approach , It will Process creation and loading a new process image are separated . The advantage is that there's more room to manage both operations .
When we create a process , The child process is usually replaced with a new process image , It works exec A series of functions . Of course ,exec A series of functions can also replace the current process .
for example : stay shell Command line execution ps command , It's actually shell Process call fork Copy a new child process , Using exec The system call completely replaces the newly generated subprocess with ps process .
Two 、exec Series of functions (execl、execlp、execle、execv、execvp)
function :
use exec Function can replace the current process with a new process , The new process is the same as the original process PID.exec Under the name is a complete series composed of multiple correlation functions .
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[]);path Parameter indicates the name of the program you want to start, including pathname .
arg The parameter indicates the parameter of the startup program , Generally, the first parameter is the name of the command to be executed , Not with path and arg Must be NULL end .
Return value : Successfully returns 0, Failure to return -1.
notes : Above exec The bottom layer of a series of functions is through execve System call implementation .
#include <unistd.h>
int execve(const char *filename, char *const argv[],char *const envp[]);
DESCRIPTION:
execve() executes the program pointed to by filename.
filename must be either a binary executable, or a script
starting with a line of the formabove exec The difference between a series of functions :
1、 belt l Of exec function :execl、execlp、execle, Indicates that the following parameters are given in the form of variable parameters and all end with a null pointer .
Example :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
printf("entering main process---\n");
execl("/bin/ls","ls","-l",NULL);
printf("exiting main process ----\n");
return 0;
}result :
entering main process---
Total usage 24
-rwxr-xr-x 1 xcl xcl 16456 6 month 7 09:11 execl
-rw-r--r-- 1 xcl xcl 215 6 month 7 09:11 execl.c
utilize execl Put the current process main Replace , All the last printed statements will not be output .
2、 belt p Of exec function :execlp、execvp, Represents the first parameter path Do not enter the full path , Just give the command name , It will be in the environment variable PATH Find the command in
Example :
When not p But when the full path is not given :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
printf("entering main process---\n");
execl("ls","-l",NULL);
printf("exiting main process ----\n");
return 0;
}result :
entering main process---
exiting main process ----
The result shows that... Could not be found , All replacements failed ,main The process continues
Now bring it p:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
printf("entering main process---\n");
execlp("ls","-l",NULL);
printf("exiting main process ----\n");
return 0;
}result :
entering main process---
main main.c
Replacement successful .
3、 No l Of exec function :execv、execvp Represents the parameters required by the command to char *arg[] The form is given and arg The last element must be NULL .
Example :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
printf("entering main process---\n");
int ret;
char *argv[] = {"ls","-l",NULL};
ret = execvp("ls",argv);
if(ret == -1)
perror("execl error");
printf("exiting main process ----\n");
return 0;
}result :
entering main process---
Total usage 24
-rwxr-xr-x 1 xcl xcl 16512 6 month 7 09:21 main
-rw-r--r-- 1 xcl xcl 306 6 month 7 09:21 main.c
Process replacement succeeded .
4、 belt e Of exec function :execle Express , Pass the environment variable to the process that needs to be replaced
From the above function prototype, we find that :
extern char **environ;
Here environ It's an array of Pointers , Every pointer in it points to char by “XXX=XXX”
environ The data storing the environment information can env Command view :

It consists of shell The process is passed to the current process , Then the current process passes it to the new process to be replaced .
Example :execle.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
//char * const envp[] = {"AA=11", "BB=22", NULL};
printf("Entering main ...\n");
int ret;
ret =execl("./hello", "hello", NULL);
//execle("./hello", "hello", NULL, envp);
if(ret == -1)
perror("execl error");
printf("Exiting main ...\n");
return 0;
}
hello.c
#include <unistd.h>
#include <stdio.h>
extern char** environ;
int main(void)
{
printf("hello pid=%d\n", getpid());
int i;
for (i=0; environ[i]!=NULL; ++i)
{
printf("%s\n", environ[i]);
}
return 0;
}result :

It can be seen that the original process does pass the environment variable information to the new process .
So now we can use execle The function itself gives the environment variable information that needs to be passed :
The sample program :execle.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char * const envp[] = {"AA=11", "BB=22", NULL};
printf("Entering main ...\n");
int ret;
//ret =execl("./hello", "hello", NULL);
ret =execle("./hello", "hello", NULL, envp);
if(ret == -1)
perror("execl error");
printf("Exiting main ...\n");
return 0;
}hello.c
#include <unistd.h>
#include <stdio.h>
extern char** environ;
int main(void)
{
printf("hello pid=%d\n", getpid());
int i;
for (i=0; environ[i]!=NULL; ++i)
{
printf("%s\n", environ[i]);
}
return 0;
}result :
Entering main ...
hello pid=13886
AA=11
BB=22
Indeed, the given environment variables are passed .
(SAW:Game Over!)
边栏推荐
- 从tf 1.x到tf 2.6(遇到的就过来更新更新)
- SQL function
- Specific meaning of go bootstrap
- Automated testing -- on the coexistence of Unitest and pytest initialization
- WGCLOUD的web ssh服务端口是多少
- 【LeetCode】59. 螺旋矩阵 II
- The basis of C language grammar -- function definition learning
- Glide's most common instructions
- jar版本冲突问题解决
- C中字符串基本操作
猜你喜欢

Redis notes (14) - persistence and data recovery (data persistence RDB and AOF, data recovery, mixed persistence)

创建对象的时候堆内存的分配

全渠道、多场景、跨平台,App如何借助数据分析渠道流量

Constraintlayout control uses full Raiders

druid数据源实现后台监控

Redis notes (15) - Pipeline (the client packages and sends batch commands to save network overhead)

Differences between JVM, Dalvik and art

逻辑英语结构【重点】

Install new version cmake & swig & tinyspline

如何更改微信小程序二维码物料颜色
随机推荐
逻辑英语结构【重点】
What should the preview do?
Problems encountered by jupyter notebook
JVM的符号引用和直接引用是什么
教你用shell脚本检测服务器程序是否在运行
c语言语法基础之——函数定义学习
logback
Logical English structure [key points]
Notes on sports planning on November 22, 2021
Code statistics tools cloc and SCC
Leetcode connected to rainwater series 42 (one dimension) 407 (2D)
c语言语法基础之——指针(字符、一维数组) 学习
How do technicians send notifications?
字符串常量池、class常量池和运行时常量池
Redis novice introduction
install realsense2: The following packages have unmet dependencies: libgtk-3-dev
2021 national vocational college skills competition (secondary vocational group) network security competition questions (1) detailed analysis tutorial
A list of common methods for customizing paint and canvas of view
【二分查找】4. 寻找两个正序数组的中位数
創建對象的時候堆內存的分配