[Haskell-cafe] forkProcess, forkIO, and multithreaded runtime

Kazu Yamamoto ( 山本和彦 ) kazu at iij.ad.jp
Tue Oct 16 03:04:05 CEST 2012

> My understanding is that System.Process avoids these problems by doing
> all the setup around forking a command in C code. I've banished
> forkProcess from my code base entirely, except for a double fork I need
> to daemonize, and I don't even trust that call. :/

I think you are right. forkProcess is necessary to deamonize a process
but I don't know other usage.

daemonize :: IO () -> IO ()
daemonize program = ensureDetachTerminalCanWork $ do
    ensureNeverAttachTerminal $ do
        changeWorkingDirectory "/"
        void $ setFileCreationMask 0
        mapM_ closeFd [stdInput, stdOutput, stdError]
    ensureDetachTerminalCanWork p = do
        void $ forkProcess p
    ensureNeverAttachTerminal p = do
        void $ forkProcess p
    detachTerminal = void createSession


More information about the Haskell-Cafe mailing list