[Haskell-cafe] Bug in runInteractiveProcess?

Donn Cave donn at drizzle.com
Wed Oct 17 01:04:28 EDT 2007


On Oct 16, 2007, at 1:48 PM, John Goerzen wrote:

>
> I have been trying to implement a Haskell-like version of shell
> pipelines using runInteractiveProcess.  I am essentially using
> hGetContents to grab the output from one command, and passing that to
> (forkIO $ hPutStr) to write to the next.  Slow, but this is just an
> experiment.

As an aside, I personally would look to System.Posix.Process for this.
Something like this would deal with the file descriptors in the fork ...

fdfork fn dupfds closefds = do
         pid <- forkProcess $ execio
         return pid
         where
                 dupe (a, b) = do
                         dupTo a b
                         closeFd a
                 execio = do
                         mapM_ dupe dupfds
                         mapM_ closeFd closefds
                         fn

... and then you can put the pipes directly between the processes ...

      -- date | tr '[A-Z]' '[a-z]' | read
      (a0, a1) <- createPipe
      (b0, b1) <- createPipe
      p1 <- fdfork tr [(b0, 0), (a1, 1)] [a0, b1]
      closeFd a1
      p2 <- fdfork date [(b1, 1)] [a0, b0]
      closeFd b1
      readfd a0    -- implementation left to reader
where
      date = executeFile "/bin/date" False [] Nothing
      tr = executeFile "/usr/bin/tr" False ["[A-Z]", "[a-z]"] Nothing

There's probably a nice way to wrap that up, so you're not keeping
track of the file descriptors for all the pipes.

	Donn Cave, donn at drizzle.com


More information about the Haskell-Cafe mailing list