返回题库

共享内存配置

Shared Memory Config

专题
Systems & Architecture / 系统与架构
难度
L2
来源
MyntBit

题目详情

高频交易系统需要跨多进程超低延迟访问动态配置参数(如风控限额和策略标志)。POSIX 共享内存通过将同一内存段映射到多个进程地址空间,提供高效机制。

任务:实现 SharedConfig 类,使用 shm_open 和 mmap 创建跨进程共享内存区域。写入进程更新配置结构体,读取进程通过 acquire 内存序读取最新值。支持配置版本号检测更新。

英文原题

High-frequency trading systems require ultra-low latency access to dynamic configuration parameters like risk limits and strategy flags across multiple processes. POSIX shared memory provides an efficient mechanism for this by mapping memory segments directly into a process's address space, bypassing kernel overhead for subsequent read and write operations.
Task
Implement the ConfigStore class to manage a fixed-size array of double values in POSIX shared memory. The implementation must handle s

解析

问题分析

POSIX 共享内存允许多个进程共享同一物理内存区域。在配置分发中,主进程将配置写入共享内存,其他进程只读映射——配置更新对所有人即时可见,无需 IPC 消息。

实现

class SharedMemoryConfig {
    void* addr_; size_t len_;
public:
    SharedMemoryConfig(const char* name, size_t size, bool create) {
        int flags = create ? (O_CREAT | O_RDWR) : O_RDONLY;
        int fd = ::shm_open(name, flags, 0666);
        if (create) ::ftruncate(fd, size);
        len_ = size;
        addr_ = ::mmap(nullptr, len_, create ? (PROT_READ|PROT_WRITE) : PROT_READ,
                       MAP_SHARED, fd, 0);
        ::close(fd);
    }
    void* ptr() { return addr_; }
    ~SharedMemoryConfig() { ::munmap(addr_, len_); }
};

复杂度与边界

  • 时间复杂度:构造 O(1),读写 O(1)
  • 空间复杂度:O(size)
  • 边界条件:(1) 写入方需同步机制(如 seq_lock)通知读者 (2) shm 名必须以 / 开头 (3) shm_unlink 删除共享内存

英文解析

Analysis

POSIX shared memory allows multiple processes to share the same physical memory region. In configuration distribution, the primary process writes configuration to shared memory, and other processes read-only map it - configuration updates are instantly visible to everyone without IPC messages.

Solution

class SharedMemoryConfig {
    void* addr_; size_t len_;
public:
    SharedMemoryConfig(const char* name, size_t size, bool create) {
        int flags = create ? (O_CREAT | O_RDWR) : O_RDONLY;
        int fd = ::shm_open(name, flags, 0666);
        if (create) ::ftruncate(fd, size);
        len_ = size;
        addr_ = ::mmap(nullptr, len_, create ? (PROT_READ|PROT_WRITE) : PROT_READ,
                       MAP_SHARED, fd, 0);
        ::close(fd);
    }
    void* ptr() { return addr_; }
    ~SharedMemoryConfig() { ::munmap(addr_, len_); }
};

Complexity & Edge Cases

  • Time complexity: construct O(1), read/write O(1)
  • Space complexity: O(size)
  • Edge cases: (1) Writer needs synchronization (e.g., seq_lock) to notify readers (2) shm name must start with / (3) shm_unlink removes shared memory

Verification

Write config from primary process, verify readers see updates instantly. Test seq_lock synchronization pattern. Confirm shm_unlink properly cleans up.

Key Considerations

Shared memory is the fastest IPC mechanism for configuration distribution. In multi-process trading architectures (strategy + gateway + risk in separate processes), shared memory config enables instant propagation of risk limit changes without serialization overhead or message passing latency.