This is how I was taught in school :
int main(int argc, char *argv[]) {
int pipefd[2];
pid_t cpid;
char buf;
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) {
/* Child reads from pipe */
close(pipefd[1]);
//make the standard input to be the read end
pipefd[0] = dup2(pipefd[0], STDIN_FILENO);
system("more");
write(STDOUT_FILENO, "\n", 1);
close(pipefd[0]);
} else {
/* Parent writes argv[1] to pipe */
close(pipefd[0]);
/* Close unused read end */
pipefd[1] = dup2(pipefd[1], STDOUT_FILENO);
system("ps aux");
/* Wait for child */
wait(NULL);
exit(EXIT_SUCCESS);
}
return 0;
}
this spawns two processes, one executing "ps aux" and feeding the output to the other one running "more".