What does it mean by buffer?

I see the word "BUFFER" everywhere, but I am unable to grasp what it exactly is.

  • Would anybody please explain what is buffer in layman's language ?
  • When is it used?
  • How is it used?

  • Imagine that you're eating candy out of a bowl. You take one piece regularly. To prevent the bowl from running out, someone might refill the bowl before it gets empty, so that when you want to take another piece, there's candy in the bowl.

    The bowl acts as a buffer between you and the candy bag.

    If you're watching a movie online, the web service will continually download the next 5 minutes or so into a buffer, that way your computer doesn't have to download the movie as you're watching it (which would cause hanging).


    The term "buffer" is a very generic term, and is not specific to IT or CS. It's a place to store something temporarily, in order to mitigate differences between input speed and output speed. While the producer is being faster than the consumer, the producer can continue to store output in the buffer. When the consumer speeds up, it can read from the buffer. The buffer is there in the middle to bridge the gap.


    If you average out the definitions at http://en.wiktionary.org/wiki/buffer, I think you'll get the idea.

    For proof that we really did "have to walk 10 miles thought the snow every day to go to school", see http://antiquesilicon.com/library/bitsavers/pdf/dec/pdp10/TOPS10_softwareNotebooks/vol04/AA-0974G-TB_monCallsVol1.pdf, section 11.9, "Using Buffered I/O", at bookmark 11-24. Don't read if you're subject to nightmares.


    A buffer is simply a chunk of memory used to hold data. In the most general sense, it's usually a single blob of memory that's loaded in one operation, and then emptied in one or more, Perchik's "candy bowl" example. In a C program, for example, you might have:

    #define BUFSIZE 1024
    char buffer[BUFSIZE];
    size_t len = ;
    
    // ... later
    while((len=read(STDIN, &buffer, BUFSIZE)) > 0)
        write(STDOUT, buffer, len);
    

    ... which is a minimal version of cp(1). Here, the buffer array is used to store the data read by read(2) until it's written; then the buffer is re-used.

    There are more complicated buffer schemes used, for example a circular buffer, where some finite number of buffers are used, one after the next; once the buffers are all full, the index "wraps around" so that the first one is re-used.

    链接地址: http://www.djcxy.com/p/91700.html

    上一篇: 如何关闭Vim中的单个缓冲区(多个)?

    下一篇: 缓冲是什么意思?