【问题标题】:How to use execl?如何使用执行?
【发布时间】:2021-11-30 04:55:50
【问题描述】:

我看到execl的以下用法:

execl("/bin/ls", "ls", "-l", "-R", "-a", NULL);

我想知道为什么第一个参数是ls,它已经在第一个参数中了为什么还要加上呢?

可以为 NULL 吗?有什么影响?

【问题讨论】:

  • 我想知道你为什么没有看到它的文档,它已经在那里了,为什么还要再问呢?可以试试吗?有什么影响?
  • pathname 之后的第一个参数是程序本身(在这种情况下为/bin/ls)将在argv[0] 中看到的内容。如果您将其设为NULL,它将充当argv 数组中的终止符。
  • 可执行文件名与argv[0]传递的内容之间不需要任何关系。使用execl("/bin/ls", "elephant", "-l", "does-not-exist", (char *)0); 是合法的(但很奇怪)。当我运行它时,我收到错误消息elephant: does-not-exist: No such file or directoryls 程序被告知它的名字是elephant,它在错误消息中使用了它。

标签: c++ c exec


【解决方案1】:

您可以像下面这样查看 execl 的 man 文档。

int execl(const char *path, const char *arg, ... /* (char  *) NULL */);

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.

第一个参数指向与正在执行的文件关联的文件名。

BR,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 2021-11-28
    • 1970-01-01
    • 2014-02-07
    • 2011-08-05
    • 2011-08-17
    相关资源
    最近更新 更多