[erlang-questions] one question about BIF link

<>
Wed Oct 17 04:02:41 CEST 2012


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



More information about the erlang-questions mailing list