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

John Lato jwlato at gmail.com
Thu Jan 12 12:49:25 CET 2012

> From: Brandon Allbery <allbery.b at gmail.com>
> On Wed, Jan 11, 2012 at 16:26, Andr? Scholz <andre.scholz at uni-bremen.de>wrote:
>> (on unix) creating a process A which spawns itself a subprocess B and
>> terminating process A before it finishes leaves process B as a process on
>> its
>> own. This is because "terminateProcess" sends the sigterm signal to the
>> process only and not to its process group.
> terminateProcess passes on the semantics of kill(2); on SVID-compliant (and
> I think POSIX.1-compliant) systems, the negative of the process group
> leader's process ID is used to signal the process group.  Note that you may
> need to arrange for your initial child process to invoke setpgrp() to
> insure that the parent program is not part of the process group.

System.Process.terminateProcess takes a ProcessHandle as input, which
is an opaque type which essentially can only be created via
System.Process.createProcess or similar; it's not possible to pass
arbitrary pid's to it.

However, on Posix systems it is currently possible (ghc-7.2.1) to
import System.Process.Internals, which would allow you to either
create a new ProcessHandle from an arbitrary pid or retrieve the pid
from a ProcessHandle.  My answer on SO now shows how to do so.  Of
course this approach won't be portable, and it may change depending on
your compiler version.

(link to SO: http://stackoverflow.com/questions/8820903/haskell-how-to-timeout-a-function-that-runs-an-external-command/

John L.

More information about the Haskell-Cafe mailing list