背景:工作中遇到一个system函数执行udhcp失败返回256的问题,一开始以为这个函数有问题,
经过一番分析,查到system返回256,相当于shell命令返回值为1
std::system - cppreference.comhttps://en.cppreference.com/w/cpp/utility/program/system
返回值可以使用WEXITSTATUS 解析的
Return value
Implementation-defined value. If
command
is a null pointer, returns a nonzero value if and only if the command processor exists.Notes
On POSIX systems, the return value can be decomposed using WEXITSTATUS and WSTOPSIG
The related POSIX function popen makes the output generated by
command
available to the caller.An explicit flush of std::cout is also necessary before a call to std::system, if the spawned process performs any screen I/O.
waithttps://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html
WEXITSTATUS(stat_val)
If the value of WIFEXITED(stat_val) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main().
这儿意思是拿低8位,猜测应该有可能有大小端?正好256,拿高八位 为1,暂时这么理解吧
- ls: 无法访问/noexist: 没有那个文件或目录
- libin@libin:~/program/C/Linux/system$ echo $?
- 2
- libin@libin:~/program/C/Linux/system$
我们看到了,虽然/noexist文件并不存在,ls这条命令执行出了错,但是仍然属于shell顺利执行完毕。 ls /noexist的错误吗是2,所以,system函数的返回值为 2*256 = 512.