简体   繁体   English

错误的文件描述符

[英]Bad File descriptor

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]);

Based on your code, I'm guessing pipefd is defined as: 根据您的代码,我猜pipefd被定义为:

int pipefd[2][2];

Now, when you do: 现在,当您这样做时:

pipe(pipefd[0])

This only populates pipefd[0][0] and pipefd[0][1] . 这只会填充pipefd[0][0]pipefd[0][1]

So when you do: 因此,当您这样做时:

# Bad descriptor
close(pipefd[1][1]);

you are referencing random junk (you never set pipefd[1][0] or pipefd[1][1] ). 您引用的是随机垃圾(您从未设置pipefd[1][0]pipefd[1][1] )。

From the code shown, I can't see why you aren't just doing: 从显示的代码中,我看不到为什么您不只是这样做:

int pipefd[2];
pipe(pipefd);

第二个块中的索引看起来很可疑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM