在 execlp() 系统调用中,为什么 arg0 参数必须指向与正在启动的进程关联的文件名?
In execlp() system call why the arg0 argument must point to a filename that's associated with the process being started?
我正在阅读 Galvin 等人的恐龙书。阿尔。在那里我遇到了 fork()
系统调用的下图。
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid;
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
execlp("/bin/ls","ls",NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait(NULL);
printf("Child Complete");
}
return 0;
}
文字说:
After a fork()
system call, one of the two processes typically uses the exec()
system call to replace the process’s memory space with a new program. The exec()
system call loads a binary file into memory (destroying the memory image of the program containing the exec()
system call) and starts its execution.
所以在上面的这个例子中:
The child process then overlays its address space with the UNIX command /bin/ls
(used to get a directory listing) using the execlp()
system call (execlp()
is a version of the exec()
system call).
Here据说:
#include <unistd.h>
int execlp( const char * file,
const char * arg0,
const char * arg1,
…
const char * argn,
NULL );
file:用于构造标识新过程映像文件的路径名。如果文件参数包含斜杠字符,则文件参数用作文件的路径名。否则,此文件的路径前缀是通过搜索作为环境变量 PATH 传递的目录获得的。
arg0, …, argn :指向以 NULL 结尾的字符串的指针。这些字符串构成了可用于新过程映像的参数列表。您必须使用 NULL 指针终止列表。 arg0 参数必须指向与正在启动的进程关联的文件名,并且不能为 NULL。
谁能解释一下 execlp() 的 3 个参数在做什么?更具体地说,为什么 arg0
必须与正在启动的进程同名?网络搜索告诉我第一个参数是文件名(或路径名),其余的可以被视为指向空终止字符串的指针,这些字符串作为文件的参数。
我不明白的是为什么我们将 ls
作为参数传递给二进制文件夹中存在的 ls
程序?使用
在 Linux 终端上工作时
$
在提示中。只需打字
$ ls
然后按回车键就可以了……我的意思是我们不给。
$ ls ls
它类似于 C 程序接受命令行参数的方式吗?
int main(int argc,char* argv){
...
}
运行 上述程序对应的二进制为:
$ ./a.out xyz pqr
有argv[0]="./a.out"
和argv[1]="xyz"
和argv[2]="pqr"
。 ./a.out
是二进制文件 a.out
的参数吗?但是使用 ./a.out
我们实际上是在将 Linux 系统引导到 运行 二进制文件。
我去了 here and here,但其中 none 似乎直接回答了我的问题。
按照惯例,arg0
参数是可执行文件的名称 运行。然而,这不是必需的。您可以为此参数传递任何字符串。
我正在阅读 Galvin 等人的恐龙书。阿尔。在那里我遇到了 fork()
系统调用的下图。
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid;
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
execlp("/bin/ls","ls",NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait(NULL);
printf("Child Complete");
}
return 0;
}
文字说:
After a
fork()
system call, one of the two processes typically uses theexec()
system call to replace the process’s memory space with a new program. Theexec()
system call loads a binary file into memory (destroying the memory image of the program containing theexec()
system call) and starts its execution.
所以在上面的这个例子中:
The child process then overlays its address space with the UNIX command
/bin/ls
(used to get a directory listing) using theexeclp()
system call (execlp()
is a version of theexec()
system call).
Here据说:
#include <unistd.h>
int execlp( const char * file,
const char * arg0,
const char * arg1,
…
const char * argn,
NULL );
file:用于构造标识新过程映像文件的路径名。如果文件参数包含斜杠字符,则文件参数用作文件的路径名。否则,此文件的路径前缀是通过搜索作为环境变量 PATH 传递的目录获得的。
arg0, …, argn :指向以 NULL 结尾的字符串的指针。这些字符串构成了可用于新过程映像的参数列表。您必须使用 NULL 指针终止列表。 arg0 参数必须指向与正在启动的进程关联的文件名,并且不能为 NULL。
谁能解释一下 execlp() 的 3 个参数在做什么?更具体地说,为什么 arg0
必须与正在启动的进程同名?网络搜索告诉我第一个参数是文件名(或路径名),其余的可以被视为指向空终止字符串的指针,这些字符串作为文件的参数。
我不明白的是为什么我们将 ls
作为参数传递给二进制文件夹中存在的 ls
程序?使用
$
在提示中。只需打字
$ ls
然后按回车键就可以了……我的意思是我们不给。
$ ls ls
它类似于 C 程序接受命令行参数的方式吗?
int main(int argc,char* argv){
...
}
运行 上述程序对应的二进制为:
$ ./a.out xyz pqr
有argv[0]="./a.out"
和argv[1]="xyz"
和argv[2]="pqr"
。 ./a.out
是二进制文件 a.out
的参数吗?但是使用 ./a.out
我们实际上是在将 Linux 系统引导到 运行 二进制文件。
我去了 here and here,但其中 none 似乎直接回答了我的问题。
按照惯例,arg0
参数是可执行文件的名称 运行。然而,这不是必需的。您可以为此参数传递任何字符串。