Skip to content

Instantly share code, notes, and snippets.

@etrepum
Created October 13, 2013 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etrepum/6959892 to your computer and use it in GitHub Desktop.
Save etrepum/6959892 to your computer and use it in GitHub Desktop.
module Main where
import Control.Concurrent
import System.Posix.Signals
signalsDebugFile :: String
signalsDebugFile = "signals.out"
handleUSR1 :: IO ()
handleUSR1 =
appendFile signalsDebugFile "Got USR1\n"
handleUSR2 :: IO ()
handleUSR2 =
appendFile signalsDebugFile "Got USR2\n"
handleINT :: IO ()
handleINT =
appendFile signalsDebugFile "Got INT\n"
handleTERM :: MVar () -> IO ()
handleTERM mvar =
appendFile signalsDebugFile "Got TERM\n" >>
putMVar mvar ()
main :: IO ()
main = do
mvar <- newEmptyMVar
forkIO (daemon mvar)
readMVar mvar
daemon :: MVar () -> IO ()
daemon mvar =
do
installHandler userDefinedSignal1 ( Catch handleUSR1 ) Nothing
installHandler userDefinedSignal2 ( Catch handleUSR2 ) Nothing
installHandler keyboardSignal ( Catch handleINT ) Nothing
installHandler softwareTermination ( Catch (handleTERM mvar) ) Nothing
unblockSignals fullSignalSet
-- block forever without spinning the CPU
newEmptyMVar >>= readMVar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment