Friday, 31 March 2017

The difference between a Unix Domain Socket and a Named Pipe (fifo)

named pipes (fifo's) allow communication between different processes. They allow you to pipe data directly into a special file that is then read immediately by another process.

As an example we can use the 'mkfifo' command:

mkfifo /tmp/fifo

ls -la /tmp/fifo


Notice the precense of the 'p' bit - this indicates that the file is a named pipe / fifo.

Now let's tail it and pipe some data into it:

tail -f /tmp/fifo &

echo test data > /tmp/fifo

test data

A socket file in Linux (AKA a Unix Domain Socket) allows communication between - for example an server and multiple clients - in some respects it is similar to a named pipe (fifo) - however they provide some advantages such as bi-directional communication, passing file descriptors between processes and support packet and sequenced packet modes.

For example haproxy can utilise sockets:

ls -la /run/haproxy_admin.sock

srw-rw----. 1 root root 0 Mar 28 14:17 /run/haproxy_admin.sock

The 's' bit indicates that the file is a socket.

Socket are typically employed when network communication is not required, rather all communication is performed by processes on the local system.


Post a Comment