对系统调用逻辑感到困惑

标签 c operating-system

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/

相关文章:

c - 初始化二维数组的函数

c - 是否有描述 C 函数名称 "stand for"的文档(不是它们的作用——而是缩写名称的含义)?

operating-system - 什么是缓存大小和缓存行大小?

C 'restrict' 基于关键字的优化

java - 在 JNI 中传递对象总是返回 NULL

cURL 无法解析主机,但 Web 浏览器可以访问它

我可以获得 DOS 6.22 操作系统的 IP 地址吗?

android - 即使应用程序被杀死,也会在特定时间从 Android 操作系统获取通知

c - 如何更精确地衡量上下文切换的成本

java - 除了底层操作系统之外,Java 是否对文件名施加了进一步的限制?