重定向子进程' stdout 和 stderr 到两个命名管道(然后从它们读回)
我正在开发一个应用程序,它的 popen() 是另一个进程,其输出 - stderr 和 stderr - 需要重定向到两个命名管道,也由应用程序创建。然后我需要从管道读回数据。
mkfifo("output.fifo", 0666); // error checks etc.
mkfifo("error.fifo", 0666); // error checks etc.
popen("cstuff 'param' < input.txt 1> output.fifo 2> error.fifo", "r");
不起作用:当我尝试读取 error.fifo 时,应用程序挂起。 mkfifo()
和 popen()
之间的 sleep()
ing / wait()
也不起作用。
// output.txt is the result from a file dialog
popen("cstuff 'param' < input.txt 1> output.txt 2> error.fifo", "r");
确实有效。
popen("cstuff 'param' < input.txt 1> output.fifo", "r");
也有效。
$ cstuff 'param' < input.txt 1> output.txt 2> output.txt
从 shell 也可以工作(但不能从我的应用程序)。
我找不到一种直接的(或任何)方法来从两个工作的管道中读取数据。如何才能做到这一点?
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试仅使用
system(3)
而不是popen(3)
;您没有使用从popen(3)
返回的FILE*
,因为您没有按照预期使用popen(3)
被使用。但这在system(3)
中应该可以正常工作。Try just
system(3)
instead ofpopen(3)
; you're not using theFILE*
returned frompopen(3)
, because you're not usingpopen(3)
as it was meant to be used. But this ought to work fine fromsystem(3)
.您应该对output.fifo 和error.fifo 的文件描述符调用poll() 或select(),并且仅当有数据准备好时才调用read()。
我建议您使用 pstreams 而不是丑陋的命名管道。
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.