20

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

2 Answers 2

35

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.

8
  • If you are satisfied by my answer, please set it as best answer. Or add to comment, what's else need to be explained. Commented Feb 17, 2013 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
    Commented Feb 18, 2013 at 1: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. Commented Feb 18, 2013 at 5:49
  • Please, look at this link tkhanson.net/cgit.cgi/misc.git/plain/unixdomain/…. I think, it will be helpful. Commented Feb 18, 2013 at 5: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: tldp.org/LDP/lpg/node10.html#SECTION00721000000000000000 Thank you!
    – leokaka
    Commented Feb 18, 2013 at 8:40
-1

The created socket file is used to tell server or client how to find the other side. The key problem: how data is sent or received by each other, is solved by unix buffer. that is when client writes, the kernel will copy this data into kernel buffer, and the server side will loop query whether there is data that's available to read. Totally it's a producer-consumer queue.

Not the answer you're looking for? Browse other questions tagged or ask your own question.