为什么我们在 execlp() 的前 2 个参数中传递相同的值

why we pass same value in first 2 argument of execlp()

函数声明如下:

int execlp(const char *file, const char *arg, ...);

以下为参数说明:

file: The executable that has to be executed by  the new process. This executable is searched for in the path specified by the environmental variable PATH. 

*arg,...: list of arguments terminated by a NULL argument.

那为什么我们这样调用函数:

execlp("ls","ls",NULL);

会不会变成"ls -ls",什么意思?

第一个是加载执行的二进制文件。第二个是进程的名称(例如显示在 ps 的输出中)。它们是相同的很常见,但没有必要。

因此,在 exec*() 执行的二进制文件中,您为进程提供的名称可用作 argv[0]

来自 this 关于 execlp 的文档(我添加的粗体)

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-terminated strings that represent the argument list available to the executed program. The first argument, by convention, should point to the filename associated with the file being executed. The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.

不,第一个参数是将调用的可执行文件的名称。例如,在许多 UNIX 系统中,您将拥有指向单个可执行文件的符号链接,然后可执行文件可以确定它是如何被调用的。一个例子是 BusyBox,它是一个二进制文件,但它包含许多具有功能价值的命令。

通常虽然名称与可执行文件相同,但大多数可执行文件不关心名称是什么。

因此,例如在 BusyBox 的情况下,如果您调用 execlp("/path/to/busybox", "ls", NULL);,它的行为将与命令 ls 的行为相同,如果您调用 execlp("/path/to/busybox", "ps", NULL);,它的行为将与 [=13] 相同=] 命令。与符号链接相同的是 execlp("/path/to/ps", "ps", NULL);