Elixir: what happens to the process after the function is executed

I try to understand the differences between spawnand spawn_link, but I can not understand what happens when the function ends.

defmodule SendAndDie do
  def send_and_die(target) do
    send(target, "Goodbye")
    # Process.exit(self, :boom)
  end
end

dying_process = spawn_link(SendAndDie, :send_and_die, [self])
:timer.sleep(500)
IO.puts("Dying process is alive: #{Process.alive?(dying_process)}")
receive do
  msg -> IO.puts(msg)
end

I expected the main process to fail, as it is associated with a process that apparently died before the end of the program. However, a goodbye message is printed and then the program exits the system. Change spawn_linkto is spawnnot affected.

When I uncomment Process.exiton line 4, I see the difference between spawnand spawn_link(later the whole program stops, while the first one does not work). But it Process.exitis the last execution of the function send_and_die. Doesn't the process end when the function ends?

+4
1

erlang

, , , , , .

normal, .

+7

All Articles