Unix Domain Socket
lastmod 2025-01-24created 2015-08-0412 edits
A Unix domain socket or IPC socket (inter-process communication socket) is a data communications endpoint for exchanging data between processes executing within the same host operating system. Like named pipes, Unix domain sockets support transmission of a reliable stream of bytes (SOCK_STREAM, compare to TCP). They additionally support ordered and reliable transmission of datagrams (SOCK_SEQPACKET), or unordered and unreliable transmission of the same (SOCK_DGRAM, compare to UDP). The Unix domain socket facility is a standard component of POSIX operating systems.
Preview
Unix Domain Socket (hereafter UDS) uses the socket API without modification, and differs from the port-based Inernet Domain Socket in that it uses the local system's file system for communication between internal processes.
Looking at the file created for communication with ls, it looks like this.
You can see that the file type has an "s", and the file size is 0. This is because, as with FIFO, messages are not stored in the file but are passed to the kernel and handled there.
It is very similar to FIFO in that it communicates through a file and messages are managed inside the kernel, but unlike FIFO it has the characteristic of supporting bidirectional communication. This makes it very easy to build a server/client model that accepts multiple clients. It also has the advantage of being more than twice as efficient as external communication through Inet sockets.
In many cases, UDS is widely used when somewhat complex communication between internal processes is needed. Unlike communication at the INET layer, which goes through all four TCP/IP layers, with UDS messages are passed only from the application layer down to the TCP layer and then go straight back up to the application layer. For details on the TCP/IP layers, refer to TCP/IP Overview (2).
As mentioned above, it is more than twice as efficient as INET sockets, and one of the reasons is that it uses only 2 layers, compared to INET sockets, which must go through all 4 layers.
Abstract Domain Socket
TCP/IP vs Unix Domain Sockets vs Named PIPE
- linux - IPC performance: Named Pipe vs Socket - Stack Overflow
- Benchmark TCP/IP, Unix domain socket and Named pipe 1
UDS is a considerable improvement over TCP/IP. Specifically, queries per second (QPS) rose by about 15% when responses were relatively small, and an impressive 50% increase was observed for responses as large as 100KB. In other words, UDS performs much better when large amounts of data need to be transferred. Named pipes are as efficient as UDS for small response bodies, but much worse for large response bodies.
In short, you should use UDS as much as possible for local calls.
For reference, the performance difference between Unix Domain Sockets and Named PIPE seems to vary by PC/OS.
See also
- Asio:UDS: Sample code using Asio
- Abstract Domain Socket (Abstract Namespace)
- Inter-process communication (IPC)