文章目录
一、exec族函数函数的作用:
我们用fork函数创建新进程后,经常会在新进程中调用exec函数去执行另外一个程序。当进程调用exec函数时,该进程被完全替换为新程序。因为调用exec函数并不创建新进程,所以前后进程的ID并没有改变。
功能
在调用进程内部执行一个可执行文件。可执行文件既可以是二进制文件,也可以是任何Linux下可执行的脚本文件。
函数族
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[]); //初学很少用,有工作经验后,做一些功能需要对环境变量进行修改的时候才会用到
返回值:
exec函数族的函数执行成功后不会返回,调用失败时,会设置errno并返回-1,然后从原程序的调用点接着往下执行。
参数说明:
path:可执行文件的路径名字
arg:可执行程序所带的参数,第一个参数为可执行文件名字,没有带路径且arg必须以NULL结束
file:如果参数file中包含/,则就将其视为路径名,否则就按 PATH环境变量,在它所指定的各目录中搜寻可执行文件。
exec族函数参数极难记忆和分辨,函数名中的字符会给我们一些帮助:
l : 使用参数列表
p:使用文件名,并从PATH环境进行寻找可执行文件
v:应先构造一个指向各参数的指针数组,然后将该数组的地址作为这些函数的参数。
e:多了envp[]数组,使用新的环境变量代替调用进程的环境变量
二、demo:execl 的使用
//文件execl.c
#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","abc",NULL) == -1) //arg必须以NULL 结尾
{
printf("execl failed!\n");
perror("execl");
//前面说到exec函数族调用失败时,会设置errno并返回-1,可用perror将错误打印出来
}
printf("after execl\n");
return 0;
}
//文件echoarg.c
#include <stdio.h>
int main(int argc,char *argv[])
{
int i = 0;
for(i = 0; i < argc; i++)
{
printf("argv[%d]: %s\n",i,argv[i]);
}
return 0;
}
实验结果
若成功:我们可以很明显看到 printf(“after execl\n”); 这一句并没有执行,所以exec 启动的新程序成功后,该进程被完全替代。
若失败:
demo:使用execl 调用系统指令
whereis date 查看 date 的绝对路径
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
printf("before execl\n");
if(execl("/bin/date","date",NULL) == -1)
{
printf("execl failed!\n");
perror("execl");
}
printf("after execl\n");
return 0;
}
实验结果:
三、demo:ececlp 的使用
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
printf("before execl\n");
if(execlp("ps","ps",NULL) == -1)
{
printf("execl failed!\n");
perror("execl");
}
printf("after execl\n");
return 0;
}
实验结果:
从实验结果可以看出,上面的exaclp函数可以通过环境变量PATH查找到可执行文件ps,不用每次都使用whereis 自己去查询了
四、demo:execvp 的使用
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
printf("before execl\n");
char *argv[] = {"ps",NULL};
if(execvp("ps",argv) == -1)
{
printf("execl failed!\n");
perror("execl");
}
printf("after execl\n");
return 0;
}
也能达到效果,但它与不带v 的函数的差别就是,你要启动的新程序的参数不再是写在函数里了,而是用一个指针数组来存放了
五、为什么要用exec 族函数,有什么作用
六、exec族函数配合fork使用
修改配置文件
#include <stdio.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pid;
int data = 0;
while(1){
printf("please input data:\n");
scanf("%d",&data);
if(data == 1){
pid = fork();
if(pid > 0){
wait(NULL);
}
if(pid == 0){
while(1){
execl("change","change","test.config",NULL);
}
}
}else{
printf("wait connect\n");
}
}
return 0;
}
change 程序:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
char *modify(char *buf)
{
char *p = strstr(buf,"LENG=");
p = p + strlen("LENG=");
*p = 5;
return buf;
}
int main(int argc,char **argv)
{
int fdSrc;
fdSrc = open(argv[1],O_RDWR);
int size = lseek(fdSrc,0,SEEK_END);
lseek(fdSrc,0,SEEK_SET);
char *readBuf;
readBuf = (char *)malloc(sizeof(char)*size +1);
int n_read = read(fdSrc,readBuf,size);
readBuf = modify(readBuf);
lseek(fdSrc,0,SEEK_SET);
int n_write = write(fdSrc,readBuf,strlen(readBuf));
close(fdSrc);
return 0;
}