Link Processes
Explore the concept of linking processes in Elixir to detect when a process crashes and understand how this leads to error recovery through process supervision. Learn to use spawn_link for atomic process creation and linking, and how trapping exits transforms exit signals into manageable messages. This lesson helps you grasp the mechanics behind reliability in concurrent Elixir applications and prepares you to work effectively with supervisors.
We'll cover the following...
The way to get processes to know when other processes crash is to link them. That’s what we explore next.
New IEx session
First, let’s alias the DemoProc module:
alias IslandsEngine.DemoProc
linked = spawn(DemoProc, :loop, [])
Process.link(linked)
Process.alive?(linked)
send(linked, "Hello Process!")
Use
spawn_linkto avoid race conditionsWhen we spawn a process first, then link to it, there’s a small window of time where either process might terminate before the link is complete.
Kernel.spawn_link/3eliminates this race condition. It’s like spawning a process and then linking, except that it’s an atomic action. There’s no ...