如果从管道读取时 tail 失败怎么办
因此,与上面的链接相关,我有正在执行 tail
的子进程,而父进程正在通过管道
读取其输出。
dup2(pipefd[1], STDOUT_FILENO);
dup2(pipefd[1], STDERR_FILENO);
我的问题是,如果 tail
失败,我正在读取的管道会发生什么情况?我能在 stderr
上得到任何信息吗? tail
会自行终止吗?或者它可能会作为已失效
挂在那里?
distinguish stdout from stderr on pipe
So, related to the link above, I have a child who is executing tail
and parent is reading its out put via a pipe
.
dup2(pipefd[1], STDOUT_FILENO);
dup2(pipefd[1], STDERR_FILENO);
My question is, if somehow tail
fails, what happens to the pipe from which I am reading? Do I get anything on stderr
? Does tail
terminate itself? or it may hang in there as defunct
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当
tail
终止时,内核将向管道上的其他进程发送SIGPIPE
信号。该信号的默认操作(如果未安装处理程序)是终止进程。如果您不想处理信号,可以忽略父级中的
SIGPIPE
(因此当tail
终止时它不会终止),而是检查是否每次读取
后,errno
的值为EPIPE
。此外,您必须从父级调用wait
或waitpid
才能获取僵尸子级。The kernel will send a
SIGPIPE
signal to the other process on the pipe whentail
has terminated. The default action for this signal (if a handler is not installed) is to terminate the process.If you don't want to deal with signals, you can ignore
SIGPIPE
in the parent (so it doesn't terminate whentail
has terminated), and instead check whether the value oferrno
isEPIPE
after eachread
. Additionally, you'll have to callwait
orwaitpid
from the parent to reap the zombie child.读时不会得到 EPIPE,只有写时才会返回 EPIPE。您将得到 EOF,由 read 返回 0 指示,并且由于您读取了 stderr,您也会收到错误消息(在 EOF 之前)。
该进程将成为僵尸进程,您可以使用 wait/waitpid 来获取退出状态,如果出现错误,该退出状态将非零。
you don't get EPIPE when reading, only write will return EPIPE. You'll get EOF, indicated by read returning 0, and since you read stderr, you'll get the error message as well (before the EOF).
The process will become a zombie, and you can use wait/waitpid to get the exit status, which will be non-zero if there was an error.
如果 tail 失败,则管道读取端的任何读取都将返回 EOF。如果 tail 失败,则它已经终止,“失败”的定义是它以非零退出状态终止。它将保留在进程表中(即“失效”),直到父进程等待它为止。
但是为什么你让 tail 对 stderr 和 stdout 使用相同的管道呢?为什么不直接制作两根管子呢?看起来这将消除区分两个输出流的问题。
If tail fails, any read on the read end of the pipe will return EOF. If tail fails, it has already terminated, the definition of "fail" being that it terminated with a non-zero exit status. It will remain in the process table (ie, "defunct") until the parent waits for it.
But why are you having tail use the same pipe for both stderr and stdout? Why not just make two pipes? It seems that this would eliminate the problem of distinguishing between the two output streams.