File descriptor exchange between parents and forked children

I am starting to program on the net.

I follow stevens and implement a UDP server with reliability.

I have a question.

when a child forks, what happens to the parent socket fd in the child ...? I read that they are inherited, which means that we have two copies of the fd socket in the parent and child? we need to close the inherited parent socket fd in child (Stevens code doesn't do this)

How to close parent fds in child (using close () ??), but not the socket on which the client request was received (should I keep this open)?

If I'm naive, excuse me.

Thanks for the help!

+4
source share
1 answer

File descriptors are indeed β€œinherited” during forking, but only with respect to which socket they are connected to, and closing the file descriptor only closes the socket if it is last connected to this socket (or the file if we are dealing with files).

What you usually do is install a socket and then a plug. In the parent process (the one where fork returned a non-zero value), you can continue and close the file descriptor with close(fd) , if you do not, you will end up with file descriptors in the parent process. This is for stream sockets (e.g. TCP), where you have one server socket that listens for connections, and one socket in the established connection. However, you are using UDP, so there really is only one socket, and if you intend to continue using it in the parent process, you will need to figure out how to split it between the parent and child process. Both may continue to use it, but it will be almost random, which reads what and in what order the material is sent. In this case, you usually have some kind of multiplexing process that receives packets and forwards them to the appropriate child element (to some other mechanism, such as pipes or other sockets) based on some message content (in TCP, this is the ip / port source and destination ip / port tuple).

As Matt noted, using shutdown will actually make the socket unusable (usually unacceptable, but you can specify this) for all participants. In TCP, this can cause a FIN packet to be sent, effectively initiating a connection drop, but you can still receive data until the remote end confirms FIN.

+1
source

All Articles