对系统调用逻辑感到困惑

时间:2018-02-03 03:54:18

标签: c operating-system

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

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

1 个答案:

答案 0 :(得分:0)

查看MAN页面。

  

system()的返回值是以下之一:

   *  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的零值。这意味着命令可能永远不会执行,你可能会得到零。

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