将stdout与stderr区分开来

popen()替代方案

我的问题与上面发布的问题有关。 在第一个/接受的回复中,我们正在做:

// 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得到任何东西时,我需要对它做出反应。

它没有多大意义,但是,我可以遵循吗?:

 int pipefd[3] /* instead of 2 */ dup2(pipefd[1], STDOUT_FILENO); dup2(pipefd[2], STDERR_FILENO); 

我正在使用select来查看fd并查看输出是否可用。 但到现在为止,我只需要看看1 fd,现在我要看2。

注意 :管道只能有两个端部,对吗? 一个写入和另一个读取。 我怎样才能适应这个第三端:D ??

您需要创建两个独立的管道并分别从每个管道中读取。 不应该很难,因为你已经有一个select()。