前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >system函数,WIFEXITED和WEXITSTATUS

system函数,WIFEXITED和WEXITSTATUS

作者头像
全栈程序员站长
发布2022-11-16 17:57:47
发布2022-11-16 17:57:47
1.2K0
举报

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/227268.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月30日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档