[erlang-questions] What's wrong with this code?
Marcel Meyer
marcel.meyer@REDACTED
Sun Feb 20 08:15:49 CET 2011
Oh sorry, that I did in the shell, to test.
Here is the output:
100> node1:test().
I am 1 (<0.329.0>) and my parent Id is 0
I am 2 (<0.330.0>) and my parent Id is 1
I am 3 (<0.331.0>) and my parent Id is 2
started
Query: am I 1's parent? false. New state:{state,1,<0.329.0>,0,undefined,[]}
Query: am I 1's parent? false. New state:{state,2,<0.330.0>,1,undefined,[]}
Query: am I 1's parent? false. New state:{state,3,<0.331.0>,2,undefined,[]}
Query: am I 2's parent? true. New state:{state,1,<0.329.0>,0,undefined,
[{2,<0.330.0>}]}
Query: am I 2's parent? false. New state:{state,2,<0.330.0>,1,undefined,[]}
Query: am I 2's parent? false. New state:{state,3,<0.331.0>,2,undefined,[]}
Query: am I 3's parent? false. New state:{state,3,<0.331.0>,2,undefined,[]}
Query: am I 3's parent? false. New state:{state,1,<0.329.0>,0,undefined,
[{2,<0.330.0>}]}
Query: am I 3's parent? true. New state:{state,2,<0.330.0>,1,undefined,
[{3,<0.331.0>}]} % <------------
this shows that the record's children value got updated with {3, <Pid of 3>}
---> 3 found parent @ <0.330.0>
---> 2 found parent @ <0.329.0>
101> pid(0,329,0) ! get_children.
{state,1,<0.329.0>,0,undefined,[{2,<0.330.0>}]} %<-------- this is
correct: Node 2 found his parent, Node 1
get_children
102> pid(0,330,0) ! get_children.
{state,2,undefined,1,<0.329.0>,[]} %<------- this is incorrect: Node 2
does not know of Node 3
get_children
On Sun, Feb 20, 2011 at 2:01 AM, Edward Wang <yujiangw@REDACTED> wrote:
> But I didn't see where you call (node2 ! get_children).
>
> -Ed
>
>
> On Sun, Feb 20, 2011 at 2:33 PM, Marcel Meyer <marcel.meyer@REDACTED>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