UNIX Domain sockets vs Shared Memory (Mapped File)

后端 未结 4 473
心在旅途
心在旅途 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:21

    It's more a question of design, than speed (Shared Memory is faster), domain sockets are definitively more UNIX-style, and do a lot less problems. In terms of choice know beforehand:

    Domain Sockets advantages

    • blocking and non-blocking mode and switching between them
    • you don't have to free them when tasks are completed

    Domain sockets disadvantages

    • must read and write in a linear fashion

    Shared Memory advantages

    • non-linear storage
    • will never block
    • multiple programs can access it

    Shared Memory disadvantages

    • need locking implementation
    • need manual freeing, even if unused by any program

    That's all I can think of now. However, I'd go with domain sockets any day -- not to mention that it's a lot easier then to reimplement them to do distributed computing. The speed gain of Shared Memory will be lost because of the need of a safe design. However, if you know exactly what you're doing, and use the proper kernel calls, you can achieve greater speed with Shared Memory.

提交回复
热议问题