// test pipe() and dup2() // #include #include int main( void) { int fd[2]; if( pipe(fd) < 0) { perror("pipe"); return 1; } int pid = fork(); if( pid < 0) { perror("fork"); return 1; } if( pid == 0) // child { dup2( fd[0], 0); // make stdin come from pipe close(fd[0]); close(fd[1]); // fd no longer needed, close it int x = 0; scanf("%d",&x); // read from parent printf( "x = %d\n", x); } else // parent { dup2( fd[1], 1); // make stdout go to pipe close(fd[0]); close(fd[1]); // fd no longer needed, close it printf( "%d", 123); // write to child } return 0; } /* output: x = 123 */