Multiple behaviour processes

Vlad Dumitrescu <>
Thu Jan 6 20:24:25 CET 2005


Hi,

>I have two issues I couldn't figure out:
>1. When I start the second one I am getting an error:
>59> test_fsm:start().
>{ok,<0.140.0>}
>60> test_fsm:start().
>{error,{already_started,<0.140.0>}}

My guess is that you are using the standard gen_fsm template that comes in 
the Emacs mode for Erlang. The code there looks like

    start_link() ->
        gen_fsm:start_link({local, ?SERVER}, ?MODULE, [], []).

which registers a name for the started process, and of course you can't 
start several with the same name.

You can change this to either create anonymous processes
    start_link() ->
        gen_fsm:start_link(?MODULE, [], []).

or you can send the name as an argument and take care not to use them twice
    start_link(FsmName) ->
        gen_fsm:start_link({local, FsmName}, ?MODULE, [], []).


>2. As far as I understand, the only way to notify an FSM is to use the 
>gen_fsm:send_event function. I don't see a >way to send an event to a 
>specific instance of my fsm (Pid ! Msg doesn't work of cource).

If you call Pid ! Msg, then the handle_info callback will be called. The 
reason for the separation between messages and events is (as I see it) that 
a process has to be able to discern between them. Messages can be system 
messages for example notifications that a linked process has died or from a 
debugger/tracer/profiler, and such things should not be handled in the same 
place as the events (which are supposed to be the "business interface" to 
the fsm).

If you really need to translate such regular messages to (asynchronous) 
events, then you can extend handle_info to call gen_fsm:send_event, but I'm 
not sure it's a good idea from a "best practices" point of view.

Hope this helps. Regards,
Vlad




More information about the erlang-questions mailing list