linux下对system 的简单解析

来源:互联网 发布:日本物价知乎 编辑:程序博客网 时间:2024/05/17 01:33
函数说明
system()会调用fork()产生子进程,由子进程来调用/bin/sh -c string来执行参数string字符串所代表的命令,此命令执行完后随即返回原调用的进程。


返回值
=-1: 出现错误  
=0: 调用成功但是没有出现子进程  
>0: 成功退出的子进程的id
如果system()在调用/bin/sh时失败则返回127,其他失败原因返回-1。
若参数string为空指针(NULL),则返回非零值。
如果system()调用成功则最后会返回执行shell命令后的返回值,但是此返回值也有可能为 system()调用/bin/sh失败所返回的127,因此最好能再检查errno来确认执行成功。




WIFSIGNALED(ret)  子进程正常退出(调用成功)的情况下此宏值为真

检查返回值:

<span style="font-size:18px;">int ret = system("ls -al /etc/passwd /etc/shadow");int ret = system("ls -l");if(WIFSIGNALED(ret)){printf("正常退出\n");}else{printf("异常退出\n");}</span>



system函数对返回值的处理,涉及3个阶段:
阶段1:创建子进程等准备工作。如果失败,返回-1。
阶段2:调用/bin/sh拉起shell脚本,如果拉起失败或者shell未正常执行结束(参见备注1),原因值被写入到status的低8~15比特位中。system的man中只说明了会写了127这个值,但实测发现还会写126等值。
阶段3:如果shell脚本正常执行结束,将shell返回值填到status的低8~15比特位中。
备注1:
只要能够调用到/bin/sh,并且执行shell过程中没有被其他信号异常中断,都算正常结束。
比如:不管shell脚本中返回什么原因值,是0还是非0,都算正常执行结束。即使shell脚本不存在或没有执行权限,也都算正常执行结束。
如果shell脚本执行过程中被强制kill掉等情况则算异常结束。


如何判断阶段2中,shell脚本是否正常执行结束呢?系统提供了宏: WIFEXITED(status)。如果 WIFEXITED(status)为真,则说明正常结束。
如何取得阶段3中的shell返回值?你可以直接通过右移8bit来实现,但安全的做法是使用系统提供的宏: WEXITSTATUS(status)。


写一个调用system的程序

<span style="font-size:18px;">==================================#include <stdio.h>#include <stdlib.h>#include <sys/wait.h>#include <sys/types.h>int main()  {  pid_t          status;  int            errno = 0;  status = system("ls -l");  printf("wifexited(status):%d/n", WIFEXITED(status));  printf("WEXITSTATUS(status):%d/n", WEXITSTATUS(status));  if (status == -1) {  printf("system error!");  }  if (WIFEXITED(status)) {  printf("cp exit normal![%d]/n", errno);  printf("exit staus = [%X]/n", WEXITSTATUS(status));  } elseprintf("cp exit illegal![%d]/n", errno);return 0;}  </span>

==================================
编译后运行结果
==================================


wifexited(status):1
WEXITSTATUS(status):10
cp exit normal![0]
exit staus = [A]
==================================

可以看到:
WEXITSTATUS(status)可以得到调用程序的返回值。



总结:

对于system的返回值

1、status == -1 执行失败

2、WIFEXITED(status) !=1 执行失败 (正常情况下应该是1)

3、WEXITSTATUS(status) != 0 执行失败(正常情况下 应该是0)



0 0