execl() 的参数传递到哪里?
where does the arguments of execl() passed to?
在UNIX环境下的系统编程上下文中,在使用C++编程语言时,在我的理解中,execl()会传入程序的路径运行,以及一个vector。当传入该向量时,我将其理解为传入入口点,通常是 main 函数。在一个main函数中,我理解我的参数可以写成:
int main(int argc, int* argv[]){ return 0; }
考虑到上述情况,当参数被传递到 execl() 时,在我看来它并没有直接传递到主函数中。
是否有 "processing" 阶段将 execl() 的参数更改为整数数据类型和数组?
同时,如果我的理解有任何错误,欢迎指正。
您为 main
函数提供的签名不正确。应该是:
int main(int argc, char *argv[]);
或:
int main(void);
至于参数,传递给 execl
的参数应该与被调用程序接收的参数相匹配。
例如,如果程序 A 像这样执行程序 B:
execl("/path/to/progB", "progB", "-a", "1", "-x", "hello", "command", (char *)NULL);
然后在程序 B 中,argc
将是 6,而 argv
基本上是:
{ "progB", "-a", "1", "-x", "hello", "command" }
main
接受一个 char*argv[]
参数(不是 int *argv[]
),就像通常的 core exec 系统调用通常做的那样。在 Linux 上,系统调用是 execve
(需要 char*[]
)并且所有其他 exec* 函数都是根据它实现的。
至于 execl
,参数列表需要以 NULL 结尾,这允许您对参数进行计数,然后将它们复制到一个数组中,该数组向下传递给 execve
。
musl libc 库相当直接地做到了这一点:https://git.musl-libc.org/cgit/musl/tree/src/process/execl.c
你的理解有误。从这个 documentation 参数列表总是一些空终止的 char*
指针(强调我的):
execl(<shell path>, arg0, file, arg1, ..., (char *)0);
where is an unspecified pathname for the sh utility, file is the >> process image file, and for execvp(), where arg0, arg1, and so on correspond to the values passed to execvp() in argv[0], argv[1], and so on.
The arguments represented by arg0,... are pointers to null-terminated character strings. These strings shall constitute the argument list available to the new process image. The list is terminated by a null pointer. The argument arg0 should point to a filename string that is associated with the process being started by one of the exec functions.
所以这与您声称的不符:
int main(int argc, int* argv[]){ return 0; }
// ^^^^
在UNIX环境下的系统编程上下文中,在使用C++编程语言时,在我的理解中,execl()会传入程序的路径运行,以及一个vector。当传入该向量时,我将其理解为传入入口点,通常是 main 函数。在一个main函数中,我理解我的参数可以写成:
int main(int argc, int* argv[]){ return 0; }
考虑到上述情况,当参数被传递到 execl() 时,在我看来它并没有直接传递到主函数中。
是否有 "processing" 阶段将 execl() 的参数更改为整数数据类型和数组?
同时,如果我的理解有任何错误,欢迎指正。
您为 main
函数提供的签名不正确。应该是:
int main(int argc, char *argv[]);
或:
int main(void);
至于参数,传递给 execl
的参数应该与被调用程序接收的参数相匹配。
例如,如果程序 A 像这样执行程序 B:
execl("/path/to/progB", "progB", "-a", "1", "-x", "hello", "command", (char *)NULL);
然后在程序 B 中,argc
将是 6,而 argv
基本上是:
{ "progB", "-a", "1", "-x", "hello", "command" }
main
接受一个 char*argv[]
参数(不是 int *argv[]
),就像通常的 core exec 系统调用通常做的那样。在 Linux 上,系统调用是 execve
(需要 char*[]
)并且所有其他 exec* 函数都是根据它实现的。
至于 execl
,参数列表需要以 NULL 结尾,这允许您对参数进行计数,然后将它们复制到一个数组中,该数组向下传递给 execve
。
musl libc 库相当直接地做到了这一点:https://git.musl-libc.org/cgit/musl/tree/src/process/execl.c
你的理解有误。从这个 documentation 参数列表总是一些空终止的 char*
指针(强调我的):
execl(<shell path>, arg0, file, arg1, ..., (char *)0);
where is an unspecified pathname for the sh utility, file is the >> process image file, and for execvp(), where arg0, arg1, and so on correspond to the values passed to execvp() in argv[0], argv[1], and so on.
The arguments represented by arg0,... are pointers to null-terminated character strings. These strings shall constitute the argument list available to the new process image. The list is terminated by a null pointer. The argument arg0 should point to a filename string that is associated with the process being started by one of the exec functions.
所以这与您声称的不符:
int main(int argc, int* argv[]){ return 0; } // ^^^^