5

Given two Erlang nodes, "foo@host" and "bar@host", the following produces a print-out on "foo":

(foo@host) rpc:call('bar@host', io, format, ["~p", [test]]).

While the following prints out on "bar":

(foo@host) rpc:call('bar@host', erlang, display, [test]).

Even if erlang:display/1 is supposed to be used for debug only, both functions are supposed to send stuff to the standard output. Each process should inherit the group leader from its parent, so I would expect that the two functions would behave in a consistent way.

Is there any rationale for the above behaviour?

1 Answer 1

12

The reason for this difference in behaviour is where and by whom the output is done:

  • erlang:display/1 is a BIF and is handled directly by the BEAM which writes it straight out to its standard output without going anywhere near Erlang's io-system. So doing this on bar results in it printed to bar's standard output.

  • io:format/1/2 is handled by the Erlang io-system. As no IoDevice has been given it sends an io-request to its group leader. The rpc:call/4 is implemented in such away that the remotely spawned process inherits the group leader of the process doing the RPC call. So the output goes to the standard output of the calling process. So doing an RPC call on foo to the node bar results in the output going to foo's standard output.

Hence the difference. It is interesting to note that no special handling of this is needed in the Erlang io-system, once the group leader has been set it all works transparently.

4
  • Brilliant explanation. Thank you. Sep 19, 2012 at 12:44
  • Yes, this is also why erlang:display/1 is great for demonstrating how Erlang I/O works. Your exact example using rpc on a remote node with display or format is something I've used in training sessions. :-)
    – RichardC
    Sep 20, 2012 at 21:40
  • @RichardC You mean the difference between writing straight out to stdout and using the Erlang io?
    – rvirding
    Sep 20, 2012 at 22:43
  • Yep. The rpc doing display/1 prints its output on the remote node's terminal, while the one using io prints on the caller's terminal.
    – RichardC
    Sep 21, 2012 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.