IO concurrency
Simon Marlow
simonmar@microsoft.com
Mon, 15 Oct 2001 16:47:25 +0100
> Playing with GHC, I met some oddity.
> Consider the example:
>=20
> > import IO
> > import Concurrent
> > import Posix
> > import PosixIO
> >=20
> > main =3D do
> > (fdIn, fdIn_send) <- createPipe
> > hIn_send <- fdToHandle fdIn_send
> > hIn <- fdToHandle fdIn
> > -- The "waiter" is a simple tk/tcl script which
> > -- prints "click" anytime I click a button.
> > runProcess "./waiter" [] Nothing Nothing
> > Nothing (Just hIn_send) Nothing
> > -- hSetBuffering hIn LineBuffering {-2-}
> > -- hSetBuffering stdin LineBuffering
> > forkIO(interact stdin)
> > -- forkIO(interact hIn) {-1-}
> > main_cycle
> > where
> > interact hIn =3D do
> > line <- hGetLine hIn
> > numIn <- handleToInt {-made from handleToFd & fdToInt-}
> > -- threadWaitRead numIn {-3-}
> > putStrLn line
> > interact hIn
> > main_cycle =3D do
> > putStrLn "tick"
> > threadDelay 1000000
> > main_cycle
>=20
>=20
> The example works OK. It prints "tick" once a second and dups my input
> as I type it. But, when I uncomment the line {-1-} to handle=20
> input from
> child as well, everything is hanged on this read operation.=20
> Uncommenting
> the {-3-} line helps, but, first, I'm not sure it is a right way and,
> second, the matter is still unclear - what is the reason of such
> difference?
>=20
> Uncommenting the {-2-} lines doesn't help. I thing the reason is in
> some inner properties of the handlers, but have no idea in which one.
I tried this here on Linux/x86 with 5.02 and it seems to work fine.
Instead of the tcl/tk script you mentioned I used a FIFO in /tmp/fifo
and made the runProcess just call "cat /tmp/fifo".
Which GHC version and platform is this on?
Cheers,
Simon