进程程序替换

进程程序替换

用fork创建子进程后执行的是和父进程相同的程序(但有可能执行不同的代码分支),子进程往要调用一种exec函数以执行另一个程序。当进程调用一种exec函数时,该进程的用户空间代码和数据完全被新程序替换(也仅仅替换代码和数据,其它不变),从新程序的启动例程开始执行。调用exec并不创建新进程,所以调用exec前后该进程的id并未改变。

函数原型

其实有六种以exec开头的函数,统称exec函数:

前5中为C库函数,在man 2 号手册中:

         

最后一种为系统调用,在3号手册中:

         

前5种库函数的实现都是封装了最后的系统调用。如图:

              

这些函数如果调用成功则加载新的程序从启动代码开始执行,不再返回,如果调用出错则返回-1, 所以exec函数只有出错的返回值而没有成功的返回值。

这些函数原型看起来很容易混,但只要掌握了规律就很好记。
不带字母p (表示示path)的exec函数 第一个参数必须是程序的相对路径或绝对路径,例如"/bin/ls"或"./a.out",而不能是"ls"或"a.out"。对于带字母p的函数: 如果参数中包含/,则将其视为路径名。 否则视为不带路径的程序名,在PATH环境变量的目录列表中搜索这个程序。
带有字母l( 表示示list)的exec函数要求将新程序的每个命令行参数都当作一个参数传给它,命令行参数的个数是可变的,因此函数原型中有...,...中的最后一个可变参数应该是NULL, 起sentinel的作用用。
带有字母v( 表示示vector)的函数,则应该先构造一个指向各参数的指针数组,然后将该数组的首地址当作参数传给它,数组中的最后一个指针也应该是NULL,就main函数的argv参数或者环境变量表一样。
对于以e (表示示environment)结尾的exec函数,可以把一份新的环境变量表传给它,其他exec函数仍使用当前的环境变量表执行新程序。

示例代码:

execl:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
	pid_t id = fork();
	if (id < 0)
	{
		perror("fork");
		return 1;
	}
	else if (id == 0) // child
	{
		printf("child exec ...\n");
		execl("/bin/ls", "ls", "-l", NULL);
		printf("exec error\n");
	}
	else		  // father
	{
		printf("father wait ...\n");
		int ret = waitpid(id, NULL, 0);
		if (ret < 0)
		{
			perror("wait");
			return 2;
		}
		else
		{
			printf("wait %d success...\n", ret);
		}
	}

	return 0;
}
运行结果:

         

execv:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
	pid_t id = fork();
	if (id < 0)
	{
		perror("fork");
		return 1;
	}
	else if (id == 0) // child
	{
		printf("child exec ...\n");
		char * const argv[] = {
			"ls",
			"-l",
			NULL
		};

		execv("/bin/ls", argv);
		printf("exec error\n");
	}
	else		  // father
	{
		printf("father wait ...\n");
		int ret = waitpid(id, NULL, 0);
		if (ret < 0)
		{
			perror("wait");
			return 2;
		}
		else
		{
			printf("wait %d success...\n", ret);
		}
	}

	return 0;
}

运行结果:

         

execve:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
	pid_t id = fork();
	if (id < 0)
	{
		perror("fork");
		return 1;
	}
	else if (id == 0) // child
	{
		printf("child exec ...\n");
		char * const argv[] = {
			"ls",
			"-l",
			NULL
		};
		char * const envp[] = {
			"/bin"
		};
		execve("/bin/ls", argv, envp);
		printf("exec error\n");
	}
	else		  // father
	{
		printf("father wait ...\n");
		int ret = waitpid(id, NULL, 0);
		if (ret < 0)
		{
			perror("wait");
			return 2;
		}
		else
		{
			printf("wait %d success...\n", ret);
		}
	}

	return 0;
}

运行结果:

         

其它三个函数类似,不在演示。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fireplusplus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值