加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

system函数,WIFEXITED和WEXITSTATUS

(2013-08-01 16:01:36)

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

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

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

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

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

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 ;
#include ;
#include ;
#include ;

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)可以得到调用程序的返回值。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有