1,在程序中,用exit来设置进程的退出值时,虽然该函数的参数类型为int型,但再父进程中只能取到其值的低8位.所以用exit返回值时,高于255的值是没有意义的.

2,对于system函数,返回值是由两部分组成的,低8位值表示所执行的脚本在执行过程中所接收到的信号值,其余的位表示的脚本exit退出时所设置的值,

即脚本内exit退出是的值的低8位,在system返回值的低9-16位.

原文:http://zhangwenxin82.blog.163.com/blog/static/1145959562010323103241387/

包含文件
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
先写一个被调用的函数
==================================
#include <iostream>
int main()
{
printf("Return 10.\n");
return 10;
}
==================================
编译后生成一个"rt"的可执行文件
运行结果
==================================
Return 10.
==================================
再写一个调用system的程序
==================================
#include <stdio.h>;
#include <stdlib.h>;
#include <sys/wait.h>;
#include <sys/types.h>;

int main()
{

pid_t status ;

int errno = 0 ;
status = system("./rt") ;

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)) ;
}else
printf("cp exit illegal![%d]\n", errno) ;
}
~
[tiantao@probe sys_test]$ cat sys_test.cpp
#include <stdio.h>;
#include <stdlib.h>;
#include <sys/wait.h>;
#include <sys/types.h>;

int main()
{

pid_t status ;

int errno = 0 ;
status = system("./rt") ;

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)) ;
}else
printf("cp exit illegal![%d]\n", errno) ;
}

==================================
编译后运行结果
==================================
Return 10.
wifexited(status):1
WEXITSTATUS(status):10
cp exit normal![0]
exit staus = [A]
==================================
可以看到:
WEXITSTATUS(status)可以得到调用程序的返回值。

system函数,WIFEXITED和WEXITSTATUS相关推荐

  1. Linux下使用system()函数一定要谨慎

    转载自:http://my.oschina.net/renhc/blog/53580 linux尽量避免使用system. 曾经的曾经,被system()函数折磨过,之所以这样,是因为对system( ...

  2. system函数和fork-exec机制

    system函数 使用man system查看帮助如下: NAMEsystem -execute a shell commandSYNOPSIS#include<stdlib.h>ints ...

  3. linux下system函数的深入理解

    这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以为 ...

  4. exec族函数、system函数、popen函数、PATH

    exec族函数函数的作用: 我们用fork函数创建新进程后,经常会在新进程中调用exec函数去执行另外一个程序.当进程调用exec函数时,该进程被完全替换为新程序(在exec都后面的代码不会被得到执行 ...

  5. linux系统编程之进程(七):system()函数使用

    一,system()理解 功能:system()函数调用"/bin/sh -c command"执行特定的命令,阻塞当前进程直到command命令执行完毕 原型: int syst ...

  6. 【C/C++】Linux下使用system()函数一定要谨慎

    曾经的曾经,被system()函数折磨过,之所以这样,是因为对system()函数了解不够深入.只是简单的知道用这个函数执行一个系统命令,这远远不够,它的返回值.它所执行命令的返回值以及命令执行失败原 ...

  7. linux系统编程之进程(七):system()函数使用【转】

    本文转载自:http://www.cnblogs.com/mickole/p/3187974.html 一,system()理解 功能:system()函数调用"/bin/sh -c com ...

  8. system函数的返回值和执行脚本的返回值

    1.先统一两个说法: (1)system返回值:指调用system函数后的返回值,比如上例中status为system返回值 (2)shell返回值:指system所调用的shell命令的返回值,比如 ...

  9. Linux system函数返回值

    例: status=system("./test.sh"); 1.先统一两个说法: (1)system返回值:指调用system函数后的返回值,比如上例中status为system ...

最新文章

  1. FauxPas试用时间破解
  2. opencv 直线检测笔记
  3. word设置不同页脚
  4. python小技巧-基于python本身
  5. JZOJ 1980. 【2011集训队出题】Construct
  6. 深度学习笔记第三门课 结构化机器学习项目 第二周 机器学习(ML)策略(2)...
  7. 针对新手的Java EE7和Maven项目–第2部分–为我们的应用程序定义一场简单的战争...
  8. r语言pls分析_R语言:生存分析
  9. UBUNTU安装 SSH 服务
  10. python半径为2.11的圆球的体积_python 学习笔记 11 -- 使用参数使你的程序变得更性感...
  11. socket长连接server c语言,使用socket建立长连接
  12. 计算机专业毕业设计资料免费下载
  13. 服务器 启动多个nginx_Nginx工作原理和优化总结
  14. 知乎在港上市破发是必然:周源被“打脸”,增长极其依赖营销
  15. 工作中使用到的单词(软件开发)_2022-06-01备份
  16. 黎曼 zeta 函数与黎曼猜想
  17. 新手必备:网关与网段、IP协议、主机与DNS 、访问控制 资料汇总110篇
  18. storm tread 耐克_耐克 Nike Air Zoom Pegasus 35 Shield 登月35代跑鞋
  19. 关系型数据库设计——银行业务管理系统
  20. 2021年上半年软考真题及答案解析汇总

热门文章

  1. 苦尽甘来——Ceph中国社区2016
  2. Micro Metro UI风格介绍
  3. 科研学习过程中的收获
  4. 研发工程师笔试c语言题,研发工程师面试题(含答案)
  5. vite打包类库的时候,总包和子包区分打包,怎么打包出来的都是总包的内容
  6. android进入应用黑屏原因以及解决方案
  7. 吸血鬼猎人巴菲第一至八季/全集Buffy迅雷下载
  8. 计算机组成原理测试习题集3
  9. 前端之SEO搜索引擎优化
  10. 关于电脑上耳机听不见歌词的问题(win7)