Concurrent question

Volker Stolz stolz@hyperion.informatik.rwth-aachen.de
Wed, 7 Nov 2001 12:59:17 +0100


In local.glasgow-haskell-users, you wrote:
> Suppose I have a program with two (ghc-)threads,
> one that does execute some  `system' commands, 
> and another one that accepts socket connections.
> While the system thread waits (for the system call to finish),
> will the other thread be awake (and accept new connections)?

As Simon already stated: no.
If you decide to use fork() in any way, be sure to implement
some kind of locking as *all* threads (including the thread
for the socket!) will be clowned, e.g.:

giant <- newMVar ()
...
takeMVar giant
pid <- fork
case pid of
  Nothing -> do -- Child
    systen(...)
  Just p -> do -- Parent
    putMVar giant ()

and placing locks on 'giant' in the essential parts in every other
threads, e.g. before hGetLine. Ugly, but necessary for the worst
case:

threadWaitRead fd
takeMVar giant
hGetLine h
putMVar giant ()

However, waiting on the system command to finish requires that you
use a sigCHLD signal handler which invokes get[Any]ProcessStatus
as you can't invoke waitpid in the parent.
-- 
Volker Stolz * stolz@i2.informatik.rwth-aachen.de * PGP + S/MIME