/* * pipe.c: sample program showing how to redirect stdout and process * the argument vector. * * Stefan Brandle, COS 421, February 2000. */ #include #include #include int main( int argc, char *argv[], char *envp[] ) { char junk[80]; int pfd[2]; if( pipe(pfd) == -1 ) { fprintf(stderr, "The pipe failed.\n"); return 1; } switch( fork() ) { case -1: fprintf(stderr, "Fork bombed.\n"); exit(1); case 0: // Child #2 if( dup2(pfd[0],0) == -1 ) { fprintf(stderr, "Child 2 dup2 failed\n"); exit(1); } close(pfd[0]); close(pfd[1]); gets(junk); printf("Child 2: got message => %s\n", junk); break; default: // Child #1 if( dup2(pfd[1],1) == -1 ) { fprintf(stderr, "Child 1 dup2 failed\n"); exit(1); } close(pfd[0]); close(pfd[1]); printf("Hi to child #2"); break; } }