Search code examples
cmultithreadingunixposixunix-socket

Unix Domain Socket concurrent read/write on both ends


For many reasons, I would like to use unix domain sockets for IPC between two processes.

Each process reacts to asynchronous events of some specific kind from the outside world by writing to the socket and communicating this event to the second process and - at the same time - each process also needs to read data coming from the other socket to do some stuff. In substance, in this model there would be one socket and two threads per process: one for possibly blocking reads, and one for the writings.

I would like to know if it's possible to use unix domain sockets for concurrently reading and writing from/to each process independently, without making use of any explicitly locking in that safety would implicitly guaranteed by these kind of sockets. If yes, I'd also like to know where this guarantee is officially claimed.


Solution

  • The only relevant difference between an AF_LOCAL socket and an AF_INET socket is that AF_LOCAL sockets are local to the current computer. Creating an AF_LOCAL socket and binding it is no difference than creating an AF_INET socket and binding it to localhost.

    The path used for binding AF_LOCAL sockets is only used for connecting to the socket, nothing else.

    So if you create a connection-oriented AF_LOCAL socket (using SOCK_STREAM or SOCK_SEQPACKET) then each connection is unique and you can have multiple processes connecting through the same listening (passive) AF_LOCAL socket.