3

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.

Shachar Shemesh
  • 8,193
  • 6
  • 25
  • 57
alpha
  • 1,228
  • 1
  • 11
  • 26

1 Answers1

1

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.

Shachar Shemesh
  • 8,193
  • 6
  • 25
  • 57