IEEE Robotics & Automation Magazine - March 2015 - 79
The Ach IPC Library
Ach provides a message bus or publish-subscribe style of
communication between multiple writers and multiple
readers. A real-time system has multiple Ach channels
across which individual data samples are published. Messages are sent as byte arrays, so arbitrary data may be transmitted, such as floating point vectors, text, images, and
binary control messages. Each channel is implemented as
two circular buffers: 1) a data buffer with variable-sized
entries and 2) an index buffer with fixed-size elements indicating the offsets into the data buffer. These two circular
buffers are written in a channel-specific POSIX shared
memory file. Using this formulation, we solve and formally
verify the synchronization problem exactly once and contain it entirely within the Ach library.
The Ach interface consists of the following procedures.
● ach_create: Create the shared memory region and initialize its data structures.
● ach_open: Open the shared memory file and initialize
process local channel counters.
● ach_put: Insert a new message into the channel.
● ach_get: Receive a message from the channel.
● ach_close: Close the shared memory file.
Channels must be created before they can be opened. Creation
may be done directly by either the reading or writing process,
or it may be done via the shell command, ach mk channel_
name, before the reader or writer start. This is analogous to the
creation of FIFOs with either the mkfifo shell command or
C function. After the channel is created, each reader or writer
must open the channel before it can get or put messages.
Channel Data Structure
The core data structure of an Ach channel is a pair of circular arrays located in the POSIX shared memory file (Figure 2). It differs from typical circular buffers by permitting
multiple consumers to access the same message from the
channel. The data array contains variable-sized elements
that store the actual message frames sent through the Ach
channel. The index array contains fixed-size elements
where each element contains both an offset into the data
array and the length of that data element. A head offset into
each array indicates the place to insert the next data and the
location of the most recent message frame. Each reader
maintains its own offset into the index array, indicating the
last message seen by that reader. This pair of circular arrays
allows readers to find the variable-size message frames
based on the index array offset and the corresponding
entry in the data array.
Access to the channel is synchronized using a mutex and
condition variable. This allows readers to either periodically
poll the channel for new data or wait on the condition variable until a writer has posted a new message. Using a read/
write lock instead would have allowed only polling. In addition, synchronization using a mutex prevents starvation and
enables proper priority inheritance between processes, which
is important to maintaining real-time performance.
2
header
3
index_head index _free data_head data_free
4
1
0
0
I0
I1
I2
I3
index_array
data_array
0
a0
a1
a2
a3
b0
0
0
D0
D1
D2
D3
D4
D5
D6
D7
Figure 2. A logical memory structure for an Ach shared memory
file. In this example, I 0 points to a 4-byte message starting at
D 1, and I 1 points to a 1-byte message starting at D 5 . The next
inserted message will use index cell I 2 and start at D 6 . There
are two free index cells and three free data bytes. Both arrays are
circular and wrap around when the end is reached.
Core Procedures
Two procedures compose the core of Ach: ach_put and
ach_get. Detailed pseudocode is provided in [6].
ach_put
The procedure ach_put inserts new messages into the
channel. It is analogous to write, sendmsg, and mq_send.
The procedure is given a pointer to the shared memory
region for the channel and a byte array containing the message to post. There are four broad steps to the procedure.
1) Get an index entry. If there is at least one free index entry,
use it. Otherwise, clear the oldest index entry and its corresponding message in the data array.
2) Make room in the data array. If there is enough room
already, continue. Otherwise, repeatedly free the oldest
message until there is enough room.
3) Copy the message into data array.
4) Update the offset and free counts in the channel structure.
ach_get
The procedure ach_get receives a message from the channel. It is analogous to read, recvmsg, and mq_receive.
The procedure takes a pointer to the shared memory region, a
storage buffer to copy the message to, the last message
sequence number received, the next index offset to check for
a message, and option flags indicating whether to block waiting for a new message and whether to return the newest message bypassing any older unseen messages. There are four
broad steps to the procedure.
1) If we are to wait for a new message and there is no new
message, then wait. Otherwise, if there are no new messages, return a status code indicating this fact.
2) Find the index entry to use. If we are to return the newest
message, use that entry. Otherwise, if the next entry we
expected to use contains the next sequence number we
expect to see, use that entry. Otherwise, use the oldest entry.
March 2015
*
IEEE ROBOTICS & AUTOMATION MAGAZINE
*
79
Table of Contents for the Digital Edition of IEEE Robotics & Automation Magazine - March 2015
IEEE Robotics & Automation Magazine - March 2015 - Cover1
IEEE Robotics & Automation Magazine - March 2015 - Cover2
IEEE Robotics & Automation Magazine - March 2015 - 1
IEEE Robotics & Automation Magazine - March 2015 - 2
IEEE Robotics & Automation Magazine - March 2015 - 3
IEEE Robotics & Automation Magazine - March 2015 - 4
IEEE Robotics & Automation Magazine - March 2015 - 5
IEEE Robotics & Automation Magazine - March 2015 - 6
IEEE Robotics & Automation Magazine - March 2015 - 7
IEEE Robotics & Automation Magazine - March 2015 - 8
IEEE Robotics & Automation Magazine - March 2015 - 9
IEEE Robotics & Automation Magazine - March 2015 - 10
IEEE Robotics & Automation Magazine - March 2015 - 11
IEEE Robotics & Automation Magazine - March 2015 - 12
IEEE Robotics & Automation Magazine - March 2015 - 13
IEEE Robotics & Automation Magazine - March 2015 - 14
IEEE Robotics & Automation Magazine - March 2015 - 15
IEEE Robotics & Automation Magazine - March 2015 - 16
IEEE Robotics & Automation Magazine - March 2015 - 17
IEEE Robotics & Automation Magazine - March 2015 - 18
IEEE Robotics & Automation Magazine - March 2015 - 19
IEEE Robotics & Automation Magazine - March 2015 - 20
IEEE Robotics & Automation Magazine - March 2015 - 21
IEEE Robotics & Automation Magazine - March 2015 - 22
IEEE Robotics & Automation Magazine - March 2015 - 23
IEEE Robotics & Automation Magazine - March 2015 - 24
IEEE Robotics & Automation Magazine - March 2015 - 25
IEEE Robotics & Automation Magazine - March 2015 - 26
IEEE Robotics & Automation Magazine - March 2015 - 27
IEEE Robotics & Automation Magazine - March 2015 - 28
IEEE Robotics & Automation Magazine - March 2015 - 29
IEEE Robotics & Automation Magazine - March 2015 - 30
IEEE Robotics & Automation Magazine - March 2015 - 31
IEEE Robotics & Automation Magazine - March 2015 - 32
IEEE Robotics & Automation Magazine - March 2015 - 33
IEEE Robotics & Automation Magazine - March 2015 - 34
IEEE Robotics & Automation Magazine - March 2015 - 35
IEEE Robotics & Automation Magazine - March 2015 - 36
IEEE Robotics & Automation Magazine - March 2015 - 37
IEEE Robotics & Automation Magazine - March 2015 - 38
IEEE Robotics & Automation Magazine - March 2015 - 39
IEEE Robotics & Automation Magazine - March 2015 - 40
IEEE Robotics & Automation Magazine - March 2015 - 41
IEEE Robotics & Automation Magazine - March 2015 - 42
IEEE Robotics & Automation Magazine - March 2015 - 43
IEEE Robotics & Automation Magazine - March 2015 - 44
IEEE Robotics & Automation Magazine - March 2015 - 45
IEEE Robotics & Automation Magazine - March 2015 - 46
IEEE Robotics & Automation Magazine - March 2015 - 47
IEEE Robotics & Automation Magazine - March 2015 - 48
IEEE Robotics & Automation Magazine - March 2015 - 49
IEEE Robotics & Automation Magazine - March 2015 - 50
IEEE Robotics & Automation Magazine - March 2015 - 51
IEEE Robotics & Automation Magazine - March 2015 - 52
IEEE Robotics & Automation Magazine - March 2015 - 53
IEEE Robotics & Automation Magazine - March 2015 - 54
IEEE Robotics & Automation Magazine - March 2015 - 55
IEEE Robotics & Automation Magazine - March 2015 - 56
IEEE Robotics & Automation Magazine - March 2015 - 57
IEEE Robotics & Automation Magazine - March 2015 - 58
IEEE Robotics & Automation Magazine - March 2015 - 59
IEEE Robotics & Automation Magazine - March 2015 - 60
IEEE Robotics & Automation Magazine - March 2015 - 61
IEEE Robotics & Automation Magazine - March 2015 - 62
IEEE Robotics & Automation Magazine - March 2015 - 63
IEEE Robotics & Automation Magazine - March 2015 - 64
IEEE Robotics & Automation Magazine - March 2015 - 65
IEEE Robotics & Automation Magazine - March 2015 - 66
IEEE Robotics & Automation Magazine - March 2015 - 67
IEEE Robotics & Automation Magazine - March 2015 - 68
IEEE Robotics & Automation Magazine - March 2015 - 69
IEEE Robotics & Automation Magazine - March 2015 - 70
IEEE Robotics & Automation Magazine - March 2015 - 71
IEEE Robotics & Automation Magazine - March 2015 - 72
IEEE Robotics & Automation Magazine - March 2015 - 73
IEEE Robotics & Automation Magazine - March 2015 - 74
IEEE Robotics & Automation Magazine - March 2015 - 75
IEEE Robotics & Automation Magazine - March 2015 - 76
IEEE Robotics & Automation Magazine - March 2015 - 77
IEEE Robotics & Automation Magazine - March 2015 - 78
IEEE Robotics & Automation Magazine - March 2015 - 79
IEEE Robotics & Automation Magazine - March 2015 - 80
IEEE Robotics & Automation Magazine - March 2015 - 81
IEEE Robotics & Automation Magazine - March 2015 - 82
IEEE Robotics & Automation Magazine - March 2015 - 83
IEEE Robotics & Automation Magazine - March 2015 - 84
IEEE Robotics & Automation Magazine - March 2015 - 85
IEEE Robotics & Automation Magazine - March 2015 - 86
IEEE Robotics & Automation Magazine - March 2015 - 87
IEEE Robotics & Automation Magazine - March 2015 - 88
IEEE Robotics & Automation Magazine - March 2015 - 89
IEEE Robotics & Automation Magazine - March 2015 - 90
IEEE Robotics & Automation Magazine - March 2015 - 91
IEEE Robotics & Automation Magazine - March 2015 - 92
IEEE Robotics & Automation Magazine - March 2015 - 93
IEEE Robotics & Automation Magazine - March 2015 - 94
IEEE Robotics & Automation Magazine - March 2015 - 95
IEEE Robotics & Automation Magazine - March 2015 - 96
IEEE Robotics & Automation Magazine - March 2015 - 97
IEEE Robotics & Automation Magazine - March 2015 - 98
IEEE Robotics & Automation Magazine - March 2015 - 99
IEEE Robotics & Automation Magazine - March 2015 - 100
IEEE Robotics & Automation Magazine - March 2015 - 101
IEEE Robotics & Automation Magazine - March 2015 - 102
IEEE Robotics & Automation Magazine - March 2015 - 103
IEEE Robotics & Automation Magazine - March 2015 - 104
IEEE Robotics & Automation Magazine - March 2015 - 105
IEEE Robotics & Automation Magazine - March 2015 - 106
IEEE Robotics & Automation Magazine - March 2015 - 107
IEEE Robotics & Automation Magazine - March 2015 - 108
IEEE Robotics & Automation Magazine - March 2015 - 109
IEEE Robotics & Automation Magazine - March 2015 - 110
IEEE Robotics & Automation Magazine - March 2015 - 111
IEEE Robotics & Automation Magazine - March 2015 - 112
IEEE Robotics & Automation Magazine - March 2015 - 113
IEEE Robotics & Automation Magazine - March 2015 - 114
IEEE Robotics & Automation Magazine - March 2015 - 115
IEEE Robotics & Automation Magazine - March 2015 - 116
IEEE Robotics & Automation Magazine - March 2015 - 117
IEEE Robotics & Automation Magazine - March 2015 - 118
IEEE Robotics & Automation Magazine - March 2015 - 119
IEEE Robotics & Automation Magazine - March 2015 - 120
IEEE Robotics & Automation Magazine - March 2015 - 121
IEEE Robotics & Automation Magazine - March 2015 - 122
IEEE Robotics & Automation Magazine - March 2015 - 123
IEEE Robotics & Automation Magazine - March 2015 - 124
IEEE Robotics & Automation Magazine - March 2015 - 125
IEEE Robotics & Automation Magazine - March 2015 - 126
IEEE Robotics & Automation Magazine - March 2015 - 127
IEEE Robotics & Automation Magazine - March 2015 - 128
IEEE Robotics & Automation Magazine - March 2015 - 129
IEEE Robotics & Automation Magazine - March 2015 - 130
IEEE Robotics & Automation Magazine - March 2015 - 131
IEEE Robotics & Automation Magazine - March 2015 - 132
IEEE Robotics & Automation Magazine - March 2015 - 133
IEEE Robotics & Automation Magazine - March 2015 - 134
IEEE Robotics & Automation Magazine - March 2015 - 135
IEEE Robotics & Automation Magazine - March 2015 - 136
IEEE Robotics & Automation Magazine - March 2015 - 137
IEEE Robotics & Automation Magazine - March 2015 - 138
IEEE Robotics & Automation Magazine - March 2015 - 139
IEEE Robotics & Automation Magazine - March 2015 - 140
IEEE Robotics & Automation Magazine - March 2015 - 141
IEEE Robotics & Automation Magazine - March 2015 - 142
IEEE Robotics & Automation Magazine - March 2015 - 143
IEEE Robotics & Automation Magazine - March 2015 - 144
IEEE Robotics & Automation Magazine - March 2015 - 145
IEEE Robotics & Automation Magazine - March 2015 - 146
IEEE Robotics & Automation Magazine - March 2015 - 147
IEEE Robotics & Automation Magazine - March 2015 - 148
IEEE Robotics & Automation Magazine - March 2015 - 149
IEEE Robotics & Automation Magazine - March 2015 - 150
IEEE Robotics & Automation Magazine - March 2015 - 151
IEEE Robotics & Automation Magazine - March 2015 - 152
IEEE Robotics & Automation Magazine - March 2015 - Cover3
IEEE Robotics & Automation Magazine - March 2015 - Cover4
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2023
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2023
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2023
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2023
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2022
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2022
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2022
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2022
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2021
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2021
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2021
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2021
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2020
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2020
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2020
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2020
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2019
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2019
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2019
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2019
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2018
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2018
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2018
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2018
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2017
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2017
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2017
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2017
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2016
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2016
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2016
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2016
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2015
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2015
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2015
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2015
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2014
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2014
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2014
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2014
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2013
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2013
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2013
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2013
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2012
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2012
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2012
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2012
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2011
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2011
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2011
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2011
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2010
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2010
https://www.nxtbookmedia.com