Linux进程——exec族函数

一、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;
}
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值