in reply to Mail::SendMail and fork

On UNIX, process creation is done using the kernel C interface fork (some also have vfork). The fork function creates an (almost) exact copy of the parent process, and after the fork the same code runs in both parent and child. Running another program in the child is common, and this is usually done using one of the exec family of functions to replace the current program with another.

In Perl, you can use fork in exactly the same way. Usually it is used to run an asynchronous process, that is when you don't want to wait for the child to complete. In your code you are waiting for the child to complete, which rather defeats the object. You don't have to wait at once in the parent, you can defer the wait until after all your child processes have started, see the doc for waitpid.

A couple of things to watch. First, on most UNIXs, when a child process ends it wants to tell the parent its exit code, so it waits until that has been collected, usually through waitpid or by handling the SIGCHLD signal. So if you never do that then the child cannot die, and you end up with a zombie. Second, when the parent exits it will send each child process a SIGHUP signal, which, by default, will kill the child. So make sure you wait until all the children have finished.

Replies are listed 'Best First'.
Re^2: Mail::SendMail and fork
by hiradhu (Acolyte) on Apr 01, 2009 at 08:28 UTC
    Thanks for the reply. i missed to mention that I'm running perl in Windows. Moreover will this fork handle more than 10k processes at once?!!!

      fork is a bad idea on Windows, and it's a bad idea on any system to try to fork 10000 processes at once. Take a look at Parallel::ForkManager or a simple thread-based solution that runs (say) 5 or 10 worker threads and feeds them from a queue.

        Thanks! I suppose ForkManager is also uses Fork function, not sure. Basically I'm testing Mail scalability, it would be good if I can stress the server with 50000 mails per day sending mails in parallel...