exec和execve等系统调用的exec系列的功能有什么区别?

bud*_*ghe 28 c linux exec system-calls

我最近一直在关注系统编程课程,我通过系统调用exec()execve()来完成.到目前为止,我发现这两者之间没有任何区别,即使维基百科也没有给出明确的解释,所以exec()execve()之间有区别.

有人可以给出关于exec系列调用的简短描述,例如execl(), execv(), execle(), execvp().

Bar*_*mar 51

没有exec系统调用 - 这通常用于将所有execXX调用称为一个组.它们都基本上做同样的事情:将新程序加载到当前进程中,并为其提供参数和环境变量.不同之处在于如何找到程序,如何指定参数以及环境来自何处.

  • v名称中的调用采用数组参数来指定argv[]新程序的数组.

  • 与电话NULL的名义采取新程序的参数作为一个可变长度的参数列表函数本身.

  • l名称中的调用需要额外的参数来提供新程序的环境; 否则,程序继承当前进程的环境.

  • (char *)NULL名称中的调用搜索NULL环境变量,以便在程序中没有目录时查找该程序(即它不包含e字符).否则,程序名称始终被视为可执行文件的路径.

  • 实际上,*only*[系统调用](http://en.wikipedia.org/wiki/System_call)是[execve(2)](http://man7.org/linux/man-pages/man2/execve .2.html)和所有其他`exec*`函数都包装它. (4认同)

nra*_*aus 20

使用man exec和阅读:

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. 
Run Code Online (Sandbox Code Playgroud)

execv

int execv(const char *path, char *const argv[]);
Run Code Online (Sandbox Code Playgroud)

所以你传递一个数组作为参数

int execle(const char *path, const char *arg,
              ..., char * const envp[]);
Run Code Online (Sandbox Code Playgroud)

几乎相同,但不是作为数组,而是作为值列表(字符串),后跟数组指定环境.

这里:

int execvp(const char *file, char *const argv[]);
Run Code Online (Sandbox Code Playgroud)

您正在调用一个没有路径的文件,所以它希望您path在调用之前已经在右侧.

最后但并非最不重要的:

int execve(const char *filename, char *const argv[],
                  char *const envp[]);
Run Code Online (Sandbox Code Playgroud)

与前一个类似,但现在您有两个数组,用于参数和环境变量.

  • 上述系统调用之间的唯一区别在于参数。是这样吗 如果是这样,执行所有执行家族系统(使用不同参数)的调用的最终结果是吗? (3认同)
  • 只是不同的参数,没有其他不同 (2认同)

Pra*_*shi 9

由于所有这些函数都属于exec()家族,因此让我differentiate根据extra characters其含义,

1.执行ve():

p:不存在=>要运行的程序的名称将从 pathname

v:present =>参数将作为 array

e:present =>环境将取自 envp argument

2.exec ():

p:不存在=>要运行的程序的名称将从 pathname

l:present =>参数将被传递为 list

e:present =>环境将取自 envp argument

3.exec lp():

p:present =>要运行的程序的名称将从filename指定的名称中获取或系统将search for program file使用PATH变量。

l:present =>参数将被传递为 list

e:不存在=>环境将从 caller's environ

4.exec vp():

p:present =>要运行的程序的名称将从filename指定的名称中获取或系统将search for program file使用PATH变量。

v:present =>参数将作为 array

e:不存在=>环境将从 caller's environ

5.exec v():

p:不存在=>要运行的程序的名称将从 pathname

v:present =>参数将作为 array

e:不存在=>环境将从 caller's environ

6.exec l():

p:不存在=>要运行的程序的名称将从 pathname

l:present =>参数将被传递为 list

e:不存在=>环境将从 caller's environ