Linux Ubuntu 中的 execl() 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12677120/
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
execl() arguments in Ubuntu
提问by user1698102
I am learning linux programming and came across exec function which is kind of very useful. But the problem is exec function arguments are very confusing and I am unable to grasp which argument is for what purpose.. In the following code execl()
function is called from a child created through fork()
, What is the purpose of the last argument (NULL
) in execl()
?
我正在学习 linux 编程并遇到了非常有用的 exec 函数。但问题是 exec 函数参数非常混乱,我无法掌握哪个参数用于什么目的..在下面的代码中,execl()
函数是从创建的子进程调用fork()
的,最后一个参数 ( NULL
)的目的是什么execl()
?
execl("/bin/ls","ls","-l",NULL);
If any one can explain what is the purpose of NULL
argument and other arguments and the purpose of arguments of exec()
family function, It would be a great help to me!
如果有人能解释一下什么是NULL
争论和其他争论的目的以及exec()
家庭功能争论的目的,那对我来说将是一个很大的帮助!
采纳答案by James Kanze
To create undefined behavior. That is not a legal call to execl
. A
correct call might be:
创建未定义的行为。这不是对execl
. 正确的调用可能是:
execl( "/bin/ls", "ls", "-l", (char*)0 );
The last argument mustbe (char*)0
, or you have undefined behavior.
The first argument is the path of the executable. The following
arguments appear in argv
of the executed program. The list of these
arguments is terminated by a (char*)0
; that's how the called function
knows that the last argument has been reached. In the above example,
for example, the executable at "/bin/ls"
will replace your code; in
its main
, it will have argc
equal 2, with argv[0]
equal "ls"
,
and argv[1]
equal "-l"
.
最后一个参数必须是(char*)0
,否则您有未定义的行为。第一个参数是可执行文件的路径。以下参数出现在argv
执行的程序中。这些参数的列表以(char*)0
;结尾。这就是被调用函数如何知道已经到达最后一个参数的方式。例如,在上面的示例中,可执行文件 at"/bin/ls"
将替换您的代码;在它的 中main
,它将具有argc
等于 2、argv[0]
等于"ls"
和argv[1]
等于"-l"
。
Immediately after this function, you should have the error handling
code. (execl
always returns -1, when it returns, so you don't need to
test it. And it only returns if there was some sort of error.)
在这个函数之后,你应该有错误处理代码。(execl
当它返回时总是返回-1,所以你不需要测试它。它只有在出现某种错误时才返回。)
回答by pb2q
The exec
functions are variadic: they take a variable number of parameters so that you can pass a variable number of arguments to the command. The functions need to use NULL
as a marker to mark the end of the argument list.
这些exec
函数是可变参数的:它们采用可变数量的参数,以便您可以将可变数量的参数传递给命令。这些函数需要NULL
用作标记来标记参数列表的结尾。
Within variadic functions is a loop that will iterate over the variable number of arguments. These loops need a terminating condition. In some cases, e.g. printf
, the actual number of arguments can be inferred from another argument. In other functions, NULL
is used to mark the end of the list.
在可变参数函数中是一个循环,它将迭代可变数量的参数。这些循环需要一个终止条件。在某些情况下,例如printf
,可以从另一个参数推断出实际的参数数量。在其他函数中,NULL
用于标记列表的结尾。
Another option would be to add an additional function parameter for number of arguments, but that would make the code a little more brittle, requiring the programmer to manage an additional parameter, rather than simply always using a NULL
as the final argument.
另一种选择是为number of arguments添加一个额外的函数参数,但这会使代码更加脆弱,需要程序员管理一个额外的参数,而不是简单地总是使用 aNULL
作为最终参数。
You'll also see (char *) 0
used as the marker:
您还将看到(char *) 0
用作标记:
execl("/bin/ls", "ls", "-l", (char *) 0);
回答by jrrk
In /usr/include/libio.h
, since gcc 2.8 (a long time ago) NULL
is defined to be null (is reserved for builtins), prior to that NULL
was (void *)0
which is indistinguishable from (char *)0
in a varargs
situation since the type is not passed, the exception being if __cplusplus
is defined in which case NULL
is defined as 0.
在/usr/include/libio.h
,由于GCC 2.8(很久以前)NULL
被定义为空(保留用于建宏),在此之前,NULL
是(void *)0
它是从不可区分(char *)0
的varargs
情况,因为该类型未通过,该异常被如果__cplusplus
是在这种情况下定义NULL
定义为 0。
The safe thing to do especially if you have a 64-bit architecture is to explicitly use (void *)0
which is defined to be compatible with any pointer and not rely on any dodgy #defines
that might happen to be in the standard library.
安全的做法尤其是如果您拥有 64 位架构,则明确使用(void *)0
定义为与任何指针兼容的对象,而不依赖于#defines
标准库中可能出现的任何狡猾之处。
回答by Carlos Davila
The purposeof the ending argument (char *) 0
is to terminate the parameters. Undefined behavior may result if this is missing.
The man page defines the execlsignature as :
结束参数的目的(char *) 0
是终止参数。如果缺少此项,可能会导致未定义的行为。手册页将execl签名定义为:
int execl(const char *path, const char *arg, ...);
path: The location of the program to invoke, the program to invoke.
path: 要调用的程序的位置,要调用的程序。
arg, ...*: can be thought of as arg0, arg1, ..., argn.
arg, ...*: 可以认为是 arg0, arg1, ..., argn。
In your case execl( "/bin/ls", "ls", "-l", (char*)0 );
is the correct function call.
在您的情况下execl( "/bin/ls", "ls", "-l", (char*)0 );
是正确的函数调用。
"bin/ls" the program to invoke
“ bin/ls”要调用的程序
"ls" the program name
“ ls”程序名称
"-l" is the parameter for that program called
“ -l”是那个被调用的程序的参数