简体   繁体   English

system()调用在C中返回错误值

[英]system() call is returning wrong value in C

I am getting a -1 return value, even after running a command successfully via system() call. 即使通过system()调用成功运行了命令,我仍得到-1的返回值。 Please see the following C code for more information. 请参阅以下C代码以获取更多信息。

system("ping -w 3 -c 1 -q -I 63.254.203.24 63.254.203.26 >/dev/null");

and then I am checking the return value of this system() call, even though it is pingable but I found -1 as a return value. 然后我正在检查此system()调用的返回值,即使它可以ping通,但我发现-1作为返回值。

to see the actual return value use WEXITSTATUS(n) and the reason why system doesn't return the actual value is because (and this is only valid for linux) it is implemented using fork , exec and wait the later returns the status of the process which contains whether the process ended normally or because of a signal plus the actual return value , and to access those different values you'll have to use macro's such as WEXITSTATUS(n) these macros are mentioned in man 3 wait 要查看实际的返回值,请使用WEXITSTATUS(n) ,而system不返回实际值的原因是(并且这仅对linux有效)它是使用forkexec实现的,并wait稍后返回进程,该进程包含进程是正常结束还是由于信号加实际返回值而结束,并且要访问那些不同的值,您将必须使用诸如WEXITSTATUS(n)宏,这些宏在man 3 wait被提及

The section RETURN VALUE of man system : 手动man system返回值部分:

The value returned is -1 on error (eg, fork(2) failed), and the return status of the command otherwise. 错误时返回的值为-1(例如,fork(2)失败),否则返回命令的返回状态。 This latter return status is in the format specified in wait(2). 后一种返回状态采用wait(2)中指定的格式。 Thus, the exit code of the command will be WEXITSTATUS(status) . 因此,该命令的退出代码将为WEXITSTATUS(status) In case /bin/sh could not be executed, the exit status will be that of a command that does exit(127) . 如果/ bin / sh无法执行,则退出状态将是exit(127)的命令的状态。

Fortunately, I got the answer. 幸运的是,我得到了答案。 it was fork failed due to memory failure. 由于内存故障,它被派生失败。 It is not a good idea to use system() call. 使用system()调用不是一个好主意。

Thanks for the support and answer. 感谢您的支持和答复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM