54

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

Thanks.

5 Answers 5

85

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.

2
  • Thanks for the detailed answer and comparison!
    – SyBer
    Jan 21, 2010 at 13:18
  • 2
    need manual freeing, even if unused by any program can be battled with with /dev/ashmem
    – Vi.
    Jan 17, 2014 at 16:05
15

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.

3
  • 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. Mar 28, 2010 at 22:28
  • There are at least two extra copies - from user to kernel, and then back. Mar 28, 2010 at 23:39
  • Isn't this only true if more than 512 bytes? (4096 bytes on Linux)
    – JSON
    Jul 1, 2019 at 11:46
6

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.

5

@Kornel Kisielewicz 's answer is good IMO. Just adding my own results here for sockets, not only Unix domain sockets.

Shared Memory

  • Performance is very high. No copies with RAW access data. Fastest access for sure.
  • Synchronization needed. Design not so easy to setup for complex cases.
  • Fixed size. Growing shared memory is doable but memory has to be unmapped first, growed, and then remapped.
  • Signaling mechanism can be quite slow, see here : Boost.Interprocess notify() performance. Especially if you want to do lots of exchanges between processes. Signaling mechanism not so easy to setup also.

Sockets

  • Easy to setup.
  • Can be used on different machines.
  • No complex synchronisation needed.
  • Size is not a problem if you use TCP. Simple design with header containing the packet size and then send the data.
  • Ping/Pong exchange is fast because it can be treated as hardware interruption by the OS.
  • Performance is average: a few copies of data are made.
  • High CPU consumption compared to shared memory. Sockets calls are not that cheap if you use them a lot.

In my tests, exchanges of small chunks of data (around 1MByte/second) shows no real advantage for shared memory. I would even say that ping/pong exchanges were faster using TCP (due to simple and efficient signaling mechanism). BUT when exchanging large amount of data (around 200MBytes/second), I had 20% CPU consumption with sockets, compared to 3% CPU using shared memory. So a huge win for shared memory in terms of CPU because read and write socket calls are not cheap.

-1

In this case - sockets are faster. Writing to shared memory is faster then any IPC but writing to a memory mapped file and writing to shared memory are 2 completely different things.

when writing to a memory mapped file you need to "flush" what was written to the shared memory to an actual binded file (not exactly, the flush is being done for you), so you copy your data first to the shared memory, and then you copy it again (flush) to the actual file and that is super duper expansive - more then anything, even more then writing to socket, you are gaining nothing by doing that.

2
  • 4
    This isn't true. It's relatively simple to set up shared memory, via a memory-mapped file (which is the normal mechanism), and for the data sharing to happen the fast way. Sep 15, 2015 at 15:20
  • Also, just to prevent confusion, mmaped files exists in memory not on phisical disks, and does not have the same performance limits created by physical disk mechanisms and their related drivers. Data is only written to disk if using a persisted mmaped file.
    – JSON
    Jul 1, 2019 at 11:40

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.