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

后端 未结 4 903
轻奢々
轻奢々 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:19

    Choosing between the POSIX shm_open/mmap interface and the older System V shmop one won't make a big difference, because after the initialization system calls, you end up with the same situation: a memory area that is shared between various processes. If your system supports it, I'd recommend to go with shm_open/mmap, because this is a better designed interface.

    You then use the shared memory area as a common blackboard where all processes can scribble their data. The difficult part is to synchronize the processes accessing this area. Here I recommend to avoid concocting your own synchronization scheme, which can be fiendishly difficult and error-prone. Instead, use the existing working socket-based implementation for synchronizing access between processes, and use the shared memory only for transferring large amounts of data between processes. Even with this scheme you'll need a central process to coordinate the allocation of buffers, so this scheme is worth it only if you have very large volumes of data to transfer. Alternatively, use a synchronization library, like Boost.Interprocess.

提交回复
热议问题