开发者

distinguish stdout from stderr on pipe

popen() alternative

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);

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 pipef开发者_如何学编程d[3]     /* instead of 2 */

dup2(pipefd[1], STDOUT_FILENO);
dup2(pipefd[2], STDERR_FILENO);

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.

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 ??


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜