spawn
and spawn_link
are the two basic Erlang functions for starting a new process. Both will create a process which then calls the function with arguments as specified in the arguments to spawn
/spawn_link
. When that function ends the process automatically terminates with the exit reason normal
. The difference between the functions is that spawn_link
also creates a link between the two processes.
The gen_server:start_link
function does much more than just creating the process by initiating the behaviour and then running a behaviour top-loop which provides all the behaviour functionality. Amongst other things the callback function init
is called to initialise the behaviour and then return {ok,State}
to tell the behaviour that everything has been initialised and went well and here is the local state it is to pass into all the callbacks. The callback functions of a gen_server are not meant to be called directly but by the behaviour.
So when you explicitly spawn a process just running the init
function it will terminate as soon as the init
function ends. This is what happening here.
The {'EXIT',Pid,Reason}
messages comes from the processes being linked and that the process is trapping exits. When a process dies an exit signal is sent from the dying process to all the processes to which it is linked. When this signal arrives at a process trapping exits then it is converted to normal message and put in that processes message queue. This is what you are seeing here. Note that all this is done automatically because of the link and trapping exits.
I hope that helps. Sorry for being a bit over-didactic here.