behavior of io:format() in spawn'ed process

HP Wei <>
Wed Jun 28 21:40:20 CEST 2006

I am writing a monitoring module whose partial code is appended
at the end of this email.
I would like to ask for help in understanding the behavior of
io:format() in a spawn'ed process.

Suppose there are three nodes.  What the code does is illustrated
in the following.

          node_a             node_b            node_c
stage(1)  migrate()------->  watch_job()
          through spawn_link()
stage(2)                     send_back done
stage(3)                     migrate()-------->watch_job()
                             through spawn_link

The code is compiled and loaded on all three nodes' erl-sehll
(in interactive mode).
The first migrate() call is invoked in node_a's shell window.

Here comes the questions:
   How come the output of io:format() in the first line of the
   function watch_job()
   always gets displayed in the shell-window of node_a ??

   In the above diagram, both watch_job() are not run on
   node_a.  I thought there would be no output on node_a.

   Somehow, the IO is tied to node_a's erl-shell.
   [ If I break the tie (by killing the erl-shell on node_a,
     the code won't work anymore...]

   Am I doing something wrong in the code ?



-export([watch_job/2, migrate/1]).

watch_job(Nodes, Calling_pid) ->
    io:format("~p -- calling from ~p~n", [node(), Calling_pid]),
    do_something(), % including re-starting any dead erl.
    % ask the next node to take over
    Calling_pid ! {done, {node(), self()}},
    spawn(?MODULE, migrate, [Nodes]).


migrate(Nodes) ->
    process_flag(trap_exit, true),
    Node = next_node(node(), Nodes), % get the next node other than me
    Pid = spawn_link(Node, ?MODULE, watch_job, [Nodes, self()]),
    loop(Pid, Nodes).

loop(Pid, Nodes) ->
	{'EXIT', Pid, noconnection} ->
	    % start another migrater
	    spawn(?MODULE, migrate, [Nodes]);
	X ->


More information about the erlang-questions mailing list