views:

488

answers:

3

Hi.

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

Thanks.

A: 

Both are inter process communication (IPC) mechanisms. UNIX domain sockets are use 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.

stacker
+8  A: 

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.

Kornel Kisielewicz
+1 for the comparison.
jldupont
Thanks for the detailed answer and comparison!
SyBer
+1  A: 

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.

Nikolai N Fetissov
I believe that what separates Unix Domain sockets from normal sockets is that a write on one end goes directly into the receiving buffer(s) on the other end. So there are not necessarily extra copies. This may still be more copies than are required if you can figure out a way for apps to actually use the same object in shared memory rather than copy it out.
Joseph Garvin
There are at least two extra copies - from user to kernel, and then back.
Nikolai N Fetissov