错误的文件描述符
有人看到这个问题吗,它不起作用,说文件描述符错误,不知道为什么?
pipe(pipefd[0]);
if ((opid = fork()) == 0) {
dup2(pipefd[0][1],1);/*send to output*/
close(pipefd[0][0]);
close(pipefd[0][1]);
execlp("ls","ls","-al",NULL);
}
if((cpid = fork())==0){
dup2(pipefd[0][1],0);/*read from input*/
close(pipefd[0][0]);
close(pipefd[1][1]);
execlp("grep","grep",".bak",NULL);
}
close(pipefd[0][0]);
close(pipefd[0][1]);
Does anyone see a problem with this, its not working saying bad file descriptor not sure why?
pipe(pipefd[0]);
if ((opid = fork()) == 0) {
dup2(pipefd[0][1],1);/*send to output*/
close(pipefd[0][0]);
close(pipefd[0][1]);
execlp("ls","ls","-al",NULL);
}
if((cpid = fork())==0){
dup2(pipefd[0][1],0);/*read from input*/
close(pipefd[0][0]);
close(pipefd[1][1]);
execlp("grep","grep",".bak",NULL);
}
close(pipefd[0][0]);
close(pipefd[0][1]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的代码,我猜测 pipelinefd 被定义为:
现在,当您这样做时:
这只填充
pipefd[0][0]
和pipefd[0][1].
因此,当您这样做时:
您正在引用随机垃圾(您从未设置
pipefd[1][0]
或pipefd[1][1]
)。从显示的代码中,我不明白为什么你不只是这样做:
Based on your code, I'm guessing pipefd is defined as:
Now, when you do:
This only populates
pipefd[0][0]
andpipefd[0][1]
.So when you do:
you are referencing random junk (you never set
pipefd[1][0]
orpipefd[1][1]
).From the code shown, I can't see why you aren't just doing:
第二个块中的索引看起来很可疑。
The indexes in the second block look suspect.