/dev/null ") != 0); 在上面的代码中,我了解到正在创建一个名为 junk 的新目录,并且标准错误正在被重定向到空设备。我的问题是,只要-6ren">
gpt4 book ai didi

对系统调用逻辑感到困惑

转载 作者:行者123 更新时间:2023-11-30 16:36:14 26 4
gpt4 key购买 nike

if(system("mkdir junk 2>/dev/null ") != 0);

在上面的代码中,我了解到正在创建一个名为 junk 的新目录,并且标准错误正在被重定向到空设备。我的问题是,只要目录垃圾存在,这行代码就会返回非零值吗?

最佳答案

查看 MAN 页面。

The return value of system() is one of the following:

   *  If command is NULL, then a nonzero value if a shell is available,
or 0 if no shell is available.

* If a child process could not be created, or its status could not
be retrieved, the return value is -1.

* If a shell could not be executed in the child process, then the
return value is as though the child shell terminated by calling
_exit(2) with the status 127.

* If all system calls succeed, then the return value is the
termination status of the child shell used to execute command.
(The termination status of a shell is the termination status of
the last command it executes.)

因此,您可以获得零值,表示没有可用的 shell。这意味着该命令可能永远不会执行,并且您可能会得到零。

执行此操作的方法是进行系统调用来创建目录。

关于对系统调用逻辑感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48593774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com