Saturday, November 26, 2011
[Linux] stdio buffering / 標準輸入、標準輸出 運作模式
Linux中的stdio有著一定的行為準則,但常摸不頭緒。在Google大神中找到了一篇文章,解釋得很棒,值得參考參考。一個普遍遇到的問題就是在寫C code時,printf()有時會馬上輸出到stdout,但遇到了換行字元 "\n" 又會馬上寫出,自行呼叫 fflush(stdout) 又可以強迫將buffered的資料全寫出。
其實結論大概可以總結如下:
Default Buffering modes:
其實結論大概可以總結如下:
Default Buffering modes:
- stdin is always buffered
- stderr is always unbuffered
- if stdout is a terminal then buffering is automatically set to line buffered, else it is set to buffered
- Buffer size only directly affects buffered mode
- The default size like the kernel is based on the page size (4096 bytes on my system)
- if stdin/stdout are connected to a terminal then default size = 1024; else size = 4096
.buffering in standard streams:
http://www.pixelbeat.org/programming/stdio_buffering/
2 Comments:
When: November 27, 2011 11:07 PM
You meant "解釋", not "寫釋", right?
When: November 28, 2011 12:44 AM
@SkyPather,
Ya, thanks a lot!
Plz Post a Comment / 拜託你留個言啦...^^"