Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote nodes, group leaders and printouts

Tags:

erlang

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?

like image 676
Roberto Aloi Avatar asked Sep 19 '12 09:09

Roberto Aloi


1 Answers

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.

like image 150
rvirding Avatar answered Sep 28 '22 03:09

rvirding