RogueCSDev RogueCSDev - 4 months ago 14
Linux Question

Concurrent system calls in Linux

Simple as this question is, I haven't found anything about it by Googling. Is it safe to make concurrent calls to the Linux system, such as calling

socket()
on several threads at (what might be) the same time? Specifically, does the kernel guarantee thread-safety for
socket()
,
connect()
, and/or
send()
?

If not, why not? I'd really like to learn more about this topic and why system calls are or are not thread-safe.

My main concern here is really that
socket()
will not return a duplicate or invalid file descriptor when called from different threads. I'm not going to be connecting or writing to the same socket at the same time in my case.

Answer

Is it safe to make concurrent calls to the Linux system, such as calling socket() on several threads at (what might be) the same time?

Yes, they are thread safe. Though I am not sure its just as per the POSIX standard or not.

Specifically, does the kernel guarantee thread-safety for socket(), connect(), and/or send()?

As per THIS link, yes. It says that locks are used internally, which would mean that your send operation would be serialized, but not in any specific order.

Answer to updated part of the question:

My main concern here is really that socket() will not return a duplicate or invalid file descriptor when called from different threads.

No worries here. OS will make sure that the file descriptors for the socket are not duplicated.

Comments