15

I am doing a study about Unix domain socket. Especially about how does it work. I googled many times with many keywords but the results are all about API, system calls, how to use it, examples ... . I have read about Pipe and FIFO too because Unix Domain socket is said to be the same with Pipe and FIFO but I still want to know more about the concept/priciples of Unix Domain Socket. How does it work? (Maybe at the kernel level because Wiki say this:"This allows two processes to open the same socket in order to communicate. However, communication occurs entirely within the operating system kernel."

I still wonder why Unix domain Socket documentaries is less than Pipe or FIFO? Maybe because it was born so many years ago?

Could anyone show me any ideas or which books/links to read?

Thanks in advance!

ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
leokaka
  • 151
  • 1
  • 1
  • 6
  • For a book, try *Unix Network Programming, Volume 1* by W. Richard Stevens. The third edition is the latest, but a used copy of the second edition will cost much less and be sufficient, if you can't find it in your local library. – rob mayoff Feb 18 '13 at 03:49
  • Thanks @robmayoff for your books, I read it. But it talks about UDS API :(. I need information about, maybe, how data is sended or received in kernel level. Something like they talk about pipe here: http://www.tldp.org/LDP/lpg/node10.html#SECTION00721000000000000000 Thank you! – leokaka Feb 21 '13 at 01:12
  • [*TCP/IP Illustrated, Vol. 3: TCP for Transactions, HTTP, NNTP, and the UNIX Domain Protocols*](http://amzn.com/0201634953), also by W. Richard Stevens. – rob mayoff Feb 21 '13 at 03:28
  • @robmayoff :thanks, I'm reading that book now! – leokaka Feb 21 '13 at 04:43
  • A UDS is actually **more** than a FIFO, not less. – Omnifarious Dec 05 '17 at 01:30

1 Answers1

31

Unix sockets are used as any other socket types. This means, that socket system calls are used for them. The difference between FIFOs and Unix sockets, is that FIFO use file sys calls, while Unix sockets use socket calls.

Unix sockets are addressed as files. It allows to use file permissions for access control.

Unix sockets are created by socket sys call (while FIFO created by mkfifo). If you need client socket, you call connect, passing it server socket address. If you need server socket, you can bind to assign its address. While, for FIFO open call is used. IO operation is performed by read/write.

Unix socket can distinguish its clients, while FIFO cannot. Info about peer is provided by accept call, it returns address of peer.

Unix sockets are bidirectional. This means that every side can perform both read and write operations. While, FIFOs are unidirectional: it has a writer peer and a reader peer.

Unix sockets create less overhead and communication is faster, than by localhost IP sockets. Packets don't need to go through network stack as with localhost sockets. And as they exists only locally, there is no routing.

If you need more details about, how Unix sockets work at kernel level, please, look at net/unix/af_unix.c file in Linux kernel source.

Sabyasachi Patra
  • 650
  • 4
  • 12
  • If you are satisfied by my answer, please set it as best answer. Or add to comment, what's else need to be explained. – Андрей Москвичёв Feb 17 '13 at 17:33
  • Thank you for your information but I read about these information already. :( could you please give me some more details about how it send and receive data? Does it use files I/O? Thanks! – leokaka Feb 18 '13 at 01:40
  • Unix sockets use the same api as other types of sockets. They are created by socket sys call, and then you can connect to server socket by connect call, or bind it (if you need server socket). read/write calls can be used to perform IO. – Андрей Москвичёв Feb 18 '13 at 05:49
  • Please, look at this link http://tkhanson.net/cgit.cgi/misc.git/plain/unixdomain/Unix_domain_sockets.html. I think, it will be helpful. – Андрей Москвичёв Feb 18 '13 at 05:50
  • 1
    Thank you for your information, Андрей Москвичёв. I agree with you about how to use UDS through socket API. But still want to know exactly kernel does when the send/receive function is called. They are files, but how they can connect, send and receive data with each other? I need information about what happening at the kernel level. Some thing about the concept of UDS, just like Pipes in this link: http://www.tldp.org/LDP/lpg/node10.html#SECTION00721000000000000000 Thank you! – leokaka Feb 18 '13 at 08:40
  • Is there any ideas my friends? – leokaka Feb 20 '13 at 00:45
  • There is one more important difference. A UDS is an endpoint that you can call `bind` on and then `accept` new connections. A FIFO is just a special file that leads to a small in-memory buffer. Anybody who writes to it writes to the same buffer, anybody who reads reads from the same buffer. If you use `connect` and `accept` with a UDS each new connection gets its own buffer. – Omnifarious Dec 05 '17 at 01:33
  • Your text doesn't answer the question _How does it work_ You just copy/pasted general facts about sockets from the Internet. You don't explain how they work, how a socket can _listen_. – Green Apr 09 '18 at 21:43