Tuesday, July 3, 2012

erlang registering and monitoring global processes

Please note, the shell command numbers may not be in order as I put this together in a hurry.

% start up an erlang node
 erl -sname a -setcookie qwerty




%% create a new process which echos back anything it receives
(a@philipclarkeirl-desktop)1> Pid = spawn(fun() -> receive {Sender, Msg} -> Sender ! Msg end end).
<0.40.0>



%% test it out
(a@philipclarkeirl-desktop)2> Pid ! {self(), "Hello"}.
{<0.38.0>,"Hello"}


%% globally register the process
 (a@philipclarkeirl-desktop)3> global:register_name(myprocess, Pid).
yes


%% check it is registered
(a@philipclarkeirl-desktop)4> global:registered_names().
[myprocess]



(a@philipclarkeirl-desktop)6> global:whereis_name(myprocess) =:= Pid.
true
 

%% send to myprocess a message
(a@philipclarkeirl-desktop)7> global:send(myprocess, {self(), "Hello from shell"}).
<0.40.0>


%% note the return value is the pid found for myprocess
 

%% check in the shell that we received a response
(a@philipclarkeirl-desktop)8> flush().
Shell got "Hello from shell"
ok



%% start up another node
erl -sname b -setcookie qwerty

%% check that the process is globally registered
(b@philipclarkeirl-desktop)3> global:registered_names().
[myprocess]


%% monitor the process started in node a
(b@philipclarkeirl-desktop)11> MRef = erlang:monitor(process, global:whereis_name(myprocess)).
#Ref<0.0.0.83>



%% in node a, kill the process
(a@philipclarkeirl-desktop)33> exit(Pid, kill).
true


%% in node b
(b@philipclarkeirl-desktop)12> flush().
Shell got {'DOWN',#Ref<0.0.0.83>,process,<5799.85.0>,killed}
ok



1 comment:

Unknown said...

Do you understand there's a 12 word sentence you can tell your partner... that will trigger intense emotions of love and impulsive attractiveness for you buried inside his heart?

Because hidden in these 12 words is a "secret signal" that fuels a man's impulse to love, look after and protect you with all his heart...

12 Words Will Trigger A Man's Desire Impulse

This impulse is so hardwired into a man's brain that it will drive him to try harder than before to make your relationship the best part of both of your lives.

Matter-of-fact, triggering this mighty impulse is so essential to getting the best possible relationship with your man that once you send your man one of these "Secret Signals"...

...You will soon notice him expose his soul and heart to you in such a way he never expressed before and he'll distinguish you as the only woman in the galaxy who has ever truly appealed to him.