UNIX Domain sockets vs Shared Memory (Mapped File)

后端 未结 4 470
心在旅途
心在旅途 2020-12-13 00:17

Can anyone tell, how slow are the UNIX domain sockets, compared to Shared Memory (or the alternative memory-mapped file)?

Thanks.

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 00:42

    In terms of speed shared memory is definitely the winner. With sockets you will have at least two copies of the data - from sending process to the kernel buffer, then from the kernel to the receiving process. With shared memory the latency will only be bound by the cache consistency algorithm between the cores on the box.

    As Kornel notes though, dealing with shared memory is more involved since you have to come up with your own synchronization/signalling scheme, which might add a delay depending on which route you go. Definitely use semaphores in shared memory (implemented with futex on Linux) to avoid system calls in non-contended case.

提交回复
热议问题