Haskell createProcess and read from Handle
-
21-12-2019 - |
Question
when i use Haskell createProcess do i need to fork before as if i was using exec in c?
From the example's i have seen and for what i have tried i don't think i do but if i read from the output Handle once i get the expected result but if i try to read twice it doesn't read even once.
For example:
beginProcess is the same as createProcess and z3 is a smt solver that reads from stdin and writes to stdout.
execute :: Process -> String -> IO String
execute (Just std_in, Just std_out,_,_) cmd = do
hPutStr std_in cmd
hFlush std_in
hGetLine std_out
main :: IO()
main = do
proc <- beginProcess "z3" ["-smt2","-in"]
execute proc "(set-option :print-success true)" >>= print
execute proc "(set-option :print-success true)" >>= print
If i do one execute i get the expected result, but if i do both nothing happens. Am i reading or writing wrong to the handles ?
Thank you for any help you can give.
Solution
I have a hunch that you are "suffering from buffering".
Note that you are not emitting any newlines to the z3 process. Also, z3 may not be flushing its output after every command.
The best way to interact with a terminal program is through a pseudo tty. Here is an explanation of how such a set up would work: http://rachid.koucha.free.fr/tech_corner/pty_pdip.html