I have a piping question, I am trying to implement piping on my own shell and am having some trouble...esentially I am trying to make something to do command|command|command.
I can get it to work fine if the last pipe command is not forked, but executes in the shell and then exits..but I need it to fork. I tryed the following, but it will hang..any suggestions? I have a feeling it has something to do with closing/opening descriptors but i don't know...thanks.:
Code:
int pipe1[2], pipe2[2];
pipe(pipe1);//create first pipe
pid_t PID=fork();
if(PID==0){
close(1);
dup(pipe1[1]);
close(pipe1[0]);
close(pipe1[1]);
execvp(*argv,argv);
printf("operation failed");
} else {
pipe(pipe2);
pid_t PID2=fork();
if (PID2==0) {
close(0);
dup(pipe1[0]);
close(1);
dup(pipe2[1]);
close(pipe1[0]);
close(pipe1[1]);
close(pipe2[0]);
close(pipe2[1]);
execvp(*argv2, argv2);
} else {
pid_t PID3=fork();
waitpid(PID3,&status,0);<-----Not sure about this
if(PID3==0){
close(0);
dup(pipe2[0]);
close(pipe1[0]);
close(pipe1[1]);
close(pipe2[0]);
close(pipe2[1]);
execvp(*argv3, argv3);
}
}
}