[英]what if tail fails while reading from pipe
distinguish stdout from stderr on pipe 在 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
.因此,与上面的链接相关,我有一个正在执行
tail
的孩子,而父母正在通过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?我的问题是,如果
tail
以某种方式失败,我正在阅读的 pipe 会发生什么? Do I get anything on stderr
?我在
stderr
上得到什么吗? Does tail
terminate itself? tail
会自行终止吗? or it may hang in there as defunct
?或者它可能挂在那里
defunct
?
The kernel will send a SIGPIPE
signal to the other process on the pipe when tail
has terminated.当
tail
终止时,kernel 将向 pipe 上的其他进程发送SIGPIPE
信号。 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 when tail
has terminated), and instead check whether the value of errno
is EPIPE
after each read
.如果您不想处理信号,您可以忽略父级中的
SIGPIPE
(因此它不会在tail
终止时终止),而是在每次read
后检查errno
的值是否为EPIPE
。 Additionally, you'll have to call wait
or waitpid
from the parent to reap the zombie child.此外,您必须从父级调用
wait
或waitpid
来获取僵尸子级。
you don't get EPIPE when reading, only write will return EPIPE.读取时不会得到 EPIPE,只有写入才会返回 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).
您将获得 EOF,由 read 返回 0 指示,并且由于您阅读了 stderr,您也会收到错误消息(在 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.进程会变成僵尸进程,可以使用wait/waitpid来获取退出状态,如果出现错误,退出状态为非零。
If tail fails, any read on the read end of the pipe will return EOF.如果 tail 失败,pipe 读取端的任何读取都将返回 EOF。 If tail fails, it has already terminated, the definition of "fail" being that it terminated with a non-zero exit status.
如果 tail 失败,它已经终止,“失败”的定义是它以非零退出状态终止。 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?但是你为什么要让 tail 对 stderr 和 stdout 使用相同的 pipe 呢? Why not just make two pipes?
为什么不只做两个管道? It seems that this would eliminate the problem of distinguishing between the two output streams.
似乎这将消除区分两个 output 流的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.