[erlang-questions] How to do counters?

Joe Armstrong erlang@REDACTED
Tue Jun 30 12:47:09 CEST 2009


On Tue, Jun 30, 2009 at 3:35 AM, Jarrod Roberson<jarrod@REDACTED> wrote:
> On Mon, Jun 29, 2009 at 7:46 PM, Richard O'Keefe <ok@REDACTED> wrote:
>
>> As a general rule, don't register a process unless there is
>> a REALLY good reason to do so.
>
>
> Is the reason not to register processes performance related or is it a "best
> practices" thing?
> And if it is performance related, how would someone know if they have a
> REALLY good reason to register the process?

It's because of namespace pollution and security.

If a Pid is secret then nobody can do exit(Pid, kill) on it - if you
register the Pid
and say register(foo, Pid) then *anybody* can do exit(whereis(foo), kill).

When you register a process it becomes easy to find - but it's
insecure  *anybody* can mess with
it if they know the name.

Inventing names is *difficult* in any programming language -
registered process names are *global*
so if two people wrote independent code using registered names then
they might accidently
choose the same name. Because module names are unique we often choose
?MODULE as the registered name
of a process.

So you'll often see

         -module(foo).

         start() ->
              register(?MODULE, spawn( fun() -> .... end)

in a module. ?MODULE expands to the local module name (in this case
foo). So the foo module (in foo.erl)
creates the foo registered process. (this is ok if foo.erl creates
*one* global process and no more)

For a registered name there is a small overhead of associating the
name with a Pid - this
is probably extremely small.

/Joe


On Tue, Jun 30, 2009 at 3:35 AM, Jarrod Roberson<jarrod@REDACTED> wrote:
> On Mon, Jun 29, 2009 at 7:46 PM, Richard O'Keefe <ok@REDACTED> wrote:
>
>> As a general rule, don't register a process unless there is
>> a REALLY good reason to do so.
>
>
> Is the reason not to register processes performance related or is it a "best
> practices" thing?
> And if it is performance related, how would someone know if they have a
> REALLY good reason to register the process?
>


More information about the erlang-questions mailing list