[Haskell-cafe] interaction between OS processes

Andrea Rossato mailing_list at istitutocolli.org
Thu Aug 30 06:14:28 EDT 2007


there's something I don't get about interaction among OS processes and
Haskell handles/channels.

Suppose I have a very small program that takes a line and prints it
till you write "quit":

main = do
  s <- getLine
  case s of
    "quit" -> putStrLn "quitting" >> return ()
    _ -> loop s
    loop s = do
         putStrLn s

This is a small interactive process I would like to talk to from
another Haskell program, like the following one which, indeed, is just
a wrapper around the first.

Now, if I write a single line with "quit", I get "quitting" back,
otherwise the program doesn't work.

I think I need some direction in order to understand how handles
work. The same with channels, I'm afraid. Could you please point me
in the right direction?

Thanks for your kind attention.

The not working code:

import Control.Concurrent
import System.Process
import System.IO

main = do
  c <- runInteractiveCommand "./main2"
  loop c

loop c@(i,o,e,p) = do
  s <- getLine
  hPutStrLn i s
  hFlush i -- now "i" is closed, right?
  s' <- hGetLine o
  putStrLn s'
  loop c 

More information about the Haskell-Cafe mailing list