module Main where import Concurrent import System(getArgs) import Random(randomRIO) philo :: [MVar ()] -> Int-> IO () philo chopsticks i = let num_phil = length (chopsticks) in do putStrLn ("Phil #"++(show i)++" thinks...") randomRIO (500,2000) >>= threadDelay takeMVar (chopsticks !! i) takeMVar (chopsticks !! ((i+1) `mod` num_phil)) putStrLn ("Phil #"++(show i)++" eats...") randomRIO (500,2000) >>= threadDelay putMVar (chopsticks !! i) () putMVar (chopsticks !! ((i+1) `mod` num_phil)) () philo chopsticks i main :: IO () main = do num <- getArgs >>= \a-> return (read (head a)) chopsticks <- sequence (take num (repeat (newMVar ()))) mapM_ (forkIO . (philo chopsticks)) [0.. num-1] block block :: IO () block = newEmptyMVar >>= takeMVar