[erlang-questions] What's wrong with this code?

Edward Wang <>
Sun Feb 20 08:01:53 CET 2011


But I didn't see where you call (node2 ! get_children).

-Ed

On Sun, Feb 20, 2011 at 2:33 PM, Marcel Meyer <>wrote:

> Dear Community,
> I am having a hard time debugging a certain behaviour in the following
> code.
> The general premise of this code is that a bunch of these 'nodes' get
> spawned, and then
> they have to figure out how they are related. (So here in the test() func,
> node 1 is the parent of 2, and 2 the parent of 3.)
> When you start a node, you give it an Id and tell it who it's Parent is.
> Using io:formats, I confirm that everyone finds a parent (except for the
> root).
> But when I ask node 2 for his children (node2 ! get_children), he has none,
> and this is incorrect.
> The current io:format of the state acknowledges that of a moment there,
> node2 did have children.
>
> Thank you in advance,
> Marcel
>
>
> -module(node).
> -record(state, {id, pid, parentId, ppid, children=[]}).
> -compile([export_all]).
>
> test() ->
>    Nodes = [{1, 0}, {2, 1}, {3, 2}],
>    Pids = [spawn(?MODULE, start, [Id, ParentId]) || {Id, ParentId} <-
> Nodes],
>    [Pid ! {connect, Pids} || Pid <- Pids],
>    started.
>
> start(Id, ParentId) ->
>    io:format("I am ~p (~p) and my parent Id is ~p~n", [Id, self(),
> ParentId]),
>    loop(#state{id=Id, pid=self(), parentId=ParentId}).
>
> loop(#state{id=Id, parentId=ParentId}=S) ->
>    receive
> {connect, Pids} ->
>    find_parent(S, Pids),
>    loop(S);
>  {parent, Pid} ->
>    io:format("---> ~p found parent @ ~p~n", [Id, Pid]),
>    loop(#state{id=Id, parentId=ParentId, ppid=Pid});
>  {id, TestId, ChildId, Pid1}  ->
>
>    NewState = case TestId == Id of
>   true ->
>       Pid1 ! {parent, self()},
>       Children = S#state.children,
>       S#state{children=[{ChildId, Pid1} | Children]};
>   _ ->
>       S
>       end,
>    io:format("Query: am I ~p's parent? ~p. New state:~p~n", [ChildId,
> TestId==Id, NewState]),
>    loop(NewState);
>
> get_children ->
>    io:format("~p~n", [S]),
>    loop(S);
>  _ ->
>    loop(S)
>    end.
>
> find_parent(#state{parentId=ParentId, id=Id}, Pids) ->
>    [Pid ! {id, ParentId, Id, self()} || Pid <- Pids].
>


More information about the erlang-questions mailing list