I would like to know how erlang processes are related to gen behaviours (gen_server, gen_fsm, gen_event). Like for example is a a gen_server a single process, group of processes or do they have no relation al all
3 Answers
A behavior such as gen_server
is a single Erlang process executing a recursive function call in which the state of the behavior is stored. Please refer to my answer to Erlang/OTP behaviors for beginner for more details.
gen_server is a 'behaviour'. It's some agreement about module structure. What functions are available in the module, what are signatures of that functions , what values they could return etc etc.
otp architecture assumes that some processes runs on that modules.
So we can say -- certain process is gen-server if it runs on module with gen_server behaviour, and was started by function, intended to start gen_server (start() or start_link() ).
Different gen behaviours have different relations to processes. gen_server
and gen_fsm
have one process per instance (you can have multiple processes running the same gen_server
code). Whereas gen_event
is running the handle_event callbacks synchronously in the context of the calling process (gen_event:call
) or asynchronous in the context of the event manager process (gen_event:notify
).
The gen behaviours are there to abstract away the concurrency and use processes to achieve this. How they are doing this and if there is a 1:1 relationship between processes and gen behaviours depends on the implementation of the actual gen behaviour.