什么时候需要execle而不是execl?
考虑这个简单的C程序什么时候需要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)
#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)