[erlang-questions] Looking for examples of interaction between gen_server and gen_fsm

Matt Williamson <>
Thu Aug 28 19:47:36 CEST 2008

The gen_server can start a tcp server and then call gen_fsm:start_link/3 in
the init callback and put the PID from the result {ok, PID} in state. As
things come through the tcp in gen_server, you can use
gen_fsm:sync_send_event/3 to send the event to the gen_fsm and it will reply
to tell the gen_server what to do and the gen_fsm will keep track of state.

If the gen_fsm dies, the gen_server will be restarted as well, unless you
put process_flag(trap_exit, true) in the init callback.

On Thu, Aug 28, 2008 at 3:46 AM, Christian Czura

> Hi,
> it's a little bit hard for me to grasp how to plug together gen_servers
> and gen_fsms that belong to the same service or operation.
> For example, Joe shows[1] how to abstract away the protocol of a
> webserver, so you end up having three processes[2]:
> server <-> some_protocol <-> tcp/udp
> I would write the server-process as a gen_server and the
> protocol-process as a gen_fsm.
> Now I wonder how/where to start the protocol-process from the server:
> Would I start it from the server's init function and save its pid in the
> server's state?
> Or would I start both the server and the protocol-process via the
> supervisor / supervision strategy?
> If so, how does the server know about the protocol-process? It's obvious
> that the protocol-process's pid needs to be passed along somehow to the
> server.
> You might have noticed that I'm not sure when to use a supervision
> strategy or when to spawn a process from within a process that is
> supervised. That is because I don't know how/if it's possible in a
> supervision strategy to pass along the pid of one process to another.
> {M,F,A} in the childspec comes to mind, but how do I get the pid of
> another process in the same childspec, and is this the right way of
> doing things?
> If you know any other helpful code example, feel free to post the link. :-)
> Links:
> [1]: http://www.sics.se/~joe/tutorials/web_server/web_server.html<http://www.sics.se/%7Ejoe/tutorials/web_server/web_server.html>
> [2]: http://www.sics.se/~joe/tutorials/web_server/web_server.jpg<http://www.sics.se/%7Ejoe/tutorials/web_server/web_server.jpg>
> Thanks,
> Chris
> _______________________________________________
> erlang-questions mailing list
> http://www.erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080828/eb4c6003/attachment.html>

More information about the erlang-questions mailing list