【发布时间】:2019-05-28 15:09:55
【问题描述】:
我曾在 C++ 中使用过管道,并且设法使单个管道正常工作。我正在努力让多个管道正常工作。我不知道它到底哪里出了问题。
我用来测试单个管道的示例:
ls -l | grep test
我用来测试多个管道的示例:
ls -l | grep test | grep test2
第一个命令对我来说很好。 但是,第二个命令对我没有任何作用。
编辑 1-6-2019:我将尝试使用此伪代码
left_pipe = NULL
right_pipe = NULL
for each command:
if not last:
right_pipe = pipe()
fork():
child:
if left_pipe is not NULL:
STDIN = left_pipe.read
if right_pipe is not NULL:
STDOUT = right_pipe.write
left_pipe.close_W()
right_pipe.close_R()
execute()
left_pipe.close_RW()
//Move right pipe to the left side for the next command
left_pipe = right_pipe
end
如果有任何见解/帮助,我将不胜感激。
谢谢转发。
【问题讨论】:
-
旁白:5 级嵌套会导致代码不可读。考虑重构您的
execute方法。 -
以后会调查的。
-
什么都没有是什么意思?喜欢程序立即退出还是继续运行?
-
@Quimby 应该解释一下,我的错。我刚刚在 shell 中执行程序后打印了状态。如果我正在执行 ls -l | grep 测试 | grep test2,然后我得到以下信息:-> 程序 test2 以状态码 256 结束。但是,程序继续运行,它可以接受我的输入。
-
您在创建管道之前调用了
dup2ReadPipe!请先致电pipe。