在管道上将stdout与stderr区分开 [英] distinguish stdout from stderr on pipe
问题描述
我的问题与上面发布的一个问题有关.在第一个/被接受的响应中,我们正在做:
My question is related to one posted above. In the first/accepted response, we are doing:
// Child. Let's redirect its standard output to our pipe and replace process with tail
close(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO);
dup2(pipefd[1], STDERR_FILENO);
但是我想要的是将ERROR
与常规OUTPUT
区分开.我怎样才能做到这一点?当我在STDERR中得到任何东西时,我需要对此做出反应.
But what I want is to distinguish ERROR
from regular OUTPUT
. How can I do that? When I get anything in STDERR, I need to react to it.
这没有多大意义,但是,我可以执行以下操作吗?:
It does not make much sense but, can I do following?:
int pipefd[3] /* instead of 2 */
dup2(pipefd[1], STDOUT_FILENO);
dup2(pipefd[2], STDERR_FILENO);
我正在使用select
查看fd,并查看输出是否可用.但是直到现在,我只需要查看1 fd,现在我必须查看2.
I am using select
to look at the fd and see if output is available. But till now, I just need to look at 1 fd, now I have to look at 2.
注意:管道只能有2个末端,对吗?一个要写,另一个要读.我如何容纳这个第三端:D ??
NOTE: A pipe can only have 2 ends, right? one to write to and other to read from. How can I accommodate this 3rd end :D ??
推荐答案
您需要创建两个独立的管道,并分别从中读取每个管道.因为您已经有一个select(),所以应该不难.
You need to create two independent pipes and read from each of them separately. Shouldn't be hard since you already have a select() in place.
这篇关于在管道上将stdout与stderr区分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!