[erlang-questions] Communicating processes

Taavi Talvik taavi@REDACTED
Mon Mar 26 20:51:23 CEST 2007


On Mar 26, 2007, at 9:05 PM, Fredrik Hoback wrote:

> Hi, I'm having some trouble with inter-process communication. First  
> of all I have a process which receives a message it looks something  
> like this:

There is difference between {Msg} and Msg. First is tuple with one  
element.

1> M="hello".
"hello"
2> {M}=M.

=ERROR REPORT==== 26-Mar-2007::21:33:19 ===
Error in process <0.30.0> with exit value: {{badmatch,"hello"}, 
[{erl_eval,expr,3}]}

** exited: {{badmatch,"hello"},[{erl_eval,expr,3}]} **


2) probably function send can look like
send(Msg) ->
    proxy ! Msg.


3) If you want to get answer, you should tell receiver where to  
return answer.

send(Msg) ->
    proxy ! {self(), MSG}.

receive_msg() ->
   receive
       {Pid_To_Answer, Msg} ->
          % proxy is registered process
           % http://www.erlang.org/doc/doc-5.5.3/lib/kernel-2.11.3/ 
doc/html/erlang.html#register/2
           Pid_To_Answer ! io_lib:format("got message ~p", [Msg])
       Msg ->
           io:format("Got really strange message without pid() for  
sending answer : ~p~n", [Msg])
   end.


best regards,
taavi



More information about the erlang-questions mailing list