I'm trying to choose between pipes and unix sockets for an IPC mechanism.
Both support the select()
and epoll()
functions which is great.
Now, pipes have a 4kB (as of today) "atomic" write, which is guaranteed by the Linux Kernel.
Does such a feature exists in the case of unix sockets? I couldn't find any document stating this explicitely.
Say I use a UNIX socket and I write x bytes of data from my client. Am I sure that these x bytes will be written on the server end of the socket when my server's select()
cracks?
On the same subject, would using SOCK_DGRAM ensure that writes are atomic (if such a guarantee is possible), since datagrams are supposed to be single well-defined messages?
What would then be the difference using SOCK_STREAM as a transfer mode?
Thanks in advance.
Unix datagram socketsWrites using the send family of functions on datagram sockets are indeed guaranteed to be atomic.
yes... two unix processes can write to a single (shared) TCP socket ... but I would consider this a design flaw. write might be atomic, but it can be partial, causing interleaved data to be written between write calls. An application level syncing element will be required to circumvent this issue.
A UNIX socket, AKA Unix Domain 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.
To create a UNIX domain socket, use the socket function and specify AF_UNIX as the domain for the socket. The z/TPF system supports a maximum number of 16,383 active UNIX domain sockets at any time. After a UNIX domain socket is created, you must bind the socket to a unique file path by using the bind function.
Yes the non-blocking capacity is usually 4KB, but for maximum portability you'd probably be better off using the PIPE_BUF
constant. An alternative is to use non-blocking I/O.
More information than you want to know in man 7 pipe
.
Writes using the send
family of functions on datagram sockets are indeed guaranteed to be atomic. In the case of Linux, they're reliable as well, and preserve ordering. (which makes the recent introduction of SOCK_SEQPACKET
a bit confusing to me) Much information about this in man 7 unix
.
The maximum datagram size is socket-dependent. It's accessed using getsockopt/setsockopt
on SO_SNDBUF
. On Linux systems, it ranges between 2048 and wmem_max
, with a default of wmem_default
. For example on my system, wmem_default = wmem_max = 112640
. (you can read them from /proc/sys/net/core
) Most relevant documentation about this is in man 7 socket
around the SO_SNDBUF
option. I recommend you read it yourself, as the capacity doubling behavior it describes can be a bit confusing at first.
Stream sockets only work connected. This mostly means they can only communicate with one peer at a time. As streams, they're not guaranteed to preserve "message boundaries".
Datagram sockets are disconnected. They can (theoretically) communicate with multiple peers at a time. They preserve message boundaries.
[I suppose the new SOCK_SEQPACKET
is in between: connected and boundary-preserving.]
On Linux, both are reliable and preserve message ordering. If you use them to transmit stream data, they tend to perform similarly. So just use the one that matches your flow, and let the kernel handle buffering for you.
Crude benchmark comparing stream, datagram, and pipes:
# unix stream 0:05.67
socat UNIX-LISTEN:u OPEN:/dev/null &
until [[ -S u ]]; do :;done
time socat OPEN:large-file UNIX-CONNECT:u
# unix datagram 0:05.12
socat UNIX-RECV:u OPEN:/dev/null &
until [[ -S u ]]; do :;done
time socat OPEN:large-file UNIX-SENDTO:u
# pipe 0:05.44
socat PIPE:p,rdonly=1 OPEN:/dev/null &
until [[ -p p ]]; do :;done
time socat OPEN:large-file PIPE:p
Nothing statistically significant here. My bottleneck is likely reading large-file.
Say I use a UNIX socket and I write x bytes of data from my client. Am I sure that these x bytes will be written on the server end of the socket when my server's
select()
cracks?
If you are using AF_UNIX
SOCK_STREAM
socket, there is no such guarantee, that is, data written in one write/send()
may require more than one read/recv()
call on the receiving side.
On the same subject, would using SOCK_DGRAM ensure that writes are atomic (if such a guarantee is possible), since datagrams are supposed to be single well-defined messages?
On there other hand, AF_UNIX
SOCK_DGRAM
sockets are required to preserve the datagram boundaries and be reliable. You should get EMSGSIZE error if send()
can not transmit the datagram atomically. Not sure what happens for write()
as the man page does not say that it can report EMSGSIZE (although man pages sometimes do not list all errors returned). I would try overflowing the receiver's buffer with big sized datagrams to see which errors exactly send/write()
report.
One advantage of using UNIX sockets over pipes is the bigger buffer size. I don't remember exactly what is the limit of pipe's kernel buffer, but I remember not having enough of it and not being able to increase it (it is a hardcoded kernel constant). fast_producer_process | slow_consumer_process
was orders of magnitude slower than fast_producer_process > file && file > slow_consumer_process
due to insufficient pipe buffer size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With