178

After looking at Unix named sockets, I thought they were Named pipes. I looked at Name pipes and didn't see much of a difference. I saw they were initialized differently but that's the only thing I noticed. Both use the C write/read function and work alike AFAIK.

  • What's the difference between Unix domain sockets and Named pipes?
  • When would I pick one over the other?
  • Which should I use by default (like how I use use vector by default in C++ than use deque, list or whatever else if I have needs)?
4
  • 1
    @GregHewgill: unfortunately that question is more of "what is IPC" rather than the difference i am asking :/. I did see that before posting, should i have linked and said its related? (it wasnt helpful to me)
    – user34537
    Commented Feb 28, 2012 at 2:28
  • 1
    @acid: Yes, linking related questions and explaining what question you still have is always a good idea.
    – Ben Voigt
    Commented Feb 28, 2012 at 4:12
  • 3
    This article pretty much summarized it well. Demystifying Unix Domain Sockets: thomasstover.com/uds.html
    – Cong Ma
    Commented Mar 3, 2013 at 3:59
  • 3
    Broken link: techdeviancy.com/uds.html
    – mcdado
    Commented Dec 26, 2019 at 17:09

2 Answers 2

158

UNIX-domain sockets are generally more flexible than named pipes. Some of their advantages are:

  • You can use them for more than two processes communicating (eg. a server process with potentially multiple client processes connecting);
  • They are bidirectional;
  • They support passing kernel-verified UID / GID credentials between processes;
  • They support passing file descriptors between processes;
  • They support packet and sequenced packet modes.

To use many of these features, you need to use the send() / recv() family of system calls rather than write() / read().

1
  • 28
    On the other hand, it should perhaps be said that names pipes have the advantage that they can be "connected to" via ordinary open(2) calls, which make them more well-suited for constructing ad-hoc pipelines between programs that normally only take filename arguments.
    – Dolda2000
    Commented May 14, 2016 at 23:58
99

One difference is that named pipes are one-way, so you'll need to use two of them in order to do two-way communication. Sockets of course are two way. It seems slightly more complicated to use two variables instead of one (that is, two pipes instead of one socket).

Also, the wikipedia article is pretty clear on the following point: "Unix domain sockets may be created as byte streams or as datagram sequences, while pipes are byte streams only."


Named pipes are, in fact, bi-directional but half-duplex. This means that communication may go either from end A to end B, or B to A, but never both at the same time.

7
  • 3
    hmm interesting +1. Do you by chance know what the difference is between bytestream and datagram? Maybe example in a sentance or two like you did already for this question?
    – user34537
    Commented Feb 28, 2012 at 2:29
  • 11
    @acidzombie: A datagram-mode pipe or socket maintains boundaries, so that one write call produces one read call. In stream-mode the data can be concatenated together in one long stream, so many writes can be read at once, or vice versa. (Windows has datagram pipes, according to jtoberon's answer Unix doesn't)
    – Ben Voigt
    Commented Feb 28, 2012 at 4:14
  • 2
    @xaxxon: Did you fully read my comment? The parenthesized portion explains that Windows has them even if Unix does not. And if you check the CreateNamedPipe documentation, you will see the PIPE_TYPE_MESSAGE flag.
    – Ben Voigt
    Commented Sep 1, 2013 at 1:58
  • 6
    @xaxxon: Both pipes and unix domain sockets are local, so lossless is the receiver is emptying its queues at all.
    – Ben Voigt
    Commented Sep 1, 2013 at 13:37
  • 15
    Yes, unlike UDP, datagram Unix domain sockets are guaranteed, in-order delivery.
    – jtchitty
    Commented Oct 21, 2016 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.