[erlang-questions] one question about BIF link

mozhenghua19811109@REDACTED
Thu Oct 18 09:52:40 CEST 2012


hi,Konstantin Tcepliaev


thanks a lot for repling me this letter ,i am fresh man here, yours reply resolve my confuse about the difference   between pid and the register pid name 

baisui


At 2012-10-17 15:34:40,"Konstantin Tcepliaev" <f355@REDACTED> wrote:
>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, "莫" <mozhenghua19811109@REDACTED>:
>> 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:
>>
>>    [baisui@REDACTED
>> 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,
>>
>> (bbbb@REDACTED)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 'bbbb@REDACTED' with exit value:
>> {badarg,[{erlang,link,[{remote,'remote@REDACTED
>>
>> 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,'remote@REDACTED'} , 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
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20121018/1e1c9a33/attachment.htm>


More information about the erlang-questions mailing list