Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么时候需要execle而不是execl?_C_Environment Variables_Exec - Fatal编程技术网

什么时候需要execle而不是execl?

什么时候需要execle而不是execl?,c,environment-variables,exec,C,Environment Variables,Exec,考虑这个简单的C程序 #include <stdlib.h> #include <sys/wait.h> #include <unistd.h> int main() { if (fork() == 0) { execl("script.sh", "script.sh", NULL); exit(EXIT_FAILURE); } int status; wait(&status)

考虑这个简单的C程序

#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
    if (fork() == 0)
    {
        execl("script.sh", "script.sh", NULL);
        exit(EXIT_FAILURE);
    }
    int status;
    wait(&status);
    if (WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS)
    {
        return 0;
    }
    return -1;
}
如果我用
gcc-o foo main.C
编译C程序并用

DEBUG=true./foo

然后输出是上的
调试模式,因此脚本实际上获得了我传递给程序
foo
的环境变量,即使我没有使用
execle
。在哪种情况下,有必要使用
execle
(除了希望直接在源代码中指定环境变量之外)?我说的是人们在做这样的事情

extern char **environ;
...
execle(path, path, NULL, environ)

这样做的目的是什么?

当您不希望环境被继承时(您希望可执行文件从一个空环境或您专门为其设置的环境开始)。

因此,您可以使用
execl
而不是底部的示例?我特别是说,是的。将
environ
作为环境参数传递给
execle
相当于调用
execl
extern char **environ;
...
execle(path, path, NULL, environ)