How to "talk" with a system process through standard descriptors in Erlang?

Viewed 26

I am writing a tool that is going to call a system process (in this case, Z3 solver) very frequently and I am having troubles setting this up.

What I want to do:

  1. Start a /usr/bin/z3 process with -in flag
  2. Keep it alive as the process waits for the standard input
  3. Send some string to its stdin
  4. Read a line from its stdout
  5. Possibly goto 3
  6. Kill the process politely

What I tried so far:

  • os:cmd – this thing doesn't seem to support stdin
  • Using port, but either it doesn't match my usecase or I don't understand it. My attempt:
main() ->
    process_flag(trap_exit, true),
    Port = open_port({spawn, "z3 -in"}, [{packet, 2}]),
    Port ! {self(), {command, "(check-sat)\n"}},
    receive
        X ->
            X
    end.

...but it leaves me waiting infinitely. However for some reason this works with cat.

At best I would like to wrap an os process with an Erlang process, but I allow some tradeofs and workarounds. What is the most idiomatic way of doing it?

0 Answers
Related