UNIX Domain sockets vs Shared Memory (Mapped File) UNIX Domain sockets vs Shared Memory (Mapped File) unix unix

UNIX Domain sockets vs Shared Memory (Mapped File)


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.


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.


Both are inter process communication (IPC) mechanisms.UNIX domain sockets are uses for communication between processes on one host similar as TCP-Sockets are used between different hosts.Shared memory (SHM) is a piece of memory where you can put data and share this between processes.SHM provides you random access by using pointers, Sockets can be written or read but you cannot rewind or do positioning.