有两点值得做进一步说明: (当然这里仅就 Linux 而论)
1. 关于系统调用
引 Robert Love, Linux Kernel Development, 2nd Edition, chapter 5:
Typically, applications are programmed against an Application Programming Interface (API), not directly to system calls. This is important, because no direct correlation is needed between the interfaces that application make use of and the actual interface provided by the kernel.
比如你在你的程序里调用 printf() , 那么你的程序, C 程序库, 及内核之间的关系如下:
你的程序调用 C 程序库提供的 printf() -> C 程序库的 printf() 调用 C 程序库的 write() -> C 程序库的 write() 调用内核提供的系统调用 write() (sys_write())
此外, cout 在其实现的最终, 肯定是会调用系统调用的, 因为它要把东西输出到 stdout, 必须假系统调用之手.
所以是不是系统调用不是两者的区别.
2. 缓冲的问题
cout 确实做了额外的缓冲, 但事实上, 没人知道 printf 或者 cout 之类的调用到底什么时候才会把内容输出. 从内核的角度看, 这牵扯到虚拟内存的管理, 换页机制等等, 要等内核把那些涉及到的页写出去了, 才真的写出去. 而什么时候内核才把那些涉及到的页写出去呢? 鬼知道.