#include
#include
#include
int main(void) {
/* the file descriptors */
int pipe_file_descr[2];
/* pipe creates a pair of file descriptors */
pipe(pipe_file_descr);
/* create a child process .... */
if(!fork()) {
/*
// dont need ``normal'' stdout() so...
// closing ``normal'' stdout
*/
close(1);
/*
// Now we make stdout of this process
// the same as pipe_file_descr[1]
// dup() `is duplicate ...'
*/
dup(pipe_file_descr[1]);
/*
// Don't need this pipe file descriptor
// since we have no input, only output for this process
// closing this file descriptor ...
*/
close(pipe_file_descr[0]);
/*
// Fire up `ls' to have some output
*/
execlp("ls", "ls", "-1", NULL);
} else {
/*
// Don't need ``normal'' stdin
*/
close(0);
/*
// making stdin the same as pipe_file_descr[0]
*/
dup(pipe_file_descr[0]);
/* again, don't need this one .. */
close(pipe_file_descr[1]);
/* count the output of `ls' ... */
execlp("wc", "wc", "-l", NULL);
}
/* done */
return 0;
}