linux:进程的替换

概念

当我们使用fork时,会创建一个子进程来给我们执行代码,但是子进程执行的也是父进程的代码,我们再写代码的时候,往往是需要执行其它代码,所以当其它程序的代码替换到自己的程序中执行,这种行为叫,进程的替换

使用进程的替换我们往往需要使用exec类型函数来进行

让我们来查看一下exec的类型函数有哪些

复制代码
man exec //查看exec函数有哪些

exec函数有六种,让我们来分别讲解用途。

execl

复制代码
int execl(const char *pathname, const char *arg, .../* (char  *) NULL */);

参数讲解 :

pathname:用于指定可执行程序的绝对路径

arg:用于指定可执行程序

...:是一个多参数类型,用于指定运行可执行程序的执行方式。

NULL:execl函数后面需要加NULL

我们来举几个例子来实验一下

创建两个文件,text3.c和text2.c

text3.c

复制代码
#include<stdio.h>
#include<unistd.h>
int main()
{
	printf("execl start text3....\n");
	execl("/root/working8/text2","text2",NULL);
	printf("text3 to be action\n");
	return 0;
}

text2.c

复制代码
#include<stdio.h>
int main()
{
	printf("text2 to be action\n");
}

当前文件路径

复制代码
/root/working8

执行text3.c的可执行文件

我们看到,execl,替换代码后运行的是text2的代码,所以execl函数将代码替换了

替换代码是不是又创建了一个进程呢,我们通过getpid函数来验证一下

text3.c代码

复制代码
#include<stdio.h>
#include<unistd.h>
int main()
{
	printf("execl start text3....\n");
	printf("PID:%d\n",getpid());
	execl("/root/working8/text2","text2",NULL);
	printf("text3 to be action\n");
	return 0;
}

text2.c代码

复制代码
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
	printf("text2 to be action\n");
	printf("PID:%d\n",getpid());
}

运行结果

上面的PID和下面的PID相同,说明execl函数是将text2.c的代码复制到了text3.c中,而不是新创建了一个进程。

execlp

复制代码
int execlp(const char *file, const char *arg, .../* (char  *) NULL */);

这里的file与上面的PATH有所不同,file是用来指定环境变量中的文件名,而PATH是用来指定可执行文件的绝对路径 ,其它的与上面的execl相同

我们来用代码解释一下

复制代码
#include<stdio.h>
#include<unistd.h>
int main()
{
	printf("execl start text3....\n");
	execlp("ls","ls","-l","-a",NULL);
	printf("text3 to be action\n");
	return 0;
}

因为ls存在与环境变量中,所以我们可以直接指定。

执行结果 :

我们将text2可执行文件放入PATH中看看能不能直接执行

复制代码
PATH=$PATH:/root/working8

我们将当前路径添加到PATH环境变量中

更改text3.c代码

复制代码
#include<stdio.h>
#include<unistd.h>
int main()
{
	printf("execl start text3....\n");
	execlp("text2","text2",NULL);
	printf("text3 to be action\n");
	return 0;
}

text2.c代码

复制代码
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
	printf("text2 to be action\n");
	printf("PID:%d\n",getpid());
}

执行结果

我们可以发现execlp后面执行的是text2.c的代码

execle

execle通常用来更改环境变量表

复制代码
int execle(const char *pathname, const char *arg, .../*, (char *) NULL, char *const envp[] */);

其中NULL的后面还要增加一个char *const envp指针数组,这个指针数组就用用来更改环境变量的表
当进程替换之后,环境变量会继续使用之前的。

我们来用两组代码测试一下

text3.c

复制代码
#include<stdio.h>
#include<unistd.h>
int main()
{
	printf("execl start text3....\n");
	execl("/root/working8/text2","text2",NULL);
	return 0;
}

text2.c

复制代码
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main(int argc,char*argv[],char * env[])
{
	for(int i=0;i<20;i++)
	{
		printf("[%d]:%s\n",i,env[i]);
	}
}

运行结果

我们可以看到进程还是用了替换之前的环境变量

我们使用execle随便更改几个环境变量

更改text3.c代码

复制代码
#include<stdio.h>
#include<unistd.h>
int main()
{
	printf("execl start text3....\n");
	char* const ch[]={"A=aaa","B=bbb",NULL};
	execle("/root/working8/text2","text2",NULL,ch);
	return 0;
}

再次运行text3.c的可执行文件

运行结果

以上是execl的进程替换操作

execle和execl是要操作文件绝对路径的,而execle可以更改进程当前的环境变量。

execlp需要操作环境变量,操作PATH中的环境变量

execv系列

复制代码
int execv(const char *pathname, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],char *const envp[]);

char *const argv[]:是一个数组,里面包含了需要执行的文件,和文件执行的方式

file:和上面是一样的意思,文件名,此文件需要在环境变量表中

envp:用于修改进程的环境变量

我们来演示一下char*const argv[]的作用,其它的函数参数是大同小异的,不同的是我们不需要在函数参数的最末尾加NULL但是我们需要在数组的最末尾加NULL

代码如下

复制代码
#include<stdio.h>
#include<unistd.h>
int main()
{
	printf("execl start text3....\n");
	char*const ch[]={"ls","-l","-a",NULL};
	execv("/usr/bin/ls",ch);
	return 0;
}

执行结果如下

|---|--------------------------|
| p | 用于指定文件,在PATH环境变量表中查可执行文件 |
| l | 全名list,函数参数以链表的形式传入函数 |
| v | vector,函数参数以数组的方式传入函数 |
| e | env,环境变量,可以修改进程的环境变量 |

汇总一下六个进程替代的函数

int execl(const char *pathname, const char *arg, .../* (char *) NULL */);

int execlp(const char *file, const char *arg, ... /* (char *) NULL */);

int execle(const char *pathname, const char *arg, .../*, (char *) NULL, char *const envp[] */);

int execv(const char *pathname, char *const argv[]);

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

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

相关推荐
悄悄敲敲敲1 小时前
Linux:进程间通信->共享内存
linux·运维·服务器
绵绵细雨中的乡音1 小时前
Linux进程学习【环境变量】&&进程优先级
linux·运维·学习
天下·第二1 小时前
【Nginx】负载均衡配置详解
运维·nginx·负载均衡
梓䈑3 小时前
【Linux系统】详解Linux权限
linux·运维·bash
小茬粥3 小时前
kvm网卡发现的采集信息脚本COLT_CMDB_KVM_NETDISC.sh
linux·kvm
Mr_sun.3 小时前
Day23-Web开发——Linux
linux·运维·服务器
星雨流星天的笔记本3 小时前
1、Linux操作系统下,ubuntu22.04版本切换中英文界面
linux·学习
极小狐3 小时前
如何创建并使用极狐GitLab 部署令牌?
运维·git·ssh·gitlab·github
muxue1784 小时前
centos 7 网络配置(2):ping命令出现问题
linux·网络·centos