Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNIX Domain sockets vs Shared Memory (Mapped File)

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

Thanks.

like image 504
SyBer Avatar asked Jan 20 '10 13:01

SyBer


People also ask

Why are sockets preferred over shared memory?

Sockets are one-to-one. You need multiple sockets if you want to send the same thing to multiple processes. With shared memory, you can have multiple readers, and also multiple writers. Sockets are resource intensive.

Are UNIX domain sockets faster?

Unix domain sockets are often twice as fast as a TCP socket when both peers are on the same host. The Unix domain protocols are not an actual protocol suite, but a way of performing client/server communication on a single host using the same API that is used for clients and servers on different hosts.

Are UNIX domain sockets reliable?

Valid socket types in the UNIX domain are: SOCK_STREAM, for a stream-oriented socket; SOCK_DGRAM, for a datagram-oriented socket that preserves message boundaries (as on most UNIX implementations, UNIX domain datagram sockets are always reliable and don't reorder datagrams); and (since Linux 2.6.

What is the difference between Unix socket and TCP socket?

A UNIX socket is an inter-process communication mechanism that allows bidirectional data exchange between processes running on the same machine. IP sockets (especially TCP/IP sockets) are a mechanism allowing communication between processes over the network.


3 Answers

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.

like image 138
Kornel Kisielewicz Avatar answered Oct 07 '22 10:10

Kornel Kisielewicz


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.

like image 13
Nikolai Fetissov Avatar answered Oct 07 '22 12:10

Nikolai Fetissov


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.

like image 6
stacker Avatar answered Oct 07 '22 12:10

stacker