I have the basic process of deploying twice and thus creating two children. Two children communicate with each other as follows:
ls | more
Now the problem is that the second child never dies. Why is this? When does the last child in the pipe really die?
Removing one call to wait () shows the expected result ls | more, but gives some additional strange behaviors (stuck terminal, etc.).
Here is my code:
int main(){
printf("[%d] main\n", getpid());
int pip[2], i;
pipe(pip);
for (i=0; i<2; i++){
if (fork()==0){
if (i==0){
printf("[%d] child1\n", getpid());
close(1); dup(pip[1]);
close(pip[0]);
execlp("ls", "ls", NULL);}
if (i==1){
printf("[%d] child2\n", getpid());
close(0); dup(pip[0]);
close(pip[1]);
execlp("more", "more", NULL);}
}
}
wait(NULL);
wait(NULL);
return 0;
}
source
share