Skip to content

Commit b9bab5b

Browse files
committed
initial release
1 parent 3a67dda commit b9bab5b

8 files changed

Lines changed: 475 additions & 125 deletions

File tree

CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
project(shared_memory_boost_ipc LANGUAGES CXX)
3+
4+
if(NOT UNIX OR APPLE)
5+
message(FATAL_ERROR "This project supports Linux only.")
6+
endif()
7+
8+
set(CMAKE_CXX_STANDARD 17)
9+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
10+
11+
find_package(Boost REQUIRED COMPONENTS)
12+
13+
add_subdirectory(writer)
14+
add_subdirectory(reader)
15+
16+
###################################
17+
# Building commands (Case Rocky Linux)
18+
###################################
19+
# rm -rf build
20+
#
21+
# cmake -S . -B build -DBOOST_ROOT=/usr -DBOOST_INCLUDEDIR=/usr/include -DBOOST_LIBRARYDIR=/usr/lib64 -DBoost_NO_SYSTEM_PATHS=ON -DBoost_DEBUG=ON
22+
#
23+
# cmake --build build
24+
#
25+
26+
####################################
27+
# NOTICE:
28+
####################################
29+
# Current shared memory is not erased automatically.
30+
# For reader/writer running sequence.
31+
# If you remove shared memory, run writer with --remove option.
32+
#
33+

README.ko.md

Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,68 @@
1-
# C++ 프로젝트용 .gitignore
1+
# 공유 메모리 Boost IPC
22

3-
[English](README.md)
3+
[English README](README.md)
44

5-
---
5+
이 프로젝트는 [Boost.Interprocess](https://www.boost.org/doc/libs/release/doc/html/interprocess.html)를 활용한 공유 메모리 기반의 프로세스 간 통신(IPC) 예제를 제공합니다. 여러 프로세스가 공유 메모리를 통해 데이터를 효율적으로 교환하는 방법을 리더와 라이터 예제를 통해 보여줍니다.
66

7-
`.gitignore` 파일은 다양한 IDE, 에디터, 빌드 시스템을 포괄하는 **C++ 프로젝트 전용 종합 무시 목록**입니다.
7+
## 주요 특징
8+
- Boost.Interprocess를 이용한 공유 메모리 통신
9+
- 라이터 및 리더 예제 프로그램 제공
10+
- CMake 기반 빌드 시스템
11+
- 손쉬운 통합을 위한 헤더 전용 인터페이스
812

9-
## 포함된 환경
10-
11-
- **IDE / 에디터**:
12-
- Visual Studio 2019/2022
13-
- Visual Studio Code (VSCode)
14-
- CLion / JetBrains IDE
15-
- Qt Creator
16-
- Eclipse CDT
17-
- Xcode
18-
- Neovim / Vim
19-
- Emacs
20-
- Sublime Text
21-
- Atom
22-
- Code::Blocks
23-
- CodeLite
24-
- KDevelop
25-
- NetBeans (C/C++)
26-
- Geany
27-
- Dev-C++
28-
- JetBrains Fleet
29-
- Kate
30-
31-
- **빌드 시스템**:
32-
- CMake
33-
- Ninja
34-
- Meson
35-
- SCons
36-
37-
- **패키지 관리자**:
38-
- Conan
39-
- vcpkg
40-
- Hunter
41-
- CPM.cmake
42-
43-
- **언어 서버**:
44-
- clangd
45-
- ccls
46-
- gtags / ctags
47-
48-
## 목적
49-
50-
`.gitignore`의 목적은 다음과 같습니다:
51-
- 빌드 과정에서 생성되는 임시 산출물을 저장소에서 제거
52-
- 로컬 IDE/에디터 설정 파일을 커밋하지 않도록 방지
53-
- 사용자/PC별로 달라지는 캐시 및 로그 파일 제외
54-
55-
## 사용 방법
56-
57-
C++ 프로젝트의 루트 디렉터리에 `.gitignore` 파일을 배치합니다:
58-
59-
```bash
60-
curl -o .gitignore https://example.com/path/to/.gitignore
13+
## 프로젝트 구조
14+
```
15+
shared_memory_boost_ipc/
16+
├── include/
17+
│ └── shared_ipc.hpp # 공유 메모리 IPC 인터페이스
18+
├── reader/
19+
│ ├── reader.cpp # 리더 예제
20+
│ └── CMakeLists.txt # 리더 빌드 설정
21+
├── writer/
22+
│ ├── writer.cpp # 라이터 예제
23+
│ └── CMakeLists.txt # 라이터 빌드 설정
24+
├── CMakeLists.txt # 최상위 빌드 설정
25+
├── LICENSE # 라이선스 정보
26+
├── README.md # 프로젝트 문서
6127
```
6228

63-
Git은 `.gitignore`에 명시된 패턴과 일치하는 파일/디렉터리를 자동으로 무시합니다.
64-
65-
## 참고 사항
66-
67-
- `.gitignore`에 포함되어 있어도 특정 파일을 강제로 추가하려면 `git add -f <file>` 명령을 사용하세요.
68-
- `Makefile`을 직접 관리하는 경우, `.gitignore``Makefile` 항목을 삭제하세요.
29+
## 필요 환경
30+
- C++17 이상
31+
- [Boost](https://www.boost.org/) 라이브러리 (Interprocess 모듈)
32+
- CMake 3.10+
33+
34+
## 빌드 방법
35+
1. Boost를 설치하세요 (`boost_system``boost_interprocess` 모듈 필요).
36+
2. 저장소를 클론합니다:
37+
```sh
38+
git clone https://github.com/JayTwoLab/shared_memory_boost_ipc.git
39+
cd shared_memory_boost_ipc
40+
```
41+
3. 빌드 디렉터리를 만들고 CMake를 실행합니다:
42+
```sh
43+
mkdir build
44+
cd build
45+
cmake ..
46+
cmake --build .
47+
```
48+
- For fast search boost
49+
```sh
50+
cmake -S . -B build -DBOOST_ROOT=/usr -DBOOST_INCLUDEDIR=/usr/include -DBOOST_LIBRARYDIR=/usr/lib64 -DBoost_NO_SYSTEM_PATHS=ON -DBoost_DEBUG=ON
51+
```
52+
4. 리더와 라이터 실행 파일이 각 폴더에 생성됩니다.
6953

54+
## 사용 방법
55+
1. 라이터 프로세스를 실행하여 공유 메모리에 데이터를 작성합니다:
56+
```sh
57+
./writer/writer
58+
```
59+
2. 리더 프로세스를 실행하여 공유 메모리에서 데이터를 읽습니다:
60+
```sh
61+
./reader/reader
62+
```
63+
64+
## 라이선스
65+
이 프로젝트는 저장소의 LICENSE 파일에 명시된 조건에 따라 라이선스가 적용됩니다.
66+
67+
## 참고 자료
68+
- [Boost.Interprocess 공식 문서](https://www.boost.org/doc/libs/release/doc/html/interprocess.html)

README.md

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,68 @@
1-
# C++ Project .gitignore
1+
# Shared Memory Boost IPC
22

3+
[Korean README](README.ko.md)
34

4-
[Korean](README.ko.md)
5+
This project demonstrates inter-process communication (IPC) using shared memory with [Boost.Interprocess](https://www.boost.org/doc/libs/release/doc/html/interprocess.html). It provides simple reader and writer examples to show how multiple processes can exchange data efficiently via shared memory.
56

6-
---
7+
## Features
8+
- Shared memory communication using Boost.Interprocess
9+
- Example writer and reader applications
10+
- CMake-based build system
11+
- Header-only interface for easy integration
712

8-
This `.gitignore` file is a **comprehensive ignore list** for C++ projects, covering a wide range of IDEs, editors, and build systems.
9-
10-
## Included Environments
11-
12-
- **IDEs / Editors**:
13-
- Visual Studio 2019/2022
14-
- Visual Studio Code (VSCode)
15-
- CLion / JetBrains IDEs
16-
- Qt Creator
17-
- Eclipse CDT
18-
- Xcode
19-
- Neovim / Vim
20-
- Emacs
21-
- Sublime Text
22-
- Atom
23-
- Code::Blocks
24-
- CodeLite
25-
- KDevelop
26-
- NetBeans (C/C++)
27-
- Geany
28-
- Dev-C++
29-
- JetBrains Fleet
30-
- Kate
31-
32-
- **Build systems**:
33-
- CMake
34-
- Ninja
35-
- Meson
36-
- SCons
37-
38-
- **Package managers**:
39-
- Conan
40-
- vcpkg
41-
- Hunter
42-
- CPM.cmake
43-
44-
- **Language servers**:
45-
- clangd
46-
- ccls
47-
- gtags / ctags
48-
49-
## Purpose
50-
51-
This `.gitignore` aims to:
52-
- Keep repositories clean from temporary build outputs.
53-
- Avoid committing local IDE/editor configuration files.
54-
- Exclude cache and log files that are machine/user-specific.
55-
56-
## Usage
57-
58-
Place the `.gitignore` file in the root of your C++ project:
59-
60-
```bash
61-
curl -o .gitignore https://example.com/path/to/.gitignore
13+
## Project Structure
14+
```
15+
shared_memory_boost_ipc/
16+
├── include/
17+
│ └── shared_ipc.hpp # Shared memory IPC interface
18+
├── reader/
19+
│ ├── reader.cpp # Reader example
20+
│ └── CMakeLists.txt # Reader build config
21+
├── writer/
22+
│ ├── writer.cpp # Writer example
23+
│ └── CMakeLists.txt # Writer build config
24+
├── CMakeLists.txt # Top-level build config
25+
├── LICENSE # License information
26+
├── README.md # Project documentation
6227
```
6328

64-
Git will automatically ignore files and directories matching the patterns.
65-
66-
## Notes
67-
68-
- If you need to track some files listed in `.gitignore`, use `git add -f <file>` to force-add them.
69-
- If you are manually managing `Makefile`, remove the `Makefile` entry in this `.gitignore`.
29+
## Prerequisites
30+
- C++17 or later
31+
- [Boost](https://www.boost.org/) library (Interprocess module)
32+
- CMake 3.10+
33+
34+
## Build Instructions
35+
1. Install Boost (ensure `boost_system` and `boost_interprocess` are available).
36+
2. Clone this repository:
37+
```sh
38+
git clone https://github.com/JayTwoLab/shared_memory_boost_ipc.git
39+
cd shared_memory_boost_ipc
40+
```
41+
3. Create a build directory and run CMake:
42+
```sh
43+
mkdir build
44+
cd build
45+
cmake ..
46+
cmake --build .
47+
```
48+
- For fast search boost
49+
```sh
50+
cmake -S . -B build -DBOOST_ROOT=/usr -DBOOST_INCLUDEDIR=/usr/include -DBOOST_LIBRARYDIR=/usr/lib64 -DBoost_NO_SYSTEM_PATHS=ON -DBoost_DEBUG=ON
51+
```
52+
4. The executables for the reader and writer will be built in their respective folders.
7053

54+
## Usage
55+
1. Start the writer process to create and write data to shared memory:
56+
```sh
57+
./writer/writer
58+
```
59+
2. Start the reader process to read data from shared memory:
60+
```sh
61+
./reader/reader
62+
```
63+
64+
## License
65+
This project is licensed under the terms of the LICENSE file in this repository.
66+
67+
## References
68+
- [Boost.Interprocess Documentation](https://www.boost.org/doc/libs/release/doc/html/interprocess.html)

include/shared_ipc.hpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#pragma once
2+
3+
#include <boost/interprocess/managed_shared_memory.hpp>
4+
#include <boost/interprocess/shared_memory_object.hpp>
5+
#include <boost/interprocess/sync/interprocess_mutex.hpp>
6+
#include <boost/interprocess/sync/interprocess_condition.hpp>
7+
8+
#include <atomic>
9+
#include <cstdint>
10+
#include <csignal>
11+
#include <cstring>
12+
#include <string>
13+
#include <thread>
14+
#include <chrono>
15+
16+
namespace ipc
17+
{
18+
namespace bip = boost::interprocess;
19+
20+
inline constexpr const char* k_segment_name = "demo_shared_segment"; // name of the shared memory segment
21+
inline constexpr const char* k_object_name = "demo_shared_object"; // name of the shared object within the segment. sub object of shared memory segment.
22+
inline constexpr std::size_t k_segment_size = 1 * 1024 * 1024; // 1 MB. you must ensure this is enough for your data. check sizeof(shared_payload).
23+
24+
struct shared_payload
25+
{
26+
std::uint32_t magic = 0xAABBCCDD; // unique identifier for the shared object
27+
std::uint32_t version = 1; // version of the shared object structure
28+
29+
std::uint64_t sequence = 0; // incrementing sequence number
30+
std::uint64_t unix_ms = 0; // timestamp in milliseconds since epoch
31+
32+
char message[256]{}; // user data. you can adjust size as needed.
33+
34+
bip::interprocess_mutex mutex; // mutex for synchronizing access
35+
bip::interprocess_condition cond; // condition variable for signaling updates
36+
};
37+
38+
inline void copy_cstr(char* dst, std::size_t dst_size, const char* src)
39+
{
40+
if (!dst || dst_size == 0) return;
41+
if (!src) { dst[0] = '\0'; return; }
42+
std::strncpy(dst, src, dst_size - 1);
43+
dst[dst_size - 1] = '\0';
44+
}
45+
46+
class shutdown_flag
47+
{
48+
public:
49+
shutdown_flag()
50+
{
51+
instance_ptr() = this;
52+
std::signal(SIGINT, &shutdown_flag::signal_handler);
53+
std::signal(SIGTERM, &shutdown_flag::signal_handler);
54+
}
55+
56+
bool is_set() const
57+
{
58+
return stop_.load(std::memory_order_relaxed);
59+
}
60+
61+
private:
62+
static shutdown_flag*& instance_ptr()
63+
{
64+
static shutdown_flag* ptr = nullptr;
65+
return ptr;
66+
}
67+
68+
static void signal_handler(int)
69+
{
70+
if (instance_ptr())
71+
instance_ptr()->stop_.store(true, std::memory_order_relaxed);
72+
}
73+
74+
std::atomic<bool> stop_{false};
75+
};
76+
77+
inline std::uint64_t now_unix_ms()
78+
{
79+
using namespace std::chrono;
80+
return static_cast<std::uint64_t>(
81+
duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count()
82+
);
83+
}
84+
85+
inline void sleep_ms(int ms)
86+
{
87+
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
88+
}
89+
90+
} // namespace ipc

0 commit comments

Comments
 (0)