[erlang-questions] spawn_link/2 behaviour
Avinash Dhumane
nistrigunya@REDACTED
Sat Nov 21 12:49:10 CET 2015
Hello,
I have following test program (on Windows):
D:\Balin\Algo_v6\v0.5>type my_test.erl
-module(my_test).
-export([start/0]).
start() ->
register(?MODULE,
spawn(fun() ->
init(),
local_loop()
end)).
init() ->
spawn_link('feed@REDACTED',
fun() ->
register(?MODULE, self()),
remote_loop()
end),
spawn('feed@REDACTED',
fun() ->
timer:sleep(timer:seconds(5)), % wait till the above process is
registered
?MODULE ! hello,
?MODULE ! world,
?MODULE ! stop
end).
remote_loop() ->
receive
X ->
{?MODULE, 'algo@REDACTED'} ! X,
remote_loop()
end.
local_loop() ->
receive
stop ->
io:format("stopped~n");
X ->
io:format("~p~n", [X]),
local_loop()
end.
D:\Balin\Algo_v6\v0.5>
Then, I start 2 nodes, as follows:
D:\Balin\Algo_v6\v0.5>start erl -name algo@REDACTED
D:\Balin\Algo_v6\v0.5>start erl -name feed@REDACTED
On the 'algo@REDACTED' node, I run the program as follows:
Eshell V7.0 (abort with ^G)
(algo@REDACTED)1> rpc:call('feed@REDACTED', erlang, whereis, [my_test]).
undefined
(algo@REDACTED)2> my_test:start().
true
(algo@REDACTED)3> hello
(algo@REDACTED)3> world
(algo@REDACTED)3> stopped
(algo@REDACTED)3> rpc:call('feed@REDACTED', erlang, whereis, [my_test]).
<6755.43.0>
(algo@REDACTED)4>
The messaging works as I expected. However, the process registered as
'my_test' on the node 'feed@REDACTED' does not terminate. I expected it
to terminate since I started it using spawn_link/2 from the calling process
on 'algo@REDACTED' which terminates after receiving the 'stop' message.
It keeps running, as is observed on the node 'feed@REDACTED':
Eshell V7.0 (abort with ^G)
(feed@REDACTED)1> whereis(my_test).
<0.43.0>
(feed@REDACTED)2>
What is wrong with the setup done in the test program above that the linked
process on another node does not terminate?
Please advise.
Thank you.
Avinash
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20151121/8dae9e58/attachment.htm>
More information about the erlang-questions
mailing list