Linux进程之exec族函数

本文详细介绍了Linux中的exec族函数,包括execl、execlp、execv等,阐述了它们在进程替换中的作用和使用方式。通过示例解释了不同函数如何处理命令行参数和路径搜索,并提及了exec族函数与fork配合的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

fork函数创建新进程后,经常会在新进程中调用exec函数去执行另外一个程序。当进程调用exec函数时,该进程被完全替换为新程序。因为调用exec函数并不创建新进程,所以前后进程的ID并没有改变。

一、int execl(const char *path, const char *arg, …);

原创链接

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

//函数原型:int execl(const char *path, const char *arg, ...);
//						可执行文件名字		可执行程序所带的参数,没有带路径且arg必须以NULL结束
//whereis指令查找程序的绝对路径

int main(void)
{
    printf("before execl\n");
    if(execl("./echoarg","echoarg","abcde",NULL) == -1)
    {
        printf("execl failed!\n");     
		perror("why"); //解析失败的原因
    }
    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;
}

execl 执行 ls -l 指令

#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("/bin/ls","ls","-l",NULL) == -1)
    {
        printf("execl failed!\n");     
	perror("why"); 
    }
    printf("after execl\n");
    return 0;
}


带l的一类exac函数(l表示list),包括execl、execlp、execle,要求将新程序的每个命令行参数都说明为 一个单独的参数。这种参数表以空指针结尾。

二、int execlp(const char *file, const char *arg, …);

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execlp(const char *path, const char *arg, ...);
//上面的exaclp函数带p,所以能通过环境变量PATH查找到可执行文件ps
//echo ¥PATH查看当前环境变量。环境变量作用:系统找到这些路径底下的可执行程序。
//配置环境变量:1.pwd:显示当前路径	2.export PATH=¥PATH:...   (当前路径)

int main(void)
{
    printf("this pro get system date:\n");
    if(execlp("ps","ps",NULL,NULL) == -1)
    {
        printf("execl failed!\n");     
	perror("why"); 
    }
    printf("after execl\n");
    return 0;
}

带p的一类exac函数,包括execlp、execvp、execvpe,如果参数file中包含/,则就将其视为路径名,否则就按 PATH环境变量,在它所指定的各目录中搜寻可执行文件。举个例子,PATH=/bin:/usr/bin

三、int execv(const char *path, char *const argv[]);

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    printf("this pro get system date:\n");

	char *argv[] = {"ps",NULL,NULL};
	
	
    if(execv("/bin/ps",argv) == -1)
    {
        printf("execl failed!\n");     
	perror("why"); 
    }
    printf("after execl\n");
    return 0;
}

带v不带l的一类exac函数,包括execv、execvp、execve,应先构造一个指向各参数的指针数组,然后将该数组的地址作为这些函数的参数。

四、int execvp(const char *file, char *const argv[]);

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    printf("this pro get system date:\n");

	char *argv[] = {"ps",NULL,NULL};
	
    if(execvp("ps",argv) == -1)
    {
        printf("execl failed!\n");     
	   perror("why"); 
    }
    printf("after execl\n");
    return 0;
}

五、int execle(const char *path, const char *arg,…, char * const envp[]);

六、int execvpe(const char *file, char *const argv[],char *const envp[]);

七、exec族函数配合fork

实现:当父进程检测到输入为1的时候,创建子进程把配置文件的字段值修改掉

config文件

SPEED=3
LENG=9
SCORE=9
LEVEL=5

changeData可执行文件

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

int main(int argc,char **argv)
{
	int fdSrc;
	char *readbuf = NULL;

	if(argc != 2){
		printf("pararm error \n");
		exit(-1);
	}
	
	fdSrc = open(argv[1],O_RDWR);
	int size = lseek(fdSrc,0,SEEK_END);
	lseek(fdSrc,0,SEEK_SET);
	readbuf = (char *)malloc(sizeof(char )*size+8);

	int n_read = read(fdSrc,readbuf,size);
	
//      char *strstr(const char *haystack, const char *needle);
	char *p = strstr(readbuf,"LENG=");
	if(p == NULL){
		printf("not found\n");
		exit(-1);
	}

	p = p + strlen("LENG=");
	*p = '5';

	lseek(fdSrc,0,SEEK_SET);
	int n_write = write (fdSrc,readbuf,strlen(readbuf));

	printf("\n");
	close(fdSrc);
	
	return 0;
}

#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
	pid_t pid;
	int data = 10;

	while(1){
		
		printf("Please Input a data\n");
		scanf("%d",&data);
		if(data == 1){
			
			pid = fork();
			
				if(pid > 0){
					wait(NULL);
				}

				 if(pid == 0) {
//实现:当父进程检测到输入为1的时候,创建子进程把配置文件的字段值修改掉
					execl("./changeData","changeData","config.txt",NULL);
					exit(0);
				}
		}
		else{
			printf("wait ... do nothing\n");
		}
			
	}
	return 0;
}

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值