distinguish stdout from stderr on pipe

最后都变了- 提交于 2019-12-24 05:32:49

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!