/********************************************************* * * pipe.c * * 4/15/04 Jeff Ondich * * Adapted from "UNIX Network Programming," W. Richard * Stevens, Prentice Hall 1990, p.102. * *********************************************************/ #include #include #include #include #include #include #include int main() { pid_t pid; int fd[2]; if( pipe( fd ) < 0 ) { perror( "Trouble creating pipe.\n" ); exit( 1 ); } printf( "fd[0] = %d, fd[1] = %d\n", fd[0], fd[1] ); pid = fork(); if( pid < 0 ) { perror( "Trouble forking.\n" ); exit( 1 ); } if( pid != 0 ) { close( fd[0] ); if( dup2( fd[1], STDOUT_FILENO ) == -1 ) perror( "Trouble redirecting stdout." ); close( fd[1] ); execlp( "cat", "cat", "moose.txt", NULL ); perror( "Parent: we shouldn't get here." ); } else { close( fd[1] ); if( dup2( fd[0], STDIN_FILENO ) == -1 ) perror( "Trouble redirecting stdin." ); close( fd[0] ); execlp( "wc", "wc", NULL ); perror( "Child: we shouldn't get here." ); } return 0; }