如果从管道读取时 tail 失败怎么办

发布于 2024-11-29 11:47:28 字数 439 浏览 4 评论 0原文

区分管道上的 stdout 和 stderr

因此,与上面的链接相关,我有正在执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

数理化全能战士 2024-12-06 11:47:28

tail 终止时,内核将向管道上的其他进程发送 SIGPIPE 信号。该信号的默认操作(如果未安装处理程序)是终止进程。

如果您不想处理信号,可以忽略父级中的 SIGPIPE (因此当 tail 终止时它不会终止),而是检查是否每次读取后,errno 的值为EPIPE。此外,您必须从父级调用 waitwaitpid 才能获取僵尸子级。

The kernel will send a SIGPIPE signal to the other process on the pipe when tail 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 when tail has terminated), and instead check whether the value of errno is EPIPE after each read. Additionally, you'll have to call wait or waitpid from the parent to reap the zombie child.

故事未完 2024-12-06 11:47:28

读时不会得到 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.

苹果你个爱泡泡 2024-12-06 11:47:28

如果 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文