Thursday, January 3, 2008


The main purpose of using PF_UNIX is local interconnection. Sometimes you need some daemon to listen on the localhost for the incoming connections to perform actions(data bus, server backend, etc.). What's the main advantage of usage PF_UNIX comparably with localhost-only network sockets? There are some reasons you should use unix socket against localhost-only network sockets:

  • Reduce network load. Kernel doesn't need to wrap packets with tcp/ip/ethernet/... layers
  • Higher security.Unix socket is a file in the filesystem. It has access permissions. So you may restrict access to it for all except specified user/group
  • Anonymity.You may call socketpair to create anonymous connected unix sockets.
Why not to use pipes?
  • For two-side interconnection you need two pipes: one for reading, another one for writing.
  • Using socket paradigm you can quickly switch to network sockets if you need.
  • Using socket you can interconnect with arbitrary[system limited] amount of connections easily*
* - you can do that with pipes too using own-defined messaging protocol. It'd would look like datagram socket communication semantics.


Cheba said...

Umm... dbus?

Ni@m said...

dbus might be heavy enough sometimes.
And you have dbus to be installed on the box ;).
Unix sockets allow you to use system-implemented intercommunication mechanism. You don't need anything except glibc =).

And btw dbus has really poor docs =/.