English | 简体中文
This repository implements a C++ IPC library based on the Linux System V IPC interface and Windows communication interface, encapsulating various IPC mechanisms such as message queues and named pipes. This library aims to simplify communication operations between processes.
- Supports both Linux and Windows operating systems, as well as multiple compilers such as GCC, MinGW, MSVC, etc.
- When creating a communication node abstraction
IPC::node
, theIPC::ChannelType
parameter can be used to specify the underlying IPC channel type, such asmessageQueue
,NamedPipe
, etc.
- Clone the repository:
git clone https://github.com/leinfinitr/ipc.git
- Navigate to the project directory:
cd ipc
- Initialize submodules:
git submodule update --init --recursive
- Compile:
make
- After compilation, the following files will be generated in the
output
directory:ipc-test-xxx
executable test file- Static library file
libipc.a
(Linux) oripc.lib
(Windows).
- Correctness Test:
/output/bin/ipc-test-correctness
- Performance Test: run
/output/bin/ ipc-test-performance-server
and/output/bin/ipc-test-performance-client
sequentially on different terminals.
- ✅ Realized
- 🔘 Unrealized
- 🚧 Implementing
Communication method | OS | |
---|---|---|
Windows | Linux | |
Named pipe | (Windows NamedPipe) ✅ | 🔘 |
Message queue | (Boost Interprocess) ✅ | (System V IPC) ✅ |
Shared memory | 🚧 | 🚧 |
For projects built using CMake, the IPC library can be introduced through the following methods:
set(IPC_LIB_PATH "/path/to/libipc.a")
set(IPC_INCLUDE_DIR "/path/to/ipc/include")
add_library(ipc STATIC IMPORTED)
set_target_properties(ipc PROPERTIES
IMPORTED_LOCATION "${IPC_LIB_PATH}"
INTERFACE_INCLUDE_DIRECTORIES "${IPC_INCLUDE_DIR}"
)
target_link_libraries(your_target ipc)
target_include_directories(your_target PRIVATE ${IPC_INCLUDE_DIR})
Alternatively, it can be used directly as a third-party library in CMakeLists.txt:
add_subdirectory(ipc EXCLUDE_FROM_ALL)
set(IPC_INCLUDE_DIR "/path/to/ipc/include")
target_link_libraries(your_target ipc)
target_include_directories(your_target PRIVATE ${IPC_INCLUDE_DIR})
#include <ipc/ipc.h>
// Create two IPC nodes named 'Wow', using NamedPipe or MessageQueue (default) at the bottom
// ipc::node receiver("Wow", ipc::NodeType::Receiver, ipc::ChannelType::NamedPipe);
// ipc::node receiver("Wow", ipc::NodeType::Receiver, ipc::ChannelType::MessageQueue);
ipc::node receiver("Wow", ipc::NodeType::kReceiver);
ipc::node sender("Wow", ipc::NodeType::kSender);
auto rec = receiver.Receive(); // Receive message (will block the process until the message is received)
sender.Send(data, sizeof(data)); // Send a message
Two example programs are provided in the examples
directory: sender.cpp
and receiver.cpp
.
The usage method is as follows:
cd examples
# Compile
make
# Run receiver
make run_receiver
# Run sender on the new terminal
make run_sender
# At this time, the receiver can receive the message
Received message: Hello, IPC!
- Windows: Intel (R) Core (TM) Ultra 5 225
- Linux: Intel (R) Core (TM) Ultra 9 185H
Communication latency / µs | Windows | Linux | |||
---|---|---|---|---|---|
Message queue | Named pipe | cpp-ipc | Message queue | cpp-ipc | |
Average | 0.952 | 21.9 | 253.7 | 61.5 | 47.0 |
Median | 0.900 | 19.7 | 220.3 | 54.0 | 45.9 |
P95 | 1.10 | 28.5 | 488.4 | 89.1 | 60.1 |
P99 | 1.20 | 56.5 | 588.9 | 119.5 | 79.1 |