Preventing multiple instances through register/2

Jeff Crane <>
Sun May 14 22:49:22 CEST 2006


>If the name is already taken, just exit

>Let the spawned process try to register itself, or
exit if it does not succeed.

%%Start gen_server()
start()->
	%% greeting msg, text output
	io:format(" Started test_server on:~p~n",[self()]),
        %% Single assign to new var, the PID of the
new load_accounts/0 spawn
	Pid = spawn(?MODULE, load_accounts, []),

        %% Case on result of catch
        %% catch on register/2 which always goes
through, even with error
        %% register new var as test_server
	case(Err = (catch register(test_server,Pid))) of
                %% Some kind of failure
		{'EXIT', _} ->
                        %% Atom output
			already_registered,
%%			io:format(" test_server:already
registered:~p~n",[Err]),
			%% Kill spawned process, via msg
			Pid ! exit;
                %% If it worked, leave the spawn alone
		_ ->
                        %% move on
			good_start

	%% This process dies here. Spawned process is either
living, or was killed.
	end.

Can anyone explain to me why the atom
already_registered does not echo to the screen, but
the io:format (commented out) does?

Without registering, how do you send messages to a PID
from the command line?

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the erlang-questions mailing list