Closing the listening jack after the plug ()

The general server socket pattern on Linux / UNIX systems should listen on the socket, accept the connection, and then fork()process the connection.

So it seems that after you, accept()and fork()as soon as you enter the child process, you will inherit the handle to the listening file of the parent process. I read that at this point you need to close the listen socket file descriptor from the child process.

My question is why? Is it easy to reduce the number of links to the listening jack? Or is it so that the child process itself will not be used by the OS as a candidate for routing incoming connections? If this is the latter, I am a little confused for two reasons:

(A) What does the OS say that a particular process is a candidate for accepting connections in a specific file descriptor? Is this the fact that the process triggered accept()? Or is it the fact that the process triggered listen()?

(B) If it is the fact that the process has caused listen(), do we not have race conditions here? What to do if this happens:

  • The parent process is listening on socket S.
  • Inbound connection refers to the parent process.
  • The parent process opens the children, the child has a copy of socket S
  • Before the child can call close(S), the second incoming connection goes to the Child Process.
  • The child process never calls accept()(because it should not), so the incoming connection is dropped

? , ?

+4
4

Linux . accept .

- , . - , , accept, , , , .

+6

"", - accept(). , , .

, , , .

, ?

, , accept() . . , , .

+2

. , .

0

The child process will not listen on the socket unless accept () is called, in which case incoming connections can go to any process.

0
source

Source: https://habr.com/ru/post/1605981/


All Articles