pclose() 返回管道的终止状态是否在所有平台上都向左移动了 8 位?
Does pclose() return pipe's termination status shifted left by eight bits on all platforms?
我在 Centos4 上发现 popen(( 的手册页部分声明:
DESCRIPTION
The pclose() function shall close a stream that was opened by popen(), wait for the command to termi-
nate, and return the termination status of the process that was running the command language inter-
preter. However, if a call caused the termination status to be unavailable to pclose(), then pclose()
shall return -1 with errno set to [ECHILD] to report this situation.
但是,在我的C++应用程序中,当我实际执行代码时,我看到终止状态向左移动了 8 位。 也许这是为了将管道终止状态的 -1 与 pclose(( 自己的退出状态 -1 区分开来?
这是便携式行为吗? 为什么手册页没有提到这一点? 如果不是可移植的,哪些平台符合此行为?
只是为了在上面的 shooper 答案中添加一些代码,您可能想按照以下行做一些事情:
#include <sys/wait.h>
//Get your exit code...
int status=pclose(pipe);
//...and ask how the process ended to clean up the exit code.
if(WIFEXITED(status)) {
//If you need to do something when the pipe exited, this is the time.
status=WEXITSTATUS(status);
}
else if(WIFSIGNALED(status)) {
//If you need to add something if the pipe process was terminated, do it here.
status=WTERMSIG(status);
}
else if(WIFSTOPPED(status)) {
//If you need to act upon the process stopping, do it here.
status=WSTOPSIG(status);
}
除此之外,根据需要添加优雅。
如果你考虑一下,那里有一个"分叉",所以你可能想要"WIFEXITED"和"WEXITSTATUS"。
从手册页:
pclose(( 函数等待关联的进程终止,并返回 wait4(2( 返回的命令的退出状态。
相关文章:
- 使用boost::multiprecision cpp_int左移时出现超时错误
- 警告:左移计数 >= 在 C++ 中将字节流读取为双精度变量时的类型宽度
- 左移<<如何在不同的功能中给出不同的结果?
- 使用 AVX2 指令左移 128 位数字
- <<(按位左移)在 Swift 中的数组上做什么?
- 带有左移操作员C 的意外输出
- 编译器将输出的流运算符<<解释为用于按位左移的二进制运算符<<
- 左移负整数为零是否有意义?
- 在 VS 移位中,UINT64 上的左移(64 >移位> 32)位仅 32 位
- C++20 是否为"overflow"的有符号整数很好地定义了左移?
- C++左移操作
- 为什么编译器左移 0
- 如何在位明智的操作中选择正确的左移
- 为什么很多人使用左移<<而不是乘法
- 左移 0 有什么意义
- 虽然左移运算符 (<<) 在 std::cout 之前使用,但它有什么用?
- 左移<<有些意外的行为
- 使用左移运算符将 F 转换为 FF
- 右移速度与左移相同的速度测试位
- Ruby和C++中的左移16位