Sanity check: spawn, spawn_link, iex

I am trying to understand how spawn / spawn_link interplays with iex.

spawn

process p runs:
  c = spawn(f)

if c dies while executing f, nothing happens to p

spawn_link

process p runs:
  c = spawn_link(f)

if c dies while executing f, p dies too

iex behaviour

So now for the consuing part: iex is something like

loop {
  spawn_and_block_on_root_shell();
}

so if I am running the above in iex, in the spawn_link case, process p dies, but then the ‘iex loop’ creates a new p_2 to replace it ?

Is the above correct ?

Roughly, yes.

In pseudo code it would roughly look like this:

loop {
  i = read_input
  try {
    i |> eval |> IO.inspect
  } except e {
    IO.print(e)
  }
}

In a more detailed view though, you’ll find supervisors instead of try/catch and isolated processes. Remember that there are even ways to switch between different “subsessions” within one iex session using the CTRL-C menu. Though this feature is probably not known by much and used by less :wink:

2 Likes