C 和 C++ 中使用的 exec 有哪些不同版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5769734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the different versions of exec used for in C and C++?
提问by node ninja
These are all the versions of exec that can be used in C (and C++)
这些是可以在 C(和 C++)中使用的所有 exec 版本
execl
execle
execlp
execv
execve
execvp
What's the difference between them? How do you know which one to use?
它们之间有什么区别?你怎么知道用哪一个?
回答by Christo
The differences are combinations of:
不同之处在于以下组合:
L vs V: whether you want to pass the parameters to the exec'ed program as
- L: individual parameters in the call (variable argument list):
execl()
,execle()
,execlp()
, andexeclpe()
- V: as an array of char*
execv()
,execve()
,execvp()
, andexecvpe()
The array format is useful when the number of parameters that are to be sent to the exec'ed process are variable -- as in not known in advance, so you can't put in a fixed number of parameters in a function call.
- L: individual parameters in the call (variable argument list):
E: The versions with an 'e' at the end let you additionally pass an array of char* that are a set of strings added to the spawned processes environment before the exec'ed program launches. Yet another way of passing parameters, really.
P: The versions with 'p' in there use the environment variable
PATH
to search for the executable file named to execute. The versions without the 'p' require an absolute or relative file path to be prepended to the filename of the executable if it is not in the current working directory.
L vs V:是否要将参数传递给已执行的程序作为
- 大号:在呼叫个体参数(变量参数列表): ,
execl()
,execle()
,execlp()
和execlpe()
- V:为char *的阵列
execv()
,execve()
,execvp()
,和execvpe()
当要发送到 exec 进程的参数数量是可变的时,数组格式很有用——因为事先不知道,所以你不能在函数调用中放入固定数量的参数。
- 大号:在呼叫个体参数(变量参数列表): ,
E:末尾带有 'e' 的版本允许您额外传递一个 char* 数组,这些字符串是在 exec'ed 程序启动之前添加到生成的进程环境中的一组字符串。确实是另一种传递参数的方式。
P: 带有'p'的版本使用环境变量
PATH
来搜索命名为执行的可执行文件。如果可执行文件不在当前工作目录中,则不带“p”的版本需要在可执行文件的文件名前添加绝对或相对文件路径。
回答by IanNorton
Opengroup are one of the best general references for core c/c++ functions.
Opengroup 是核心 c/c++ 函数的最佳通用参考之一。
The docs for exec* are here: http://pubs.opengroup.org/onlinepubs/009695399/functions/environ.html
exec* 的文档在这里:http: //pubs.opengroup.org/onlinepubs/009695399/functions/environ.html
回答by bruziuz
It's Posix extension of C runtime library. If official Posix documentation is insufficiently then I can recomend book - Samuel P. Harbison, Guy L.Steele, 2002 "C A Reference" Page# 416 - cover that question.
它是 C 运行时库的 Posix 扩展。如果官方 Posix 文档不够充分,那么我可以推荐本书 - Samuel P. Harbison, Guy L.Steele, 2002 "CA Reference" Page# 416 - 涵盖该问题。