system/WIFEXITED/WEXITSTATUS函数-linux

来源:互联网 发布:建筑电气常用数据 编辑:程序博客网 时间:2024/06/07 13:00
system:
感性认识:
systerm两层含义:
1、正确退出后。还需要再判断,操作成功或者操作失败。
2、错误退出。


[cpp] view plain copy
  1. #include <stdio.h>    
  2. #include <stdlib.h>    
  3. #include <sys/wait.h>    
  4. #include <sys/types.h>    
  5.     
  6. int main()    
  7. {    
  8.     pid_t status;    
  9.     
  10.     
  11.     status = system("./test.sh");    
  12.     
  13.     if (-1 == status)    
  14.     {    
  15.         printf("system error!");    
  16.     }    
  17.     else    
  18.     {    
  19.         printf("exit status value = [0x%x]\n", status);    
  20.     
  21.         if (WIFEXITED(status))  //正确退出  
  22.         {    
  23.             if (0 == WEXITSTATUS(status)) //操作成功  
  24.             {    
  25.                 printf("run shell script successfully.\n");    
  26.             }    
  27.             else  //操作失败  
  28.             {    
  29.                 printf("run shell script fail, script exit code: %d\n", WEXITSTATUS(status));    
  30.             }    
  31.         }    
  32.         else  //错误退出  
  33.         {    
  34.             printf("exit status = [%d]\n", WEXITSTATUS(status));    
  35.         }    
  36.     }    
  37.     
  38.     return 0;    
  39. }   

下面更详细解释:

1、先统一两个说法:

(1)system 返回值:指调用system函数后的返回值,比如上例中status为system返回值

(2)shell 返回值:指system所调用的shell命令的返回值,比如上例中,test.sh中返回的值为shell返回值。


2、如何正确判断test.sh是否正确执行

都错!(仅仅判断status是否==0?或者仅判断status是否!=-1? )


3、man中对于system的说明

RETURN VALUE

       The value returned is -1 on error (e.g.  fork() failed), and the return

       status  of  the command otherwise.  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).

看得很晕吧?


4、system函数对返回值的处理

阶段1:

创建子进程等准备工作。如果失败,返回-1。

阶段2:

调用/bin/sh拉起shell脚本,如果拉起失败或者shell未正常执行结束(参见备注1),原因值写入到status的低8~15比特位中。

如何判断阶段2中,shell脚本是否正常执行结束呢?系统提供了宏:WIFEXITED(status)。如果WIFEXITED(status)为真,则说明正常结束。

阶段3:

如果shell脚本正常执行结束,将shell返回值填到status的低8~15比特位中。

如何取得阶段3中的shell返回值?你可以直接通过右移8bit来实现,但安全的做法是使用系统提供的宏:WEXITSTATUS(status)

备注1:

只要能够调用到/bin/sh,并且执行shell过程中没有被其他信号异常中断,都算正常结束

比如:

不管shell脚本中返回什么原因值,是0还是非0,都算正常执行结束。即使shell脚本不存在或没有执行权限,也都算正常执行结束。

如果shell脚本执行过程中被强制kill掉等情况则算异常结束。


英文说明:
WIFEXITED(stat_val) Evaluates to a non-zero value if status
was returned for a child process that
terminated normally.

WEXITSTATUS(stat_val) If the value of WIFEXITED(stat_val) is
non-zero, this macro evaluates to the
low-order 8 bits of the status argument
that the child process passed to _exit()
or exit(), or the value the child
process returned from main().
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 公产房父母去世办公证怎么办 动迁过程中承租人去世了怎么办 公租房的房间带阳台怎么办 公租房合同到期没有社保怎么办 租房合同没有到期违约了怎么办 租房户到期不搬怎么办 公租房摇号摇到了又怎么办 公租房被清退会怎么办 公租房摇不到号怎么办 公租房到期不搬怎么办 租房到期租客不搬怎么办 房产证面积与实际不符怎么办 社保晚交了1天怎么办 个人社保忘交了怎么办 个人社保晚交了怎么办 医保晚交了几天怎么办 辞职后转为灵活就业养老怎么办 公司名称变更提取不了公积金怎么办 五险合一软件已经减员怎么办 法人社保不在投标单位怎么办 换工作单位后社保怎么办 在北京孩子没有一老一小怎么办 深户小孩怎么办社保卡 社保卡没办下来去医院住院怎么办 老年社保卡丢了怎么办 外墙掉瓷砖伤车伤人怎么办 医保卡姓名弄错了怎么办 走工伤和走社保怎么办 公司在朝阳社保在海淀怎么办 公司没缴纳个税怎么办 报个税工资报少了怎么办 医院预约单丢了怎么办 肛瘘手术后太疼怎么办 低位保肛手术后吻合口瘘怎么办 做完痔疮手术后大便困难怎么办 20岁长痔疮了怎么办 孕妇痔疮痒的难受怎么办 痔疮术后伤口不愈合怎么办 剖腹产液化伤口长的慢怎么办 内痔斑痕怎么办了能消化 油条面和稀了怎么办