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

Re^2: parallelisation (how to wait on N threads)

by erroneousBollock (Curate)
on Nov 20, 2007 at 13:38 UTC ( #651926=note: print w/replies, xml ) Need Help??

in reply to Re: parallelisation (how to wait on N threads)
in thread Parallelization of heterogenous (runs itself Fortran executables) code

Could you please elaborate on the implications of mixing fork() with Perl threads? ... I'd like to know what the pitfalls are.
The implications stem from how Perl threads are implemented. clones all non-shared data in the 'parent' thread to the 'child' thread. If you have a lot of data (or code) loaded in the spawning thread, that data is cloned (copied) into the spawned thread.

Users of Unix fork() have come to expect Copy-on-Write semantics; only copying data when it is written to. While that expectation is not necessarily warranted for perl's fork() (perl modifies bit flags on its variables routinely), it certainly doesn't work that way for threads... if you don't understand the behaviour, you'll end up using lots more memory than you intended.

What I don't know (I'm sure BrowserUK does) is whether all the running threads of the (forking) process are then present in the newly spawned (child) process; if they are, it could get messy ;) Tested: only active thread is forked().

If it's best to avoid mixing fork() (from the system() calls) and Perl threads, what would you suggest for Jochen's situation?
I'd recommend he use Parallel::ForkManager to manage worker processes... that's exactly what it was written for.

Also, remember that fork() is emulated using Threads on Win32, so that wouldn't necessarily be the best way to go on that platform.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2021-10-27 12:01 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (92 votes). Check out past polls.