What are linked processes in Elixir?

Overview

In Elixir, code runs inside processes. Processes are secluded from each other, run side-by-side to one another, and interact via message passing. Processes are not only the reason for concurrency in Elixir, but they also provide the means for building shared and fault-tolerant programs.

Elixir’s processes should not be mixed with operating system processes. Processes in Elixir are notably lightweight in terms of memory and CPU (even compared to threads as used in many other programming languages). Because of this, it is not unusual to have tens or even hundreds of thousands of processes running concurrently.

Definition

The processes in Elixir can be mutually linked, and the processes linked with their other process (parent process) are automatically started by their parent process. These types of processes are called linked processes.

Process links have an essential role when developing parallel and fault-tolerant applications. They support instant shutting down of parts of the system or even the whole system when required. This prevents the application from operating in a poor state.

Problem

Linked processes in Elixir are the solution to a problem that is defined below.

Process linkage

In the above illustration the, 3rd3^{rd} Process has just crashed. Assume that the 2nd2^{nd} Process remains running, heedless of the crash. It presumes to obtain a result from the 3rd3^{rd} Process and report back to the 1st1^{st} Process. Nonetheless, this will never occur, so this system is left in a poor state.

Solution

Thanks to the process linking, we can avoid the above problem of the running system being in a poor state.

When two processes are linked, they form a unique connection. As soon as a process exits, it will notify the others. All processes in a chain of linked processes will eventually be notified when a crash occurs. By default, linked processes will eliminate and clean up the memory they use, limiting other potential problems down the line.

Linked processes

In the above illustration, the crash of 3rd3^{rd} Process will trigger a chain reaction, and 3nd3^{nd} Process will terminate, followed by 1st1^{st} Process.

Pragmatic example of linked processes

defmodule Countdown do
def timer do
receive do
:start ->
send(self(), {:tick,3})
{:tick, 0} ->
exit(:done)
{:tick, value} ->
Process.sleep(1000)
IO.puts(value)
send(self(), {:tick, value - 1})
end
timer()
end
def watcher do
timer_pid = spawn_link(&timer/0)
send(timer_pid, :start)
receive do
msg ->
IO.puts("I getting this message: #{inspect(msg)}")
after
4000 ->
IO.puts("I'm not getting any messages from this timer")
end
end
end
Countdown.watcher()

Explanation

  • Line 17: We link the watcher() function to the timer() function .

  • Line 18: When timer() exits, the watcher() function also dies due to the process linking. That’s why we are not getting any output from the watcher() function.

  • Output: We get the message ** (EXIT from #PID<0.94.0>) :done for the termination of the watcher() function.

Copyright ©2024 Educative, Inc. All rights reserved