SHM
Shared-memory based Handy-communication Manager
๐Ÿ“– Introduction - Communication Library Fundamentals

[English | ๆ—ฅๆœฌ่ชž]

๐ŸŒŸ Welcome to the World of Inter-Process Communication!

Shared Memory Based Handy Communication Manager is a comprehensive library collection that makes inter-process communication easy to implement. With this library, you can achieve complex communication processing with just a few lines of code.

๐Ÿค” What is Inter-Process Communication?

Understanding Through Familiar Examples

Inter-process communication means different programs exchanging information with each other.

๐Ÿ“ฑ Smartphone Example
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” Data โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Camera App โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ถ โ”‚ Photo Edit App โ”‚
โ”‚ (Capture Data) โ”‚ โ”‚ (Processing) โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
๐Ÿญ Factory Example
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” Sensor Data โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Sensor Monitor โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ถ โ”‚ Control System โ”‚
โ”‚ Program โ”‚ โ”‚ Program โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Why is Inter-Process Communication Necessary?

๐Ÿ”น Efficiency Through Division of Labor

  • Each program focuses on specialized processing
  • Development, maintenance, and testing become easier

๐Ÿ”น Improved Stability

  • One program crashing doesn't affect others
  • Partial updates and restarts are possible

๐Ÿ”น Scalability

  • Adjust number of processes according to processing capacity
  • Distributed processing across multiple computers

๐Ÿง  Problems This Library Solves

Traditional Challenges

// โŒ Traditional method: Complex and dangerous
void* shared_mem = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
int* data = reinterpret_cast<int*>(shared_mem); // Dangerous type conversion
pthread_mutex_lock(&mutex); // Manual mutual exclusion
*data = 42;
pthread_mutex_unlock(&mutex);
munmap(shared_mem, size); // Manual memory management

Our Library's Solution

// โœ… Our library: Simple and safe
Publisher<int> pub("my_topic");
pub.publish(42); // That's all!

๐Ÿš€ Features of Three Communication Methods

Use Case: Real-time communication within the same PC

Library Features Applications
shm_pub_sub ๐Ÿ“ก Publisher/Subscriber model
โšก Microsecond-level ultra-low latency
Robot control, real-time image processing
shm_service ๐Ÿค Request/Response model
๐Ÿ”’ Guaranteed send/receive reliability
Database operations, file processing
shm_action โšก Asynchronous processing model
๐Ÿ“Š Progress monitoring & cancel functionality
Long computations, file downloads
// Example: Robot sensor data distribution
Publisher<SensorData> sensor_pub("robot_sensors");
sensor_pub.publish(sensor_reading); // Ultra-fast distribution

๐ŸŽฏ Which Communication Method Should You Choose?

๐Ÿ“Š Communication Method Selection Flowchart

What is your use case?
โ”‚
โ”œโ”€ Need maximum speed (microseconds)
โ”‚ โ””โ”€ ๐Ÿ“ก shm_pub_sub (Pub/Sub)
โ”‚
โ”œโ”€ Want reliable data send/receive
โ”‚ โ””โ”€ ๐Ÿค shm_service (Service)
โ”‚
โ””โ”€ Want to monitor time-consuming processes
โ””โ”€ โšก shm_action (Action)

๐Ÿ” Detailed Comparison Table

Feature shm_pub_sub shm_service shm_action
Communication Range Same PC Same PC Same PC
Speed โšกโšกโšก Fastest โšกโšก Fast โšกโšก Fast
Reliability ๐Ÿ“ฆ Best Effort ๐Ÿ”’ Guaranteed ๐Ÿ”’ Guaranteed
Communication Pattern 1:N (Broadcast) 1:1 (Request-Response) 1:1 (Asynchronous)
Data Size Any Any Any
Setup Simplicity ๐ŸŸข Very Easy ๐ŸŸข Very Easy ๐ŸŸก Easy

๐Ÿ› ๏ธ Development History and Design Philosophy

๐Ÿ›๏ธ Library Lineage

This library is based on a C library created by Professor Koichi Ozaki for robot development in his laboratory.

Evolution Process:

๐Ÿ•ฐ๏ธ Initial version (C language)
โ†“ Feature additions and improvements
๐Ÿ”ง C++ version (Object-oriented)

๐ŸŽฏ Design Philosophy

1. ๐ŸŽ›๏ธ Simple API

// Data transmission (publish) example
Publisher<int> pub("topic");
pub.publish(42);

2. ๐Ÿ”’ Safety

  • Type-safe template design
  • Automatic memory management
  • Proper error handling through exceptions

3. ๐Ÿš€ Performance

  • Zero-copy design (shared memory)
  • Efficient data structures (ring buffers)
  • Minimal overhead

4. ๐Ÿ”ง Extensibility and Compatibility

  • ROS-compatible API: Intuitive design based on ROS concepts
  • Custom data types: Support for arbitrary C++ structures
  • Python bindings: Same API for C++ and Python
  • Platform support: Linux, Windows (WSL) support
  • Compiler support: GCC, Clang, MSVC support

๐ŸŽ“ API Design Features

Organization by Namespaces

// Shared memory communication
namespace irlab::shm {
Publisher<T>, Subscriber<T> // Pub/Sub model
ServiceClient<T>, ServiceServer<T> // Service model
ActionClient<T>, ActionServer<T> // Action model
}
// Common base functionality
namespace irlab::shm_base {
// Shared memory foundation features (memory mapping, mutual exclusion, etc.)
}

Consistent Design Patterns

๐Ÿ”น Sender side: Naming pattern for data senders

  • Publisher (publishes data)
  • ServiceClient (requests service)
  • ActionClient (delegates action)

๐Ÿ”น Receiver side: Naming pattern for data receivers

  • Subscriber (subscribes to data)
  • ServiceServer (provides service)
  • ActionServer (executes action)

Automatic Resource Management

{
Publisher<int> pub("topic"); // โ† Automatically allocates memory
pub.publish(42);
// โ† Resources automatically released when leaving scope
}
// No memory leaks!

๐ŸŽ Referenced Systems

1. fuRom [1]

  • Low-latency communication through shared memory
  • Efficient ring buffer implementation

2. ROS (Robot Operating System) [2]

  • Communication patterns of Pub/Sub, Service, Action
  • Topic-based namespace management

3. Modern C++ Design

  • Resource management through RAII
  • Type safety through templates
  • Proper error handling through exceptions

๐Ÿš€ Next Steps

Do you understand the basic concepts? Now let's get hands-on experience!

๐Ÿƒ For Those Who Want to Try Right Now

๐Ÿ“š For Those Who Want to Learn Thoroughly

๐Ÿ”ง For Those Who Want to Know Specific Features

๐Ÿ For Python Developers


๐Ÿ“š References

[1] Irie, Kiyoshi. "ROS ใจใฎ็›ธไบ’้‹็”จๆ€งใซ้…ๆ…ฎใ—ใŸๅ…ฑๆœ‰ใƒกใƒขใƒชใซใ‚ˆใ‚‹ไฝŽ้…ๅปถใƒ—ใƒญใ‚ปใ‚น้–“้€šไฟกใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏ." ็ฌฌ 35 ๅ›žๆ—ฅๆœฌใƒญใƒœใƒƒใƒˆๅญฆไผšๅญฆ่ก“่ฌ›ๆผ”ไผšไบˆ็จฟ้›†, RSJ2017AC2B2-01 (2017). https://furo.org/irie/papers/rsj2017_irie.pdf

[2] Open Robotics, "ROS.org", http://wiki.ros.org/


Are you ready? Let's master inter-process communication! ๐Ÿš€โœจ