我在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?
这是便携行为吗?为什么手册页没有提到这个?如果不可移植,哪些平台符合这种行为?
答案 0 :(得分: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);
}
除此之外,请根据需要添加优雅。
答案 1 :(得分:0)
如果你考虑一下,就会有一个&#34; fork&#34;在那里,所以你可能想要&#34; WIFEXITED&#34;和&#34; WEXITSTATUS&#34;。
从手册页:
pclose()函数等待关联的进程终止并返回wait4(2)返回的命令的退出状态。