Shared Memory or mmap - Linux C/C++ IPC

后端 未结 4 905
轻奢々
轻奢々 2020-12-23 12:21

The context is Inter-Process-Communication where one process(\"Server\") has to send fixed-size structs to many listening processes(\"Clients\") running on the same machine.

4条回答
  •  既然无缘
    2020-12-23 13:07

    I'd use mmap together with shm_open to map shared memory into the virtual address space of the processes. This is relatively direct and clean:

    • you identify your shared memory segment with some kind of symbolic name, something like "/myRegion"
    • with shm_open you open a file descriptor on that region
    • with ftruncate you enlarge the segment to the size you need
    • with mmap you map it into your address space

    The shmat and Co interfaces have (at least historically) the disadvantage that they may have a restriction in the maximal amount of memory that you can map.

    Then, all the POSIX thread synchronization tools (pthread_mutex_t, pthread_cond_t, sem_t, pthread_rwlock_t, ...) have initialization interfaces that allow you to use them in a process shared context, too. All modern Linux distributions support this.

    Whether or not this is preferable over sockets? Performance wise it could make a bit of a difference, since you don't have to copy things around. But the main point I guess would be that, once you have initialized your segment, this is conceptually a bit simpler. To access an item you'd just have to take a lock on a shared lock, read the data and then unlock the lock again.

    As @R suggests, if you have multiple readers pthread_rwlock_t would probably the best lock structure to use.

提交回复
热议问题