0

I am new to c++, learning through Accelerated C++ by Andrew Koenig and Barbara E. Moo.

I am unable to grasp the concept of buffer in C++ as the book says "Most systems take a significant amount of time to write characters to an output device, regardless of how many characters there are to write. To avoid the overhead of writing in response to each output request, the library uses the buffer to accumulate the characters to be written, and flushes the buffer, by writing its contents to the output device, only when necessary. By doing so, it can combine several output operations into a single write."

Why do most systems take a significant amount of time to write characters to an output device? What is this buffer and why do we require it?Why do we require flushing buffers?

poorvank
  • 7,524
  • 19
  • 60
  • 102
  • 2
    Take a look at this post too: [buffer flushing](http://stackoverflow.com/questions/9651311/buffer-flushing-n-vs-stdendl) @poorvankbhatia – Adrian Chitescu May 15 '13 at 09:19

7 Answers7

4

There are multiple reasons that leads to systems taking significant amount of time for I/O operations : the device may be slow (Hard disk drives), it may require to perform various operations before and/or after writing to it, etc.

Then, a buffer reduces the number of interaction with the device by making bigger but less frequent read/write. Then, it reduces the overhead necessary to perform the operation (in the case of a hard drive, you'll want to write big chunk of data infrequently, rather than small chunks frequently, as it costs to find where to write, etc.).

Moreover, with a buffer, you can even control when you want the I/O operation to be done which can be interesting in particular cases (for example, in a real time application, you'll want to avoid I/O operation while you are running the time-constrained part of the code, storing your data in the buffer, and only doing the I/O operations once you've exited the time-constrained part of the code).

JBL
  • 12,588
  • 4
  • 53
  • 84
2

Why do most systems take a significant amount of time to write characters to an output device?

Because most devices are separated from the CPU by a bus, which is much slower to send data across than it is to write to CPU registers or (cached) memory. And many devices involve moving parts (spinning discs or motorised lasers or whatever) which are much, much slower again.

What is this buffer and why do we require it?

It's a lump of memory that we can write individual characters to very quickly, and then send them all to the device in a single batch later. Usually, this is more efficient (often much, much more efficient) than sending each character individually.

For example, storage devices are often only capable of reading and writing a fairly large block of data at a time; typically, a few kilobytes. So writing a single character requires reading a block, modifying a character and writing back the whole block; while flushing a larger buffer might only require writing each block once.

Why do we require flushing buffers?

Because if the data stays in the buffer, it will never reach the device.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

The output device might be a hard disk, for instance, and writing to the hard disk involved moving the write head around and waiting for the platter to spin to the right place. Doing that for every character you write to a file would be painfully slow.

A buffer is a queue of characters that are ready to write. When there are enough characters in the queue, they are all written together in one operation.

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
1

Some devices are slow, and so it is not efficient to write one character at a time. The efficient way to write to a device is to write a bigger chunk of data at a time. So the buffer is used to accumulate more characters and write them all in one write operation.

Adrian Chitescu
  • 293
  • 1
  • 4
  • 9
0

Let's consider HDD as an output device. Writing to HDD is very slow compared to RAM. It is much faster to write a big chunk of data at once on the HDD then to write a lot of small chunks. The buffer is used to accumulate these chunks. For example, it may wait until you asked to write 1 MB of data total. After that the buffer gets flushed (written) onto the disk. So buffer is just a storage

Andrew
  • 24,218
  • 13
  • 61
  • 90
0

Why do most systems take a significant amount of time to write characters to an output device?

For common computers, writing anything to a typical output device will take longer than writing to internal volatile memory.

What is this buffer and why do we require it?

A buffer is temporary storage for data that is to be moved somewhere else.

Why do we require flushing buffers?

That is a function of a buffer, described in the link above.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
0

I'm unfamiliar with the book, or the quote, so this may not answer your precise point.

Essentially, computers are very fast. It's very quick to move electrons around the place and perform calculations and whatnot. Input/output devices, by contrast, are much slower - disks need to turn around so that the correct part of the surface can be written or read, screens take some time to make the correct part glow in the right colour, etc. etc.

However, a lot of the overhead in writing to an output device is spent writing the first character (or byte, or whatever) and there's very little additional overhead writing several more - once the disk is in the correct position for the first byte, it's in almost the right place for the next and so on; people don't really read the screen a character at a time, they tend to read words or whole sentences so getting that first letter on screen fast isn't worth worying about, you may as well wait until you have several.

Thus, it makes sense to collect many bytes, characters or whatever together and write them all at once. This process is called buffering, and the area of memory in which the data is collected is known as a buffer.

Writing the data in the buffer to the device is known as flushing the buffer.

nurdglaw
  • 2,107
  • 19
  • 37