I’m writing tcp server using libev.
I’m creating socket and forking after listen(), then starting a libev read watcher on listening socket and receiving client connections in watcher callback.
May it be situation, when both: child and parent (or two childs if there is more than one) received event that listening socket became readable and both will try accept() client connection, in this case one of processes will block ?
I written test program and it seems that only one process receives read-ready event, but maybe i’m wrong? Where i can read about system behavior in this case? How kernel does load balancing between processes and decides who will receive event? Is it backend (select, epoll etc…) and/or OS specific?
I’m writing tcp server using libev. I’m creating socket and forking after listen(), then
Share
Is is very much possible that both processes will receive a readiness indication from the socket and that both will as a result call
accept(). That’s one of the reasons why you should always use nonblocking file descriptors with event-based APIs likesocket(),poll(),epoll(), orkqueue()(or libev, which provides an abstraction for one or more of these). If you use nonblocking sockets then one child will get a successful result fromaccept()and all the others will get anEAGAIN, ignore it, and go back to sleep with no harm done.