Inspired by Stuff Goes Bad - Erlang in Anger
defmodule Diags do
@doc """
Generate a list of all processes in the system which are neither linked nor monitored.
"""
def list_non_linked_non_monitored_processes do
Process.list |>
Enum.map( fn pid -> {pid, Process.info( pid, [:links,:monitors,:registered_name])} end) |>
Enum.map(
fn {pid, words} -> {pid,
Keyword.get(words,:links),
Keyword.get(words,:monitors),
Keyword.get(words,:registered_name)
}
end ) |>
Enum.filter(fn
{_,[],[],_} -> true
_ -> false
end)
end
end
iex(153)> idler = fn -> receive do x -> IO.puts("#{inspect x}") end end
#Function<20.50752066/0 in :erl_eval.expr/5>
iex(154)> Diags.list_non_linked_non_monitored_processes
[{#PID<0.130.0>, [], [], []}]
iex(155)> spawn(idler)
#PID<0.1029.0>
iex(156)> spawn(idler)
#PID<0.1031.0>
iex(157)> Diags.list_non_linked_non_monitored_processes
[{#PID<0.130.0>, [], [], []}, {#PID<0.1029.0>, [], [], []},
{#PID<0.1031.0>, [], [], []}]