164

After looking at a unix named socket and i thought they were named pipes. I looked at name pipes and didnt see much of a difference. I saw they were initialized differently but thats the only thing i notice. Both use the C write/read function and work alike AFAIK.

Whats 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)?

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
  • 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) –  Feb 28 '12 at 02:28
  • 1
    @acid: Yes, linking related questions and explaining what question you still have is always a good idea. – Ben Voigt Feb 28 '12 at 04:12
  • 3
    This article pretty much summarized it well. Demystifying Unix Domain Sockets: http://www.thomasstover.com/uds.html – Cong Ma Mar 03 '13 at 03:59
  • 3
    Broken link: http://www.techdeviancy.com/uds.html – mcdado Dec 26 '19 at 17:09

2 Answers2

144

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().

caf
  • 233,326
  • 40
  • 323
  • 462
  • 25
    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 May 14 '16 at 23:58
92

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.

Community
  • 1
  • 1
jtoberon
  • 8,706
  • 1
  • 35
  • 48
  • 2
    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? –  Feb 28 '12 at 02:29
  • 9
    @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 Feb 28 '12 at 04:14
  • @BenVoigt I don't think there's any such thing as a datagram-mode pipe. Like what the answer says from wikipedia. – xaxxon Aug 31 '13 at 23:36
  • 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](http://msdn.microsoft.com/en-us/library/aa365150), you will see the `PIPE_TYPE_MESSAGE` flag. – Ben Voigt Sep 01 '13 at 01:58
  • 1
    @BenVoigt well, datagram socket packet delivery is unreliable, so a write won't necessarily generate a read call. Perhaps for local sockets, but that isn't clear from your comment. So regardless it has problems. – xaxxon Sep 01 '13 at 08:00
  • 4
    @xaxxon: Both pipes and unix domain sockets *are* local, so lossless is the receiver is emptying its queues at all. – Ben Voigt Sep 01 '13 at 13:37
  • 13
    Yes, *unlike* UDP, datagram Unix domain sockets are *guaranteed*, in-order delivery. – jtchitty Oct 21 '16 at 05:37