Linux 2.进程(exec族函数)

exec族函数作用

如果一个进程想执行另一个程序,那么它就可以调用fork函数新建一个进程,然后调用exec函数族中的任意一个函数,这样看起来就像通过执行应用程序而产生了一个新进程(这种情况非常普遍)。

exec头文件及原型

#include <unistd.h>

int execl(const char * path,const char * arg, ...)
int execle(const char * path,const char * arg,char * const envp[]);
int execlp(const char * file,const char * arg, ...)
int execv(const char * path,char * const argv[]);
int execve(const char * path,char * const argv[],char * const envp[]);
int execvp(const char * file,char * const argv[]);

exec参数

path:要执行的程序路径。可以是绝对路径或者是相对路径。在execv、execve、execl和execle这4个函数中,使用带路径名的文件名作为参数。

file:要执行的程序名称。如果该参数中包含“/”字符,则视为路径名直接执行;否则视为单独的文件名,系统将根据PATH环境变量指定的路径顺序搜索指定的文件。

argv:命令行参数的矢量数组。

envp:带有该参数的exec函数可以在调用时指定一个环境变量数组。其他不带该参数的exec函数则使用调用进程的环境变量。

arg:程序的第0个参数,即程序名自身。相当于argv[O]。

:命令行参数列表。调用相应程序时有多少命令行参数,就需要有多少个输入参数项。注意:在使用此类函数时,在所有命令行参数的最后应该增加一个空的参数项(NULL),表明命令行参数结束。

exec返回值

exec函数族的函数执行成功后则不会返回,也不会从原程序中调用点下面的继续往下执行,调用失败时,会设置errno并返回-1,然后从原程序的调用点接着往下执行。

execl

int execl(const char *path, const char *arg, ...);

execl使用示例

我们直接在Linux中输入 date 回车 会显示出系统的时间。但是我们想在 demo.c 里面运行程序打印 5.4.3.2.1 后 再去运行date。所以我们用到execl。

首先我们得知道date 的路径

whereis 对象
which 对象

在这里插入图片描述

在这里插入图片描述

回车就能知道路径
date 路径是在 /bin/date

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

int main()
{
    printf("before execl\n");

    if(execl("/bin/date","date",NULL)==-1)
    {
        printf("execl fail!\n");
        perror("why");
    }

    printf("after execl\n");

    return 0;
}

运行结果:

在这里插入图片描述

注意:我们execl成功了之后是不会继续运行 下面的after execl。如果不成功才会执行下面的。

但是我们会发现每一次去运行另一个程序都需要用 whereis 来查找路径。如果我们不写路径,直接写

在这里插入图片描述

date 的结果如下:

在这里插入图片描述

很显然,execl 是没有找到的我们所要的 date 。

但是我们加一个 p 用另一个 execlp 就可以做到。

execlp

int execlp(const char * file,const char * arg, ...)

用 execlp 就可以不用加绝对路径了。
execlp 比 execl 多了一个p就能通过环境变量PATH查找到可执行文件date

execlp使用示例

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

int main()
{
    printf("before execl\n");

    if(execlp("date","date",NULL)==-1)
    {
        printf("execl fail!\n");
        perror("why");
    }

    printf("after execl\n");

    return 0;
}

运行结果:

在这里插入图片描述

execle

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

execle使用示例

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

int main()
{
	int ret_wait = -1;
        int status;

	int pid = fork();

	if(pid > 0)
	{
//		sleep(1);

		printf("This is parent : %d\n",getpid());
//		ret_wait = waitpid(-1 ,&status, 0);
//		ret_wait = waitpid(pid , &status , 0);
		ret_wait = waitpid(pid , &status , 0);

		printf("Child's process id has been goten : %d\n",ret_wait);

		printf("Child process has normally returned : %d\n",WIFEXITED(status));
		printf("Child process has returned by accident : %d\n",WIFSIGNALED(status));
		printf("Child process has normally returned : %d\n",WEXITSTATUS(status));
	}

	else if(pid == 0)
	{	
//		sleep(1);

		printf("This is child : %d\n",getpid());
//		printf("My father's pid : %d\n",getppid());
		
		char *envp[] = {"A = aaa","B = bbb",NULL};
		execle("/home/zillion/Desktop/weidongshan/hello","./hello","1",NULL,envp);

		return 66;
	}

	return 0;
}

在这里插入图片描述

PATH 环境变量

输出当前环境变量

echo $PATH

在这里插入图片描述

如果需要修改可以在后面进行追加。比如想将 /etc/apache2/bin 添加为环境变量,可写为:

export PATH=$PATH:/etc/apache2/bin

然后回车即可。

我们可以自己配置环境变量 用 export

运行程序不用./名字了 直接名字回车:
现在别的路径底下也能运行别的路径的可执行程序了。

execv

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

其实这个跟上面的差不多的,就是把后面的参数包装了起来放在 char * const argv[] 数组里面。

execv示例

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

int main()
{
    printf("before execl\n");
    
    char* argv[]={"date",NULL,NULL};
    if(execv("/bin/date",argv)==-1)
    {
        printf("execl fail!\n");
        perror("why");
    }

    printf("after execl\n");

    return 0;
}

运行结果:

在这里插入图片描述

execvp

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

这个比上面的多了一个 P,其实就是不写绝对路径

execvp示例

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

int main()
{
    printf("before execl\n");
    
    char* argv[]={"date",NULL,NULL};
    if(execvp("date",argv)==-1)
    {
        printf("execl fail!\n");
        perror("why");
    }

    printf("after execl\n");

    return 0;
}

运行结果:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值