[erlang-questions] one question about BIF link

Konstantin Tcepliaev <>
Wed Oct 17 09:34:40 CEST 2012


Hello.

You cannot use link/1 with registered process name, either local or remote, as it accepts only PIDs and ports.
It is also very likely that you don't need your remote process to exit when your local process fails.
So use erlang:monitor/2 instead.

Alternatively, you may send the local PID to remote process somehow and establish a link there, or use rpc:call/4 and erlang:whereis/1, if you need bi-directional link.

Cheers,
-- 
Konstantin

17.10.2012, 06:03, "莫" <>:
> Hi everybody:
>
>   I have a question for the erlang BIF
> link.
>
> When I use the method link to an local
> process ,the result will be what I wish to see.
>
> ▼▼▼
>
> Like this
>
>   start()->
>
>      Pid1=spawn(fun()->
> sleep(5000),exit("auto exit") end),
>
> Lib_misc:on_exit(Pid1
> , fun(Msg)-> io:format("hello ~p~n",[Msg]) end).
>
> sleep(T) ->
>
> receive
>
> after T ->
>
> true
>
> end.
>
> ▲▲▲
>
> After execute the method start for 5 sec ,
> the main process will receive exit sign from the child process.
>
> But when I create an new erlang node on an
> another server, like below:
>
>    [
> code]$ erl -name remote -setcookie abc
>
> And execute the method linktest:
> startreceivemsg() wait to receive message from network
>
> ▼▼▼
>
> -module(linktest).
>
> -export([startreceivemsg/0]).
>
> ready2receivemsg() ->
>
>   receive
>
>    Msg ->
>
>     io:format("receive
> ~p~n",[Msg]),
>
>     ready2receivemsg()
>
>   end.
>
> startreceivemsg() ->
>
> register(remote, spawn(fun
> ready2receivemsg/0)).
>
> ▲▲▲
>
> Then i launch an
> another node on my pc:
>
>   erl -name
> bbbb -setcookie abc
>
> and execute the BIF
> link method link to remote process what I have just created,
>
> ()11>
> linktest:link_remote().
>
> and then throw an
> error:
>
> ▼▼▼
>
> <0.57.0>
>
> =ERROR REPORT====
> 15-Oct-2012::17:16:22 ===
>
> Error in process
> <0.57.0> on node '' with exit value:
> {badarg,[{erlang,link,[{remote,'
>
> qa.cm6'}]},{lib_misc,'-on_exit/2-fun-0-',2}]}
>
> ▲▲▲
>
> My quest is how can I remote to an remote
> process located in anothre server? Any body can help me ,thanks.
>
> ▼▼▼
>
> -module(linktest).
>
> -export([start/0, link_remote/0]).
>
> start()->
>
> Pid1=spawn(fun()->
> sleep(5000),exit("auto exit") end),
>
> lib_misc:on_exit(Pid1 , fun(Msg)->
> io:format("hello ~p~n",[Msg]) end).
>
> link_remote() ->
>
> lib_misc:on_exit(
> {remote,''} , fun(Msg)-> io:format("hello
> ~p~n",[Msg]) end).
>
> ▲▲▲
>
> The method lib_misc:on_exit() was
> define as below:
>
> ▼▼▼
>
> on_exit(Pid,
> Fun) ->
>
> spawn(fun() ->
>
>   process_flag(trap_exit, true),
>
> link(Pid),
>
>   receive
>
>       {'EXIT', Pid, Why} ->
>
>   Fun(Why)   %% <label id="code.onexit4"/>
>
>   end
>
>       end).
>
> ▲▲▲
>
> BaiSui from China taobao corp
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list