如何获取system()系统调用的子命令返回值

系统库函数system()通常用来调用一个外部命令;这是一个同步调用,函数会一直等待外部命令执行结束才返回,调用者然后检查结果是否正确;比如:
if (system(...) == 0) or
if (system(...) != -1)
是两种常见的检查运行结果是否正确的做法。

但是要想得到子命令的返回值信息,需要一些额外的处理代码。
下面是man system对返回值的描述:

RETURN VALUE
       The value returned is -1 on error (e.g.  fork(2) failed), and the return status of the command other-
       wise.  This latter return status is in the format specified in wait(2).  Thus, the exit code  of  the
       command  will be WEXITSTATUS(status).  In case /bin/sh could not be executed, the exit status will be
       that of a command that does exit(127).

       If the value of command is NULL, system() returns non-zero if the shell is  available,  and  zero  if
       not.

可以看到system()的返回值并不是命令行子进程的返回值,其中也包括系统调用出错的错误信息,system()的内部实现代码可能就会出错,比如fork()函数就出错了,在这种情况下,子进程都没有被调用起来,因此system()的返回值肯定不是命令行子进程的返回码;另外如果命令行子进程意外终止,比如收到外部信号了,甚至crash了,system()的返回值也肯定不是子进程的正常返回值。

那么如何才能准确的到命令行子进程的返回值呢?有两个宏可以用来判断:

WIFEXITED(ret)

Returns true if the child process exited normaly, the next step is to examine the actual return value using WEXITSTATUS(ret).

WIFSIGNALED(ret)

Returns true child was terminated abnormally (a signal was raised), If WIFSIGNALED(ret) is true, WTERMSIG(ret) evaluates to the number of the signal that caused the termination of the process.

完整的判断代码如下

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
    int ret = system("your_external_command_line");
    if (ret == -1) {
        printf("command subprocess is not launched\n");
    }
    else if (WIFEXITED(ret)) {
        printf("command subprocess is normally terminated, exit status = %d\n", WEXITSTATUS(ret));
    }
    else if (WIFSIGNALED(ret)) {
        printf("command subprocess is abnormally terminated, signal number = %d\n", WTERMSIG(ret));
    }
    else {
        printf("command subprocess is abnormally terminated\n");
    }
    return 0;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,773评论 19 139
  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 14,748评论 0 38
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,844评论 0 17
  • 2016.10.17 你总善于追忆往事 无论是你的糟糕的失败 还是你喜悦的成就 你总在和朋友聊天时 提起过去的你 ...
    读娘阅读 2,795评论 0 0
  • 这是亿万光年中的平常午后 天上又下起了雨 坐在屋内的地板上 想起时光的点点滴滴 这场风雨将我和世界隔绝 在自己的天...
    金铃子阅读 1,748评论 11 4