开发者

Redirecting a child process' stdout and stderr to two named pipes (then reading back from them)

I'm working on an application that popen()s another process, whose output - both stderr and stderr - needs to be redirected to two named pipes, also created by the application. Then I need to read data back from the pipes.

mkfifo("output.fifo", 0666); // error checks etc.
mkfifo("error.fifo", 0666); // error checks etc.
popen("cstuff 'param' < input.txt开发者_如何学Python 1> output.fifo 2> error.fifo", "r");

does not work: the application hangs when I try to read from error.fifo. sleep()ing / wait()ing between mkfifo() and popen() doesn't work either.

// output.txt is the result from a file dialog
popen("cstuff 'param' < input.txt 1> output.txt 2> error.fifo", "r");

does work.

popen("cstuff 'param' < input.txt 1> output.fifo", "r");

also works.

$ cstuff 'param' < input.txt 1> output.txt 2> output.txt

from the the shell also works (but not from my application).

I couldn't find a straightforward (or any) way to get reading from both pipes working. How can that be achieved?


Try just system(3) instead of popen(3); you're not using the FILE* returned from popen(3), because you're not using popen(3) as it was meant to be used. But this ought to work fine from system(3).


You should call poll() or select() on file descriptors of both output.fifo and error.fifo, and call read() only when there is a data ready.

I suggest you to use pstreams instead of ugly named pipes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜