I am currently working with building realtime IPC system.
I am trying to build a realtime system free of all undeterministic components.
I try to setup IPC based on shared memory model.
Is it safe to use managed_shared_memory from boost library to minimize unpredictable latency? or should I use mmap() for memory sharing?
Also Is rt_signal which has channel from SIGRTMIN to SIGRTMAX safe to use?
Thanks! By the way I am using them on preempt-rt patched linux kernel.
Advertisement
Answer
Is it safe to use
managed_shared_memory
from boost library to minimize unpredictable latency? or should I usemmap()
for memory sharing?
Manipulating process address space mapping with mmap
or brk
is an expensive operation, so you’d want to minimize the number of times you use mmap
. mmap
is one source of unpredictable latency.
managed_shared_memory
uses mmap
and provides extra functionality on top of that, which you’d need to implement yourself when using mmap
.
It probably doesn’t matter for performance which one you use since mmap
cost is going to dominate.
Also Is
rt_signal
which has channel fromSIGRTMIN
toSIGRTMAX
safe to use?
Signals may not be the best choice for IPC because the POSIX signal interfaces are prone to race conditions: process ids get reused so that pid
argument may refer to a totally different process by the time you call sigqueue
.
Signals only support directed communication, no broadcast. There is also a system-wide limit on the number of queued real-time signals.
And the APIs to handle signals aren’t convenient, but that recently changed with the introduction of signalfd
and pidfd_open
.
A relatively simple and robust message-based shared-memory IPC is a message ring-buffer in shared memory along with a shared memory mutex and condition variable.
Boost Interprocess library provides interprocess_mutex
and interprocess_condition
and for ring-buffer you can use boost::lockfree::spsc_queue
with a fixed-size.
By the way I am using them on preempt-rt patched linux kernel.
You should watch Kernel Recipes 2016 – Who needs a Real-Time Operating System (Not You!) – Steven Rostedt. Real-time basically means slow but with guaranteed dead-lines.