Link Processes
Learn to link processes in this lesson.
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_link
to 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/3
eliminates this race condition. It’s like spawning a process and then linking, except that it’s an atomic action. There’s no time ...