恩。谢谢大家的回答。 下面是我选取的部分答案: Buffered vs non-buffered I/O
The printf() is buffered, the write() is a direct system call. The write happens immediately no matter what, the printf will be (usually) buffered line-by-line when the output is a terminal and block-by-block when the output is a real file. In the file-output case (redirection) your actual printf output will happen only when you return from main() or in some other fashion call exit(3), unless you printf a whole bunch of stuff.
而至于第一个问题 2>file 和 2>&fd 的区别,好像不怎么大。得到的回答,大部分都是说,二者的功能都类似于这个dup2函数。 The 2>&1 part makes the shell do something like that:
dup2(1, 2); This makes fd 2 a "copy" of fd 1.
The 2 >& file is interpreted as
fd = open(file, ...); dup2(fd, 2); which opens a file and puts the filedescriptor into slot 2.