139
votes

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

2
@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
@acid: Yes, linking related questions and explaining what question you still have is always a good idea. - Ben Voigt
This article pretty much summarized it well. Demystifying Unix Domain Sockets: thomasstover.com/uds.html - Cong Ma

2 Answers

120
votes

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

73
votes

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.