-
Notifications
You must be signed in to change notification settings - Fork 0
/
surviving.hs
43 lines (36 loc) · 990 Bytes
/
surviving.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import Control.Concurrent
import Control.Monad
import System.Environment
import System.Exit (ExitCode (..))
import System.Posix.Signals
import System.Posix.Process
import System.Process
import System.IO
import qualified Data.ByteString.Char8 as S8
say :: String -> IO ()
say s = do
S8.putStr (S8.pack (s ++ "\n"))
hFlush stdout
die :: String -> IO ()
die msg = do
say msg
exitImmediately (ExitFailure 42)
child :: Int -> IO ()
child i = do
_ <- createProcess (proc "surviving" [show i])
return ()
main :: IO ()
main = do
hSetBuffering stdout LineBuffering
_ <- installHandler sigTERM (Catch (die "Got a TERM")) Nothing
args <- getArgs
case args of
[] -> do
mapM_ child [1..4]
say "Parent sleeping"
threadDelay 2000000
say "Parent exiting"
[num] -> forever $ do
say ("Child: " ++ num)
threadDelay 1000000
_ -> error $ "Unknown args: " ++ show args