diff options
Diffstat (limited to 'dup.c')
-rw-r--r-- | dup.c | 8 |
1 files changed, 6 insertions, 2 deletions
@@ -31,8 +31,10 @@ int main(int argc, char **argv) if (!pid) { - close(1); /* break the link to stdout */ + close(1); /* break the link to stdout, dup2 already kills this but meh */ dup2(pfds[1], 1); /* redirect stdout to pfds[1] */ + close(pfds[1]); /* new stdout has been setup above, pfds[1] can be closed since it's a dup */ + close(pfds[0]); /* child will not be reading anything from out pipe */ execlp("ls", "ls", "-1", NULL); @@ -40,7 +42,9 @@ int main(int argc, char **argv) else { close(0); /* break the link to stdin */ - dup2(pfds[0], 0); /* redirect stdout to pfds[0] */ + dup2(pfds[0], 0); /* redirect stdin to pfds[0] */ + close(pfds[0]); /* new stdin has been setup above, pfds[1] can be closed since it's a dup */ + close(pfds[1]); /* parent will not be writing anything into our pipe */ ret = waitpid(pid, &stat_loc, WCONTINUED); |