Spawning processes

Ulf Wiger etxuwig@REDACTED
Fri Feb 14 12:14:03 CET 2003


On Fri, 14 Feb 2003, Vlad Dumitrescu (EAW) wrote:

>Hi,
>
>Since it's friday, I hope I may be excused if I just don't
>see the obvious. Please point it out to me! :-)
>
>I need to have a named process available at all times. The solution I came up with is to have each interface function do something like
>    Pid = start(),
>    Pid ! Msg

>where start() checks if there is a process registered, and
>if not starts it and registers; otherwise the current Pid
>is returned. This works fine, until one tries to do several
>calls in a row to an unexisting process: all try to start
>and register the server, because the registration doesn't
>have time to do it's job...
>
>Is there another way to do something like this? I need to
>introduce this in a working system and don't know from
>which process it will be called first, and also have no
>control over the startup order...

If you could serialize the start commands using a server,
this would solve the problem, but then you might have the
same problem with starting the serialization server. ;)

You could use

Pid = case whereis(Name) of
         undefined ->
            global:trans({{start,Name},self()},
                         fun() ->
                            start()
                         end);
         P when pid(P) ->
            P
      end.

/Uffe
-- 
Ulf Wiger, Senior Specialist,
   / / /   Architecture & Design of Carrier-Class Software
  / / /    Strategic Product & System Management
 / / /     Ericsson AB, Connectivity and Control Nodes




More information about the erlang-questions mailing list