一、族函数的引入:
1、族函数说明:
fork函数是用于创建一个子进程,该子进程几乎是父进程的副本,而有时我们希望子进程去执行另外的程序,exec函数族就提供了一个在进程中启动另一个程序执行的方法。它可以根据指定的文件名或目录名找到可执行文件,并用它来取代原调用进程的数据段、代码段和堆栈段,在执行完之后,原调用进程的内容除了进程号外,其他全部被新程序的内容替换了。另外,这里的可执行文件既可以是二进制文件,也可以是Linux下任何可执行脚本文件。
2、在Linux中使用exec函数族主要有以下两种情况:
a、当进程认为自己不能再为系统和用户做出任何贡献时,就可以调用任何exec 函数族让自己重生。
b、如果一个进程想执行另一个程序,那么它就可以调用fork函数新建一个进程,然后调用任何一个exec函数使子进程重生。
二、exec族函数的介绍和实战:
1、还是老套路,首先我们用man 3 exec来查看有哪些exec族函数:
NAME
execl, execlp, execle, execv, execvp, execvpe - execute a file
SYNOPSIS
#include <unistd.h>
extern char **environ;
int execl(const char *path, const char *arg, ...
/* (char *) NULL */);
int execlp(const char *file, const char *arg, ...
/* (char *) NULL */);
int execle(const char *path, const char *arg, ...
/*, (char *) NULL, 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[]);
DESCRIPTION
The exec() family of functions replaces the current process image with
a new process image. The functions described in this manual page are
front-ends for execve(2). (See the manual page for execve(2) for fur‐
ther details about the replacement of the current process image.)
The initial argument for these functions is the name of a file that is
to be executed.
The const char *arg and subsequent ellipses in the execl(), execlp(),
and execle() functions can be thought of as arg0, arg1, ..., argn.
Together they describe a list of one or more pointers to null-termi‐
nated strings that represent the argument list available to the exe‐
cuted program. The first argument, by convention, should point to the
filename associated with the file being executed. The list of argu‐
ments must be terminated by a null pointer, and, since these are vari‐
adic functions, this pointer must be cast (char *) NULL.
The execv(), execvp(), and execvpe() functions provide an array of
pointers to null-terminated strings that represent the argument list
available to the new program. The first argument, by convention,
should point to the filename associated with the file being executed.
The array of pointers must be terminated by a null pointer.
The execle() and execvpe() functions allow the caller to specify the
environment of the executed program via the argument envp. The envp
argument is an array of pointers to null-terminated strings and must be
terminated by a null pointer. The other functions take the environment
for the new process image from the external variable environ in the
calling process.
Special semantics for execlp() and execvp()
The execlp(), execvp(), and execvpe() functions duplicate the actions
of the shell in searching for an executable file if the specified file‐
name does not contain a slash (/) character. The file is sought in the
colon-separated list of directory pathnames specified in the PATH envi‐
ronment variable. If this variable isn't defined, the path list
defaults to a list that includes the directories returned by conf‐
str(_CS_PATH) (which typically returns the value "/bin:/usr/bin") and
possibly also the current working directory; see NOTES for further
details.
If the specified filename includes a slash character, then PATH is
ignored, and the file at the specified pathname is executed.
In addition, certain errors are treated specially.
If permission is denied for a file (the attempted execve(2) failed with
the error EACCES), these functions will continue searching the rest of
the search path. If no other file is found, however, they will return
with errno set to EACCES.
If the header of a file isn't recognized (the attempted execve(2)
failed with the error ENOEXEC), these functions will execute the shell
(/bin/sh) with the path of the file as its first argument. (If this
attempt fails, no further searching is done.)
RETURN VALUE
The exec() functions return only if an error has occurred. The return
value is -1, and errno is set to indicate the error.
ERRORS
All of these functions may fail and set errno for any of the errors
specified for execve(2).
说明与实战demo:
a、我们首先来分析execl和execv :
int execl(const char *path, const char *arg, ... /* (char *) NULL */);
int execv(const char *path, char *const argv[]);
这两个函数是最基本的exec族函数,都可以用来执行一个程序,区别是传参的格式不同。execl是把参数列表(“...”它是一个变参,本质上是多个字符串,【必须以NULL结尾】)依次排列而成(execl中的“l”其实就是list的缩写),execv是把参数列表事先放入一个字符串数组中,再把这个字符串数组传给execv函数。而参数path都表示可执行的文件路径。
现在我们以可执行程序ls -la来演示,但是我们的先知道它的路径,要用命令---which ls 来查看:
ubuntu@ubuntu-virtual-machine:~$ which ls
/bin/ls
我们先用execl函数来演示:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(void)
{
pid_t pid = -1;
pid_t ret = -1;
int status = -1;
pid = fork();
if (pid > 0)
{
// 父进程
printf("parent, 子进程id = %d.\n", pid);
}
else if (pid == 0)
{
// 子进程
execl("/bin/ls", "ls", "-l", "-a", NULL); // ls -l -a
return 0;
}
else
{
perror("fork");
return -1;
}
return 0;
}
演示结果:
接着我们用execv来演示:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(void)
{
pid_t pid = -1;
pid_t ret = -1;
int status = -1;
pid = fork();
if (pid > 0)
{
// 父进程
printf("parent, 子进程id = %d.\n", pid);
}
else if (pid == 0)
{
// 子进程
char * const arg[] = {"ls", "-l", "-a", NULL};
execv("/bin/ls", arg);
return 0;
}
else
{
perror("fork");
return -1;
}
return 0;
}
演示效果:
最后我们可以利用上面讲的函数来实现我们开头讲的那样(其实上面举得例子也是一样的效果),自己编写一个外部文件,来提高灵活性,这里我我创建了一个hello.c文件,内容是,然后再用execl调用执行:
#include <stdio.h>
int main(int argc, char **argv)
{
int i = 0;
printf("argc = %d.\n", argc);
while (NULL != argv[i])
{
printf("argv[%d] = %s\n", i, argv[i]);
i++;
}
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(void)
{
pid_t pid = -1;
pid_t ret = -1;
int status = -1;
pid = fork();
if (pid > 0)
{
// 父进程
printf("parent, 子进程id = %d.\n", pid);
}
else if (pid == 0)
{
// 子进程
execl("hello","aaa", NULL);
return 0;
}
else
{
perror("fork");
return -1;
}
return 0;
}
演示结果:
b、分析execlp和execvp:
int execlp(const char *file, const char *arg, .../* (char *) NULL */);
int execvp(const char *file, char *const argv[]);
execlp和execvp 这两个函数在上面2个基础上加了p,较上面2个来说,区别是:上面2个执行程序时必须指定可执行程序的【全路径】(如果exec没有找到path这个文件则直接报错),而加了p的传递的可以是file(也可以是path,只不过兼容了file。加了p的这两个函数会首先去找file,如果找到则执行,如果没找到则会去环境变量PATH所指定的目录下去找,如果找到则执行如果没找到则报错)---(注意:
进程中的环境变量说明,在Linux中,Shell进程是所有执行码的父进程。当一个执行码执行时,Shell进程会fork子进程然后调用exec函数去执行执行码。Shell进程堆栈中存放着该用户下的所有环境变量,使用execl、execv、execlp、execvp函数使执行码重生时,Shell进程会将所有环境变量复制给生成的新进程;而使用execle、execve时新进程不继承任何Shell进程的环境变量,而由envp[]数组自行设置环境变量。这两个函数会在下面进行讲解的):
现在使用excel来演示可执行程序----ls -la,最后它在环境变量目录下找到了ls ,在当前用户目录找不到:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(void)
{
pid_t pid = -1;
pid_t ret = -1;
int status = -1;
pid = fork();
if (pid > 0)
{
// 父进程
printf("parent, 子进程id = %d.\n", pid);
}
else if (pid == 0)
{
// 子进程
execlp("ls", "ls", "-l", "-a", NULL);
return 0;
}
else
{
perror("fork");
return -1;
}
return 0;
}
演示效果:
注明:execvp函数用法一样,这里我就不举例子了,可以按照execv用法模仿使用。
c、分析execle和execvpe:
int execle(const char *path, const char *arg, ... /*, (char *) NULL, char * const envp[] */);
int execvpe(const char *file, char *const argv[], char *const envp[]);
这两个函数较基本exec来说加了e,函数的参数列表中也多了一个字符串数组envp形参,e就是environment环境变量的意思,和基本版本的exec的区别就是:执行可执行程序时会多传一个环境变量的字符串数组给待执行的程序。
main函数的原型其实不止是int main(int argc, char **argv),而可以是int main(int argc, char **argv, char **env) 第三个参数是一个字符串数组,内容是环境变量,Linux系统下环境变量:
如果用户在执行这个程序时没有传递第三个参数,则程序会自动从父进程继承一份环境变量(默认的,最早来源于OS中的环境变量);如果我们exec的时候使用execle或者execvpe去给传一个envp数组,则程序中的实际环境变量是我们传递的这一份(取代了默认的从父进程继承来的那一份)
注意:execle和execvpe的第三个环境变量参数是可以更改从系统环境变量继承过来的这一份的。
下面我们还是以hello那个文件为例,然后再用execle函数来调用,hello文件里面的内容是:
#include <stdio.h>
// env就是我们给main函数额外传递的环境变量字符串数组
int main(int argc, char **argv, char **env)
{
int i = 0;
printf("argc = %d.\n", argc);
while (NULL != argv[i])
{
printf("argv[%d] = %s\n", i, argv[i]);
i++;
}
i = 0;
while (NULL != env[i])
{
printf("env[%d] = %s\n", i, env[i]);
i++;
}
return 0;
}
执行结果:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(void)
{
pid_t pid = -1;
pid_t ret = -1;
int status = -1;
pid = fork();
if (pid > 0)
{
// 父进程
printf("parent, 子进程id = %d.\n", pid);
}
else if (pid == 0)
{
// 子进程
char * const envp[] = {"AA=aaaa", "XX=abcd", NULL};
execle("hello", "hello", "-l", "-a", NULL, envp);
}
else
{
perror("fork");
return -1;
}
return 0;
}
演示结果:
注明:execvpe函数用法一样,这里我就不举例子了,可以按照execv用法模仿使用。
三、总结:
好了,今天族函数的分享就结束了,后面还会继续分享进程的文章,在最后给大家分享一个有趣的照片,昨天一个网友发的 Linux系统中常见的目录名称以及相应内容:
上面的源代码链接:https://github.com/1121518wo/linux-/tree/master