[Haskell-cafe] Getting PID of a child process

Gwern Branwen gwern0 at gmail.com
Fri Oct 19 00:38:28 CEST 2012

On Thu, Oct 18, 2012 at 5:03 PM, Jason Dusek <jason.dusek at gmail.com> wrote:
> For diagnostic purposes, I'd like to print the PID of the
> process attached to this handle -- how best to do that?

In Mueval when I wanted the PID (so I could later send sigkills), I did this:

          hdl <- runProcess "mueval-core" args Nothing Nothing Nothing
Nothing Nothing
          _ <- forkIO $ do
                     threadDelay (7 * 700000)
                     status <- getProcessExitCode hdl
                     case status of
                         Nothing -> do terminateProcess hdl
                                       _ <- withProcessHandle hdl (\x
-> case x of

OpenHandle pid -> signalProcess 9 pid >> return (undefined, undefined)

_ -> return (undefined,undefined))
                                       exitWith (ExitFailure 1)
                         Just a -> exitWith a
          stat <- waitForProcess hdl
          exitWith stat

The key is the poorly documented withProcessHandle ::
System.Process.Internals.ProcessHandle -> (ProcessHandle__ -> IO
(ProcessHandle__, a)) -> IO a

The implementation:

    data ProcessHandle__ = OpenHandle PHANDLE | ClosedHandle ExitCode
    type PHANDLE = CPid

Well, my code seems to work, anyway...


More information about the Haskell-Cafe mailing list