[Haskell-cafe] How to terminate the process group of aprocesscreated with createProcess?

Donn Cave donn at avvanta.com
Thu Jan 12 18:18:12 CET 2012

Quoth Brandon Allbery <allbery.b at gmail.com>,
> On Thu, Jan 12, 2012 at 06:56, André Scholz <andre.scholz at uni-bremen.de>wrote:
>>            gid <- createProcessGroup pid
>> but i get the error message:
>> "createProcessGroup: permission denied"

> the documentation for terminateProcess is incorrect and should not
> mention process groups

Right - and terminateProcess should _not_ be fixed to work like the
documentation says!  I think it's just a point of confusion over how the
shell works - in fact, process groups are normally created by the shell,
but only when it's serving as the top level interactive shell.

> In any case, I would not mix System.Process and System.Posix.Process.  I'd
> probably use the latter, but that's because it's closer to what I'm used to
> as a Unix/SVID/POSIX programmer.

Same here, and in any case I don't expect there's any way to do this
with System.Process, i.e., portably on non-UNIX platforms.

In the System.Posix.Process variant, you get to use "fork" directly:

   spawn :: String -> [String] -> IO ProcessID
   spawn exe cmd = forkProcess $ executeFile exe False cmd Nothing

To set the process group,

   spawnpg exe cmd = forkProcess $ do
        getProcessID >>= joinProcessGroup  -- equivalent to setpgrp()
        executeFile exe False cmd Nothing

... to terminate the process group,

   signalProcessGroup sigTERM pid

... and wait for the spawned process

   getProcessStatus True False pid

When you're running Haskell in the child fork like this, you could
run afoul of runtime thread support, and depending on your situation
it might make sense to move the fork/setpgrp/exec out into a C function
to avoid that.


More information about the Haskell-Cafe mailing list