FACTOID # 95: You can be imprisoned for not voting in Fiji, Chile and Egypt - at least in theory.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

FACTS & STATISTICS    Simple view

  1. Select countries to view: (hold down Control key and click to select several)

     

     

    Compare:

     

     

  1. Select fact or statistic: (* = graphable)

     

     

     

  2. (OPTIONAL) Compare to statistic: (both need to be graphable)

     

     

     

  3. View result as:

     

       
(OR) SEARCH ALL encyclopedia, stats & forums:   

Encyclopedia > Ring buffer

A circular buffer is a method of using memory within a computer program. To meet Wikipedias quality standards, this article or section may require cleanup. ... A computer program or software program (usually abbreviated to a program) is a step-by-step list of instructions written for a particular computer architecture in a particular computer programming language. ...


While the term "circular" is figurative, it alludes to the rotation through the buffer of the positions where the next data will be read and written. When moving through the buffer, the writer moves forward one step each time it writes, and when it passes the end of the buffer it starts again at the beginning. The reader moves through the buffer in the same way. As long as the reader is usually as fast as the writer, the buffer acts as a queue of the next data to be processed. If the writer is faster than the reader, the buffer can fill up. In computing, a buffer is memory used to temporarily hold output or input data. ... In providing services to people, and in computer science, transport and operations research a queue is a First-In-First-Out (FIFO) process â€” the first element in the queue will be the first one out. ...


The term ring buffer is also often used to describe a circular buffer.

Contents


Implementation

A circular buffer requires storage space for the data of whatever size is desired and feasible, pointers to the current locations for reading and writing, and a method for determining when the buffer is empty or full. The processes of writing and reading also need to know how big the buffer is.


Buffer Space

The data storage for the buffer must be able to hold at least one unit of data, and should (if possible) be large enough to hold as much data as the data source will produce while the user of the data is either working with the data or is otherwise not reading it.


The units of data in the buffer can be anything, but are most often the smallest unit of data that the system can process efficiently.


Read and Write Pointers

The two pointers indicate where in memory the next data should be read or written. Each time data is read, the read pointer moves forward, going back to the beginning after reading the last thing in the buffer. Each time data is written, the write pointer moves forward, going back to the beginning after writing to the last position in the buffer. Depending on how the implementation handles writing to a full buffer, the writing process may also need to move the read pointer to make room for more incoming data.


Empty and Full Buffers

If the read and write pointers point to the same spot in the buffer, that might mean that there is no data in the buffer (nothing has yet been written to the spot we next want to read from) or that the buffer is full (the next place we want to write already has in it data we have not yet read). There are two ways to make the distinction.

  • Do not allow the write pointer to catch up to the read pointer. Consider the buffer full when there is only one element between the write pointer and the read pointer.
  • Maintain a separate indication of whether there is data in the buffer. This can be the number of elements in the buffer, or a simple flag that is set to "Yes" each time data is written and "No" when the read pointer catches up to the write pointer.

How to Write to a Full Buffer

If the data source produces more data than can fit in the buffer, there are four choices:

  • If possible, tell the data source to wait until there is room in the buffer.
  • If the latest data is the most important, write over the oldest data that has not been read, and move the read pointer to the position of the oldest remaining data.
  • If the oldest data is the most important, ignore new data from the source until there is room again in the buffer.
  • Grow the buffer so it can handle more elements. This involves allocating a new buffer, copying the data and releasing the old buffer.

How Full is the Buffer

If the implementation maintains a count of the number of elements in the buffer, that count can be checked at any time the information is necessary. Otherwise, the difference between the write pointer and the read pointer can provide that information. Assuming that the position of both pointers can be expressed as numbers and each element in the buffer increases the pointer's position by 1, the amount of data waiting in the buffer is w - r modulo n (with w representing the position of the write pointer, r representing the position of the read pointer, and n being the size of the buffer). If the result is zero, the process may need to check the indicator to see whether the buffer is empty or full.


Sharing the Buffer

The processes that read and write the data do not need to be able to communicate outside the buffer itself as long as there is an agreed method for losing data that does not fit in the buffer and the pointers to the reading and writing positions cannot be changed by both processes at exactly the same time.


See also

  • For information on the abstract data structure, and an implementation in Scheme, see Queue.
  • For a description of the behaviour and sample code, see FIFO.
Scheme is a functional programming language and a dialect of Lisp. ... In providing services to people, and in computer science, transport and operations research a queue is a First-In-First-Out (FIFO) process â€” the first element in the queue will be the first one out. ... FIFO is an acronym for First In, First Out. ...

  Results from FactBites:
 
Logging in multi-threaded applications efficiently with ring buffer (2151 words)
A ring buffer is a logging technique for applications whereby the relevant data to be logged is kept in memory instead of writing it to a file on a disk every time.
Ring buffer logging consists of a fixed size of allocated memory buffer that is used by the process for logging.
Reading from a ring buffer is accomplished by keeping a read pointer; this pointer and the write pointer are moved accordingly to ensure that the read pointer never crosses the write pointer while reading.
Circular buffer - Wikipedia, the free encyclopedia (1070 words)
A circular buffer requires storage space for the data of whatever size is desired and feasible, pointers to the current locations for reading and writing, and a method for determining when the buffer is empty or full.
The data storage for the buffer must be able to hold at least one unit of data, and should (if possible) be large enough to hold as much data as the data source will produce while the user of the data is either working with the data or is otherwise not reading it.
A modern approach to this problem is to use a circular buffer and two threads: one thread reads data as it becomes available, another thread wakes up as the output driver indicates the need for another block of data (or polls and delivers another block when a need is detected).
  More results at FactBites »


 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.