175

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

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

154

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
  • 26
    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
97

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