Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, monks!

Thanks to everyone who replied to my previous post Sharing a database handle over multiple processes. Reading Apache::DBI was really helpful.

So, now that I am convinced that DB handles are unusable after a fork() call, I have a follow-up question. :)

How about socket handles? Are they usable after fork()ing ? Or do I need to provide code for creating a new socket and bind()ing local address to it so that the child can listen() and accept() ? If so, will the OS allow me to bind multiple sockets to the same port number?

Thanks again!
  • Comment on Sharing a socket handle over multiple processes

Replies are listed 'Best First'.
Re: Sharing a socket handle over multiple processes
by traveler (Parson) on Jun 29, 2001 at 02:08 UTC
    Sockets stay open across forks. They also seem to stay open across execs. The Camel book says that they do not, but I have found that (at least with 5.005 under Linux) they do. See sockets close-on-exec. You can see from there that I used IO::Socket::INET.

    You should close them in your parent, though as if both have a socket open, you may get results you do not expect.

    You can use fcntl to control whether or not sockets close on exec (if your system implements fcntl and its close-on-exec).