2024年1月15日发(作者:)

linux 下 system 返回值

1.10 What's the return value of system/pclose/waitpid?

1.10 system/pclose/waitpid的返回值是什么?

The return value of system(), pclose(), or waitpid() doesn't seem to be the

exit value of or the exit value is shifted left

what's the deal?

system(),pclose(),或waitpid()的返回值不像是我的进程的退出值……或者退出值被

左移了8位……怎么回事?

/* ??说了什么,我也不知道呀 */

The man page is right, and so are you! If you read the man page for

waitpid() you'll find that the return code for the process is encoded. The

value returned by the process is normally in the top 16 bits, and the rest

is used for other things. You can't rely on this though, not if you want to

be portable, so the suggestion is that you use the macros provided. These

are usually documented under wait() or wstat.

Man page是对的,你也是!如果你阅读了waitpid()的manpage,就会发现进程的返回值

是经过编码的。返回值只有高16位是正常的,剩下的被用作其他用途。

你不可以依赖这个思路,除非你不考虑移植性。建议使用提供的宏。记载在wait()或

wstat的联机手册里。*/[不懂不懂! 不懂不懂!]

Macros defined for the purpose (in `') include (stat is the

value returned by waitpid()):

为此定义的宏(在)包括(stat是waitpid()的返回值):

WIFEXITED(stat)

Non zero if child exited normally.

非零 如果子程序正常退出

WEXITSTATUS(stat)

exit code returned by child

子程序返回exit 值

WIFSIGNALED(stat)

Non-zero if child was terminated by a signal

非零 如果子进程被一个信号结束

WTERMSIG(stat)

signal number that terminated child

结束子进程的信号 number /* signal number 怎么翻译呀*/

WIFSTOPPED(stat)

non-zero if child is stopped

非零 如果子进程被停止

WSTOPSIG(stat)

number of signal that stopped child

停止子进程的signal number.

WIFCONTINUED(stat)

non-zero if status was for continued child

非零 如果状态是继续运行的子进程

WCOREDUMP(stat)

If WIFSIGNALED(stat) is non-zero, this is non-zero if the process left

behind a core dump.

如果WIFSIGNALED(stat)非零,而且进程产生了一个core dump,那么这个也是非零。

[WIFSIGNALED(stat) 和 core dump是否有先后次序?]

我在进程A里用system()调用进程B ..我希望进程A能得到进程B执行出错后对应返回值..

望高人指点一下..

在线等....

网友回复:要分成两部分来说:

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

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

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

Linux中调用 system的返回值

#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):%dn",WIFEXITED(status));

printf("WEXITSTATUS(status):%dn",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_

#include ;

#include ;

#include ;

#include ;

int main()

{

pid_t status ;

int errno = 0 ;

status = system("./rt") ;

printf("wifexited(status):%dn",WIFEXITED(status));

printf("WEXITSTATUS(status):%dn",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)可以得到调用程序的返回值。