Hello guys,
I've seen examples as follows:
Code:
int pfds[2];
char buf[30];
pipe(pfds);
if (!fork()) {
printf(" CHILD: writing to the pipe\n");
close(pfds[0]);
write(pfds[1], "test", 5);
printf(" CHILD: exiting\n");
exit(0);
} else {
printf("PARENT: reading from pipe\n");
read(pfds[0], buf, 5);
printf("PARENT: read \"%s\"\n", buf);
wait(NULL);
}
So the childprocess gets a copy of filedescriptors from the parents. But if the child closes a pfds[0], won't that pfds[0] in the parent process be useless? (appearently not!)
It's a bit confusing to me, cause I thought closing a filedescriptor, will close also one end of the pipe.
Thank you.