一、exec族函数的作用
我们用frok函数所创建新进程后,经常会在新进程中调用exec函数去执行另外一个程序。当进程调用exec函数时,该进程被完全替换为新程序。因为exec函数并不创建新进程,所以前后进程的ID并没有改变。
函数族
exec函数族分别是:execl,execlp,execle,execv,execvp,execvpe
函数原型:
#include <unistd.h>
extern char **environ;
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[]);
int execvpe(const char *file, char *const argv[],char *const envp[]);
————————————————
版权声明:本文为CSDN博主「云英」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u014530704/article/details/73848573
返回值:
exec函数族的函数执行成功后不会返回,调用失败时,会设置errno并返回-1,然后从原程序的调用点接着往下执行。
参数说明:
path:可执行文件的路径名字
arg:可执行程序所带的参数,第一个参数为可执行文件名字,没有带路径且arg必须以NULL结束
file:如果参数file中包含/,则就将其视为路径名,否则就按 PATH环境变量,在它所指定的各目录中搜寻可执行文件。
exec族函数参数极难记忆和分辨,函数名中的字符会给我们一些帮助:
l : 使用参数列表
p:使用文件名,并从PATH环境进行寻找可执行文件
v:应先构造一个指向各参数的指针数组,然后将该数组的地址作为这些函数的参数。
e:多了envp[]数组,使用新的环境变量代替调用进程的环境变量
————————————————
版权声明:本文为CSDN博主「云英」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u014530704/article/details/73848573
execl示例
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void)
{
printf("before execl\n");
if(execl("./echoarg","echoarg","ls","haha",NULL) == -1){
printf("execl failed!\n");
perror("why");//perror函数可以解析 execl函数失败的返回值 并将错误原因打印
}
printf("after execl\n");
return 0;
}
调用date函数显示系统时间
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void)
{
printf("before execl\n");
printf("system time is:\n");
if(execl("/bin/date","date",NULL) == -1){
printf("execl failed!\n");
perror("why");
}
printf("after execl\n");
return 0;
}
~
execlp
execlp函数带p,所以能通过环境变量PATH查找到可执行文件ps
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void)
{
printf("before execl\n");
printf("system time is:\n");
if(execlp("ps","ps",NULL) == -1){ //execlp函数会自己在环境变量里查找可执行文件
printf("execl failed!\n");
perror("why");
}
printf("after execl\n");
return 0;
}
execvp函数示例
先构造一个指向各参数的指针数组,然后将该数组的地址作为这些函数的参数。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execvp(const char *file, char *const argv[]);
int main(void)
{
printf("before execl\n");
char *argv[]={"ps",NULL};
if(execvp("ps",argv) == -1){
printf("execl failed!\n");
perror("why");
}
printf("after execl\n");
return 0;
}
execv函数示例
和execvp相比 第一个参数需要改为绝对路径
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execvp(const char *file, char *const argv[]);
int main(void)
{
printf("before execl\n");
char *argv[]={"ps",NULL};
if(execv("/bin/ps",argv) == -1){
printf("execl failed!\n");
perror("why");
}
printf("after execl\n");
return 0;
}
二、exec配合fork使用
实现功能,当父进程检测到输入为1的时候,创建子进程把配置文件的字段值修改掉。
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
int main()
{
pid_t pid;
int data;
while(1){
printf("please enter a data:\n");
scanf("%d",&data);
if(data==1){
pid=fork();
if(pid>0){
wait(NULL);//收集子进程状态防止僵尸进程
}
else if(pid==0){
execl("./wenjian/changeData","changeData","./wenjian/TEST.config",NULL); //使用execl函数调用另一程序修改config文件内容
exit(0);
}
}
else{
printf("wait,do nothing\n");
}
}
return 0;
}
三、 system函数
和execl 功能差不多 ,更加简单直接
但是system执行完后还会返回原程序 execl则不会
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
int main()
{
pid_t pid;
int data;
while(1){
printf("please enter a data:\n");
scanf("%d",&data);
if(data==1){
pid=fork();
if(pid>0){
wait(NULL);
}
else if(pid==0){
system("./changeData TEST.config");
exit(0);
}
}
else{
printf("wait,do nothing\n");
}
}
return 0;
}
四、popen函数
FILE *popen(const char *command, const char *type);
参数说明:
command: 是一个指向以 NULL 结束的 shell 命令字符串的指针。这行命令将被传到 bin/sh 并使用 -c 标志,shell 将执行这个命令。
mode: 只能是读或者写中的一种,得到的返回值(标准 I/O 流)也具有和 type 相应的只读或只写类型。如果 type 是 “r” 则文件指针连接到 command 的标准输出;如果 type 是 “w” 则文件指针连接到 command 的标准输入。
作用:
popen() 函数用于创建一个管道:其内部实现为调用 fork 产生一个子进程,执行一个 shell 以运行命令来开启一个进程这个进程必须由 pclose() 函数关闭。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
char ret[1024]={0};
FILE *fp;
fp=popen("ps","r");
int nread=fread(ret,1,1024,fp);
printf("read %d byte,the ret is:%s\n",nread,ret);
return 0;
}
popen的好处在于可以将指令的结果存放到字符数组或者文件当中