[09/20/2011] file descriptors C++ C Java 0 STDIN cin scanf() System.in 1 STDOUT cout printf() System.out 2 STDERR cerr fprintf(stderr,...) System.err file descriptor table (e.g. fd.c): +-------------+ 0 | stdin | +-------------+ 1 | newfile.txt | +-------------+ 2 | newfile.txt | +-------------+ the file descriptor table is copied as-is from parent to child (and also through an exec() system call) BEFORE +-------------+ 0 | stdin | +-------------+ 1 | stdout | +-------------+ 2 | stderr | +-------------+ AFTER we close( 1 ) and open() +--------------+ 0 | stdin | +--------------+ 1 | newfile.txt | +--------------+ 2 | stderr | +--------------+ AFTER dup2( 1, 2 ) +--------------+ 0 | stdin | +--------------+ 1 | newfile.txt | +--------------+ 2 | newfile.txt | +--------------+ PIPES after the pipe() system call: +-------------+ 0 | stdin | +-------------+ 1 | stdout | +-------------+ 2 | stderr | +-------------+ 3 | read-- | +-------------+ 4 | write-- | +-------------+ in CHILD process, after dup2() and close() +-------------+ 0 | stdin | +-------------+ 1 | write-- | +-------------+ 2 | stderr | +-------------+ 3 | | +-------------+ 4 | write-- | +-------------+ in PARENT process, after dup2() and close() +-------------+ 0 | read-- | +-------------+ 1 | stdout | +-------------+ 2 | stderr | +-------------+ 3 | read-- | +-------------+ 4 | | +-------------+