Buffered I/O , Unbuffered I/O

luckygamza·2021년 10월 13일

Buffered I/O Example - stdout

standard output is buffered simply because it's assumed there will be far more data going through it.

Another example is a logging library. If your log messages are held within buffers in your process, and your process dumps core, there a very good chance that output will never be written.

데이터를 계속 받는 상황이 예측될 때 buffered I/O를 사용. But, 디스크에 쓰기 전이라 dump 나면 다 날라갈 수도 있다.

Unbuffed I/O Example - stderr

You want unbuffered output whenever you want to ensure that the output has been written before continuing.
One example is standard error under a C runtime library - this is usually unbuffered by default.
Since errors are (hopefully) infrequent, you want to know about them immediately.

데이터를 바로바로 내보내야 할 필요가 있을 때 unbuffer I/O 를 사용.

Buffered I/O 의 장점.

system call and disk I/O 를 줄일 수 있다.

In addition, it's not just system calls that are minimized but disk I/O as well. Let's say a program reads a file one byte at a time. With unbuffered input, you will go out to the (relatively very slow) disk for every byte even though it probably has to read in a whole block anyway (the disk hardware itself may have buffers but you're still going out to the disk controller which is going to be slower than in-memory access).

By buffering, the whole block is read in to the buffer at once then the individual bytes are delivered to you from the (in-memory, incredibly fast) buffer area.

Keep in mind that buffering can take many forms, such as in the following example:

+-------------------+-------------------+
| Process A | Process B |
+-------------------+-------------------+
| C runtime library | C runtime library | C RTL buffers
+-------------------+-------------------+
| OS caches | Operating system buffers
+---------------------------------------+
| Disk controller hardware cache | Disk hardware buffers
+---------------------------------------+
| Disk |
+---------------------------------------+
출처: https://stackoverflow.com/questions/1450551/buffered-vs-unbuffered-io

0개의 댓글