Kaie"s Blog 2.0 beta!
Back to "Top"

My Secret Notebook
 Show "Table of Contents"
Contact Me
Subscribe to my Posts feed
Subscribe to my Comments feed
My Flickr
Back to "Top"
My Secret Notebook
 Show "Table of Contents"
Contact Me
Subscribe to my Posts feed
Subscribe to my Comments feed
My Flickr

Saturday, November 26, 2011

[Linux] stdio buffering / 標準輸入、標準輸出 運作模式

  Linux中的stdio有著一定的行為準則,但常摸不頭緒。在Google大神中找到了一篇文章,解釋得很棒,值得參考參考。一個普遍遇到的問題就是在寫C code時,printf()有時會馬上輸出到stdout,但遇到了換行字元 "\n" 又會馬上寫出,自行呼叫 fflush(stdout) 又可以強迫將buffered的資料全寫出。

其實結論大概可以總結如下:

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 
Default Buffer sizes:
  • 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:

Who: SkyPather Class:
When: November 27, 2011 11:07 PM  

You meant "解釋", not "寫釋", right?

Who: Tsung-Kai Chen Class:
When: November 28, 2011 12:44 AM  

@SkyPather,
Ya, thanks a lot!