问题
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 pipefd[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 ??
回答1:
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.
来源:https://stackoverflow.com/questions/7030485/distinguish-stdout-from-stderr-on-pipe