坏文件描述符

有没有人看到这个问题,它不工作说坏文件描述不知道为什么?

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

基于你的代码,我猜测pipefd被定义为:

 int pipefd[2][2]; 

现在,当你这样做时:

 pipe(pipefd[0]) 

这只会填充pipefd[0][0]pipefd[0][1]

所以当你这样做时:

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

你引用随机垃圾(你从未设置pipefd[1][0]pipefd[1][1] )。

从显示的代码中,我看不出为什么你不只是在做:

 int pipefd[2]; pipe(pipefd); 

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