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

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

    Apart from what's been suggested already, I'd like to offer another method: IPv6 Node/Interface Local Multicast, i.e. a multicast constrained to the loopback interface. http://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xml#ipv6-multicast-addresses-1

    At first this might seem quite heavyweight, but most OS implement loopback sockets in a zero-copy architecture. The page(s) mapped to the buf parameter passed to send will be assigned an additional mapping and marked as copy on write so that if the sending program overwrites the data therein, or deallocates the contents will be preserved.

    Instead of passing raw structs you should use a robust data structure. Netstrings http://cr.yp.to/proto/netstrings.txt and BSON http://bsonspec.org/ come to mind.

提交回复
热议问题