What is the difference between Unbuffered I/O and standard I/O? I know that using read(), write(), close() are unbuffered IO. Printf and gets are buffered IO. I also know that it is better to use buffered IO for big transactions. I just dont know the reason why. And what does the term "buffered" mean in this context?
Asked
Active
Viewed 1.5k times
4
-
5Your question appears similar to this: http://stackoverflow.com/questions/1450551/buffered-i-o-vs-unbuffered-io – Dave Dec 07 '11 at 00:52
-
3Strictly speaking, on Linux, read et al are **kernel** buffered unless `O_DIRECT` is enabled. For your context, buffering means that the C library maintains an internal array that is not flushed after every I/O function invocation. Rather, fflush must be called. Likewise, the read buffer may cause less per-call hang during reads. – moshbear Dec 07 '11 at 00:52
-
@moshbear: Could you please clarify what fflush does? Does it flush c internal array or the buffered cache? – FourOfAKind Dec 07 '11 at 13:59
-
From the manual: For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function. For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application. – moshbear Dec 09 '11 at 05:07
1 Answers
7
Unbuffered I/O simply means that don't use any buffer while reading or writing.Generally when we use System calls like read() and write() they read and write char by char and can cause huge performance degradation . So for huge date generally high level reads/writes or simply buffered I/O are preferred .Buffered simply means that we are not dealing with single char but a block of chars, that is why sometimes it also known as block I/O.Generally in Unix when we use high level read/write functions they fetch/store the data of a given block size and place them in buffer cache and from this buffer cache these I/O functions get the desired amount of data.

Gaurav B
- 160
- 8