Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

ActiveState, threaded fork and re-entrancy

by Biker (Priest)
on Aug 02, 2002 at 15:04 UTC ( #187116=perlquestion: print w/replies, xml ) Need Help??

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

We are using ActiveState Perl 5.6 in our shop.

I have read in several places, for instance in this thread, that the ActiveState Perl 'simulates' forking by using threads. I have a lot of personal experience in writing multi-threaded code in classical 'C'. (Classical, as opposed to C++.) That was under OS/2 (R.I.P.) so it was a while ago and I may not remember everything absolutely correct but I think the following is true.

  • In a truly fork'ed application, each pid becomes a separate process and is in fact a copy of the parent application. To share information (events or data) between the pids, some kind of IPC must be used.
  • In a threaded application this is not the fact, but global resources are shared between all threads and must be protected, normally using semaphores. Events and non-global data must be shared using pipes, queues or other IPC mechanisms. When compiling a truly threaded application in C, the compiler 'promises' that stack variables in each C function will be unique to the function instance executed. If two or more threads execute the same C function simultaneously they will share the code, but not the stack variables.
I then started to think. And this is when it got hairy...
  • Can a sub my_complicated_calculation(){} be used by more than one simulated forked process, actually implemented as threads in AS Perl?
  • Isn't there a risk that the 'my' variables in that common sub will be overwritten if my_complicated_calculation() is concurrently called from two or more threads?
  • What about global variables?
  • Is this all taken care of by the Perl interpreter, or do we have an issue here migrating a forking Perl application from UNIX to Win-32?

Everything went worng, just as foreseen.

Replies are listed 'Best First'.
(tye)Re: ActiveState, threaded fork and re-entrancy
by tye (Sage) on Aug 02, 2002 at 16:19 UTC

    perrin is correct. But that hasn't stopped the use of (the emulated) fork in Win32 Perl (it is not specific to ActiveState releases) from making scripts behave strangely. In my experience, even trivial scripts that use fork don't behave properly.

    I'd love to hear that Perl v5.8 has drastically improved this situation. But I won't believe reports from the porters as they had previously reported that it mostly worked. (:

            - tye (but my friends call me "Tye")
Re: ActiveState, threaded fork and re-entrancy
by perrin (Chancellor) on Aug 02, 2002 at 15:57 UTC
    Don't worry, the approach used for fork emulation is separate perl interpreters in each thread with nothing shared between them. There are details about copy-on-write, etc. but they are not important for this kind of consideration.
Re: ActiveState, threaded fork and re-entrancy
by Elian (Parson) on Aug 02, 2002 at 17:01 UTC
    Almost everything's separate, but there are still issues with regular expressions (which aren't fixed until 5.8.0) and potentially with C extensions which, regardless of what you see at the perl level, see things as threaded rather than forking. Not all XS plays nice with threads.

    Regexes are your big issue though. The way the match variables are implemented will kill you dead at unpredictable times.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://187116]
Approved by dimmesdale
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2022-01-27 19:40 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (71 votes). Check out past polls.