Plug with listening socket

I would like to make sure that I am trying to use accept()the socket correctly .

I know that on Linux it is safe for listen()a socket, fork()N children, and then recv()packets in all of them without any synchronization from the user (packets receive more or less load, balanced between children). But this is UDP.

Whether this is stored as a property for the TCP and listen(), fork(), accept()? Can I just assume that it is okay to accept on a shared socket created by the parent, even when other children are doing the same? Are POSIX, BSD sockets, or any other standard defining it somewhere?

+3
source share
1 answer

If fork () and then accept () in your children, only one child process will call accept () to connect, and then process it. This is pre-forking, and compounds will not be distributed to children.

You can make a standard one child for each connection scheme by changing the order, accept and unlock. However, both of these methods are for efficiency, balancing, etc., and not for sharing a particular compound.

TCP is different from UDP. It would be impractical to do this in TCP, since you will almost certainly end the mess. This received message can be distributed in one or several packets, and it would be a pain to coordinate several processes than if one child were processing the connection.

+2

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


All Articles