linuxipcatomicsocketpair

Is write to SOCK_SEQPACKET atomic?


What I mean atomic is success or failed and do nothing.

I know socketpair(AF_LOCAL, SOCK_STREAM) is not atomic, if multiple processes/threads call write(fd, buf, len), the return value of the write() maybe > 0 && < len and cause data out of order.

If multiple processes/threads write(buf, len) to a sock_fd which created by socketpair(AF_LOCAL, SOCK_SEQPACKET), is it atomic?

I check the Linux manual and found something about pipe() which says if the len is less than PIPE_BUF, the write/writev is atomic.

I found nothing about socketpair. I wrote a test code and found it seems that the SOCK_SEQPACKET is atomic, I write random length buffer to fd and the return value is always -1 or len.


Solution

  • Yes.

    Any interface that is datagram based (i.e. - the size you pass to write is visible to the person doing the read) must be atomic. There is no other way to guarantee that property.

    So SOCK_SEQPACKET, as well as SOCK_DGRAM, must be atomic in order to function.

    For that very same reason, SOCK_STREAM has no such atomicy guarantees.