[Haskell] Reading from two fifos

Setzer, Sebastian (ext) sebastian.setzer.ext at siemens.com
Tue Dec 12 13:35:02 EST 2006

Why doesn't this program start reading from one fifo until the second
gets opened (with cat), too? (It doesn't matter which fifo is opened
Why doesn't it print "forked" before "fifo1" is opened?
After both fifos are open, it works as expected.

I have ghc 6.6-3 on debian-linux (testing)

Sebastian Setzer

-- Compile: ghc twofifo.hs -o twofifo -threaded -package unix-1.0
-- Usage:
-- mkfifo fifo1
-- mkfifo fifo1
-- ./twofifo
-- in two separate xterms:
-- cat > fifo1
-- ...
-- cat > fifo2
-- ...

import System.IO
import System.Posix.IO
import System.Posix.Types
import Control.Concurrent

main :: IO()
main = do
	f1Thread <- forkOS (openAndShowFile "fifo1")
	print "forked"
	hFlush stdout
	openAndShowFile "fifo2"

openAndShowFile :: String -> IO()
openAndShowFile name = do
	fd <- openFd name ReadOnly Nothing defaultFileFlags
	showFile name fd

showFile :: String -> Fd -> IO()
showFile name fd = do
	threadWaitRead fd
	(s, count) <- fdRead fd 1024
	print (name ++ ": " ++ s)
	hFlush stdout
	showFile name fd

More information about the Haskell mailing list