[erlang-questions] Erlang Message passing questions

Joe Armstrong <>
Tue Jun 2 20:18:32 CEST 2009


On Tue, Jun 2, 2009 at 6:43 PM, Greg Perry <> wrote:
> Hello again,
>
> I have been reading Joe Armstrong's excellent "Programming Erlang" book
> and I have been studing the message passing architecture of Erlang.  For
> my test application I want to be able to create for example 20,000
> processes, each of which are performing an interative function that will
> eventually return with a result.  The question is this:  in Joe's book
> the examples given for message passing appear to be polled environments,
> where processes are polled periodically for an answer to a message.
>

Not really - processes are taken out of the run-queue when waiting for
a message. When a message arrives the message is put in the inbox of a
process and the process is put back into the run-queue. When it
eventually gets its time-slice it checks the new messages and if they
don't match puts them in a save queue. If no message match the process
is taken out of the run-queue again.
If a message matches the process is run and any saved messages are
restored. There is no polling.

> Does this present problems from a performance perspective, ie in a high
> performance environment would it make more sense to start a collection
> of processes, have those processes "do something" and then eventually
> return with their result?

This depends upon the problem. Sometimes we spawn processes on demand
and when needed - other times we might keep a fixed pool of processes
and allocate jobs to them. It's a good idea to
write your code so that you can easily swap between these two
methods so you can do some measurements and find out which
method is best.

>
> I guess the best way of describing the architecture is a star topology
> where a single supervisory process dispatches processes, each process
> works on a task and then returns with a result at some undetermined
> future time.  What would the easiest way be to create a batch of
> processes (say for example Joe's prime number example on page 357), then
> have each of those processes return with an answer message once
> complete?

You could use a star but if there are lots of processes the central
point might be a bottleneck. You could use a tree of processes
which collect results and pass their answers back up the tree
(this is the reduce step in the map-reduce algorithm)

>
> Also, based upon the immutability of Erlang how are iterative functions
> accomplished such as counter incrementing?

loop(N) ->
    receive
       bump ->
           loop(N+1)
   end.


>
> Regards
>
> Greg
>

/Joe


>
>
>
>
>
>
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>


More information about the erlang-questions mailing list