[erlang-questions] remote spawns and sending messages to the remote PID

Lennart Öhman <>
Mon Apr 13 06:16:04 CEST 2009

Hi, the reason why this does not work is that you are not doing it the same way locally versus when doing it remote. The return value bound to the shell variable S in your example is not the process identifier of your counter process, merely some kind of intermediate process you choose to use in the remote example.

Best Regards

Lennart Öhman                   direct  : +46 8 587 623 27
Sjöland & Thyselius Telecom AB  cellular: +46 70 552 6735
Hälsingegatan 43, 10 th floor   fax     : +46 8 667 82 30
SE-113 31 STOCKHOLM, SWEDEN     email   : 

-----Original Message-----
From:  [mailto:] On Behalf Of Jeff Macdonald
Sent: den 13 april 2009 05:13
Subject: [erlang-questions] remote spawns and sending messages to the remote PID


I'm just getting my feet wet in erlang. I have Joe's book, but I
wanted to try some low level stuff myself.

I created a small counter module. It simply increments a value every
time it see's an inc message and sends a reply back to the caller
supplied PID. This works fine within a single node. However, I can't
see to get it to work correctly when I start two nodes on the same
computer using sname (foo and bar). First I spawn a local receive

()2> R=spawn(fun counter:loop/0).

Testing it on that node works:

()3> R ! {ok,5}.
Value is 5

Now I spawn the counter on the bar node:
()3> S=spawn('',counter,run,[5]).

and send it a message:
()4> S ! {inc, R}.

Normally when this is done on the local node, I get this:
()5> T=counter:run(5).
()6> T ! {inc, R}.
Value is 1

Note the Value line in the output.

I'm aware of the rpc library, but I thought I could do simple things
like above without it. Below is the code. I'm not currently using the
registered process name. It was my understanding that all that one
needed was a PID. If one used a registered process name instead, than
one needed the node too. Is that incorrect?


-export([run/1, counter/2, loop/0]).

run(Limit) ->
	S = spawn(counter, counter, [Limit, 0]),
	register(counter, S),
counter(Limit, Sum) ->
        	{value, Requester}	->
        		Requester ! {ok, Sum},
        		counter(Limit, Sum);

        	{inc, Requester} when Sum < Limit	->
		    	Requester ! {ok, Sum + 1},
		    	counter(Limit, Sum + 1);

	        {inc, Requester}	->
	        	Requester ! {error, limit},
	        	counter(Limit, Sum)

loop()	->
		{ok, V}	->
			io:format("Value is ~p~n",[V]),
		{error, Reason}	->
			io:format("Error ~p~n",[Reason]),

Jeff Macdonald
Ayer, MA
erlang-questions mailing list


More information about the erlang-questions mailing list