develooper Front page | perl.perl5.porters | Postings from February 2003

fork and _exit: when do we get an improved perl doc?

Thread Next
Lippmann Jens,E2
February 26, 2003 11:00
fork and _exit: when do we get an improved perl doc?
Message ID:
Dear Perl gurus!

I'm into Perl for more than 8 years and I cannot overemphasize that I appreciated every single minute programming with it.

Now, I actually spent quite a large amount of time to find a nasty bug that crashes Perl on Windows NT when I call fork and exec.
The problem is not related to any specific Perl but rather sticks to Perl ports that create threads instead of processes.
As soon as the exec'd task terminates there is a considerable high chance (obviously a race condition) to crash perl.
When I replace exec() with system() and exit() the problem pertains, but not if I use system() and POSIX::_exit().

I'm not that deep into Win32 programming to say what actually happens, despite the fact that it crashes the perl debugger, too.
But from the amount of developer questions related to this topic I'd say that I am way off being the only one and that the Perl docs do not provide any single clue addressing this issue.
My pledge is to build up a FAQ topic (perldoc -q) for fork that helps out of this remedy, and to extent the fork/exec sections in the perlfuncs doc module, and to overhaul the notorious 'if ($pid = fork()) { ... } else { ... exit; }' examples that are used throughout the perlipc doc.

It is generally a bad idea to use exit() after fork(), unless you want to create a daemon process (see "1.1.3 Why use _exit rather than exit in the child branch of a fork?").
It is also a bad idea to use exec() after fork() on a Win32 port for the simple reason that exec() itself performs cleanup action on process termination similar to exit().

It is no good way to leave the user undocumented and let the developers run into the same obstacles again with every new release.
Meanwhile there are half a dozen Win32 module extensions on CPAN, but none seems to come with support throughout all Win32 platforms or availability for any perl distribution.
For the sake of transparency and portability, why can't Perl itself solve the exit handling to smooth out thread and non-thread forks?
I recommend to either
 - keep the transparency of fork() but then make exit() transparent as well (i.e. remember the 'thread' state and act accordingly), or
 - do not support fork() on Win32 but vfork() and provide a function like vexit()

I further recommend to generally incorporate POSIX::_exit() into the Perl core and to add a hint about the batch command 'start' which can be used to conveniently spawn a task under Win32.

kind regards
Jens Lippmann

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About