The Missing EOF
This program is supposed to capture the output of echo hello and print it, then exit. It prints hello and then hangs forever.
int fd[2];
pipe(fd);
if (fork() == 0) {
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
execlp("echo", "echo", "hello", NULL);
_exit(127);
}
char buf[256];
ssize_t n;
while ((n = read(fd[0], buf, sizeof buf)) > 0)
write(STDOUT_FILENO, buf, n);
wait(NULL);
Why does it hang, and what is the fix?
Sign in to answer questions and track your progress
Sign In