Ring buffer basics 环形缓冲基础 一

原文地址:http://www.embedded.com/electronics-blogs/embedded-round-table/4419407/1/The-ring-buffer

The ring buffer's first-in first-out data structure is useful tool for transmitting data between asynchronous processes. Here's how to bit bang one in C without C++'s Standard Template Library.
ringbuffer是一种先进先出的数据结构,在非同步数据传输中很得力。

The ring buffer is a circular software queue. This queue has a first-in-first-out (FIFO) data characteristic. These buffers are quite common and are found in many embedded systems. Usually, most developers write these constructs from scratch on an as-needed basis.
作为一种具有先入先出数据结构的环形序列,其在嵌入式系统中应用广泛,并被开发者作为基本需求。

The C++ language has the Standard Template Library (STL), which has a very easy-to-use set of class templates. This library enables the developer to create the queue and other lists relatively easily. For the purposes of this article, however, I am assuming that we do not have access to the C++ language. 
其实在C++标准库中已经包含实现模板,开发者只需简单调用即可,但是现在我们还是想试试纯C的实现。

The ring buffer usually has two indices to the elements within the buffer. The distance between the indices can range from zero (0) to the total number of elements within the buffer. The use of the dual indices means the queue length can shrink to zero, (empty), to the total number of elements, (full). Figure 1 shows the ring structure of the ring buffer, (FIFO) queue. 
通常在一个环形缓冲队列中有两个指示(首指示-head,尾指示-tail),且之间的距离为队列大小内的任意值,以此可以判断队列是空的(两指示的距离为0)还是队列已经满了(两指示的距离为队列长度),图1示意了这种环形队列结构。


Figure 1: Structure of a ring buffer.

The data gets PUT at the head index, and the data is read from the tail index. In essence, the newest data "grows" from the head index. The oldest data gets retrieved from the tail index.  Figure 2  shows how the head and tail index varies in time using a linear array of elements for the buffer. 
数据在‘首指示’位置开始被记录,在‘尾指’示位置被读出。即,最新的数据记录在首位置,最旧数据从尾位置取出,图2展示了用一线性数组体现队列首位指示随时间的变化。


Figure 2: Linear buffer implementation of the ring buffer. 

Use cases 使用示例
Single process to single process 单轨至单轨
In general, the queue is used to serialize data from one process to another process. The serialization allows some elasticity in time between the processes. In many cases, the queue is used as a data buffer in some hardware interrupt service routine. This buffer will collect the data so that at some later time another process can fetch the data for further processing. This use case is the single process to process buffering case.
一般将数据进行过程之间的序列化操作,并使数据处理保留一定的弹性时间。很多时候都是用在硬件中断服务中作为数据缓存。首先硬件(比如串口)获得数据并缓存,之后其他数据处理函数取走数据并进行下一步分析计算。
This use case is typically found as an interface between some very high priority hardware service buffering data to some lower priority service running in some background loop. This simple buffering use case is shown in  Figure 3
使用: 高优先级的硬件服务(取得数据) --->> ringbuffer 缓存(流量可调接口) ---->> 低优先级的后台服务(数据处理)


Figure 3: A single process to process buffer use case

In many cases, there will be a need for two queues for a single interrupt service. Using multiple queues is quite common for device drivers for serial devices such as RS-232, I2C or USB drivers. 

Multiple processes to single process多轨至单轨

A little less common is the requirement to serialize many data streams into one receiving streams. These use cases are quite common in multi-threaded operating systems. In this case, there are many client threads requesting some type of serialization from some server or broker thread. The requests or messages are serialized into a single queue which is received by a single process. Figure 4 shows this use case. 

多线程中经常会用到多数据流至单接收序列的实例,例如客户端线程对服务或中断线程产生的序列化数据的需求。图4给出了这种展示。



Figure 4: Multiple processes to process use case.


Single process to multiple processes单轨至多轨

The least common use case is the single process to multiple processes case. The difficulty here is to determine where to steer the output in real time. Usually, this is done by tagging the data elements in such a way that a broker can steer the data in some meaningful way.  Figure 5  shows the single process to multiple processes use case. Since queues can be readily created, it is usually better to create multiple queues to solve this use case than it would be to use a single queue. 
最不常见的是单程至多程的实例,问题在于如何实时引导输出。例如通过给数据做标记以便让正确的进程进行处理,图5展示了这种单对多的序列化操作。实际上设计一个缓冲序列很方便,从而可以建立多个序列解决问题,而不是繁琐的处理数据。


Figure 5: Single process to multiple processes use case.

Figure 6 shows how to reorganize the single process to multiple process use case using a set of cascaded queues. In this case, we have inserted an Rx / Tx Broker Dispatcher service, which will parse the incoming requests to each of the individual process queues. 

图6展示了一种使用级联队列来重新序列化单程至多程处理,通过添加一个输入\输出调度服务来解析各需求进程所使用的队列数据。




Figure 6: Single process to multiple process use case using a dispatcher and multiple queues. 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值