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:
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: