multithreading with multiprocessing (Was: Concurrent and Posix libraries...)

Simon Marlow
Fri, 21 Dec 2001 10:38:18 -0000

> This example raises a general problem (which, as it turns=20
> out, is relevant
> to my current work).  How can one mix multithreading with
> multiprocessing?  In particular, how can a threaded process=20
> safely create
> another process to run a program?  Put another way, how can the
> combination of `forkProcess` and `executeFile` be done=20
> "atomically enough"
> so that existing threads in the forking process don't "get in=20
> the way".
> I read something on this topic (involving some sort of=20
> pervasive locking
> strategy) recently, but can't recall where.  Anybody remember?

I can't think of a good way to do this at the Haskell level, because
you'd need to halt all the running threads except for the one doing the
fork.  But one hack which will work is to call out to a C function which
does the fork/exec, since foreign calls will be atomic from the point of
view of the Haskell RTS.

However, one caveat which might be relevant in the future is that if the
Haskell program is running in a multi-threaded environment (i.e. OS
threads, not Haskell threads), then the C function must be marked
'unsafe' for it to be treated as atomic by the Haskell RTS.  The OS
thread support in the RTS isn't fully implemented yet, though.