Haskell createProcess和Handle读取

时间:2014-01-10 00:26:09

标签: haskell handle createprocess smt

当我使用Haskell createProcess时,我需要先分叉,好像我在c中使用exec吗?

从我已经看过的例子中我已经看过并且我没有想到我会这样做但是如果我从输出句柄中读取一旦我得到了预期的结果但是如果我尝试读两次它甚至没有读过一次

例如:

beginProcess与createProcess相同,z3是从stdin读取并写入stdout的smt解算器。

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 

如果我执行一次,我会得到预期的结果,但如果我这样做,则两者都没有发生。 我在读句子上写错了吗?

感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:5)

我预感到你是"suffering from buffering"

请注意,您没有向z3进程发出任何换行符。此外,z3可能不会在每个命令后刷新其输出。

与终端程序交互的最佳方式是通过伪tty。以下是对此类设置如何运作的解释:http://rachid.koucha.free.fr/tech_corner/pty_pdip.html