zl程序教程

您现在的位置是:首页 >  其它

当前栏目

system函数,WIFEXITED和WEXITSTATUS

函数 system
2023-06-13 09:15:14 时间

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