linux下system返回值
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)
exitedNon 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):%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)可以得到调⽤程序的返回值。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。