Why does the following code execute the command:
"cat /etc/passwd | wc -l"
and not
"wc -l | cat /etc/passwd"?
Even though the debugging statements are in the order
b
a
int main() {
pid_t pid;
int fd[2];
int stdOut = dup(1);
pid = fork();
if (pid == 0) {
pipe(fd);
pid = fork();
if (pid == 0) {
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
write(stdOut, "a\n", 2);
execlp("cat", "cat", "/etc/passwd", NULL);
}
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
write(stdOut, "b\n", 2);
execlp("wc", "wc", "-l", NULL);
}
wait(NULL);
return 0;
}