当前位置:网站首页>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!)
边栏推荐
- libgstreamer-1.0. so. 0: cannot open shared object file: No such file or directory
- Software testing - how to select the appropriate orthogonal table
- libmagic 介绍
- Specific meaning of go bootstrap
- 國際化配置
- 从tf 1.x到tf 2.6(遇到的就过来更新更新)
- Internationalization configuration
- Allocation de mémoire tas lors de la création d'objets
- Deep learning (tentsorflow2. version) three good student performance problems (1)
- Luogu 1146 coin flip
猜你喜欢

Some problems to be considered when designing technical implementation scheme

c语言语法基础之——指针(字符、一维数组) 学习
![Logical English structure [key points]](/img/4b/52a666ed01087adbc5fa4f9e1db393.png)
Logical English structure [key points]

國際化配置

WGCLOUD的web ssh服务端口是多少

Configuration internationale

904. 水果成篮

软件测试---如何选择合适的正交表

微软 Edge 浏览器 IE 模式标签页出现卡死情况,已通过回滚更新修复

Standard implementation of streaming layout: a guide to flexboxlayout
随机推荐
Some problems to be considered when designing technical implementation scheme
首批12家企业入驻!广州首个集中展销老字号产品专柜开张
软件测试---如何选择合适的正交表
druid数据源实现后台监控
Appium自动化测试基础 — 移动端测试环境搭建(二)
1. 两数之和(LeetCode题目)
Various errors encountered by tensorflow
cento7.7安装ELK简单记录
LSP 是什么
Learning and understanding of thread pool (with code examples)
2021-11-29 quintic polynomial of trajectory planning
Allocation of heap memory when creating objects
Internationalization configuration
【深度优先搜索】312.戳气球
Does the go compiled executable have dynamic library links?
JVM的符号引用和直接引用是什么
字符串常量池、class常量池和运行时常量池
逻辑英语结构【重点】
Day 3 array, pre post, character space, keyword and address pointer
US President signs community safety act to deal with gun issue