Processes in depth

A pid is a handle to a process, and the runtime can answer questions about it from the outside: is it alive, what is it doing, how long is its mailbox, what is it linked to. The answers come from erlang:process_info/2 and are the raw material of a monitor, a status page and a test. This lesson reads them, then follows an exit signal to its two outcomes.

process_info

process_info(Pid, Item) answers {Item, Value} or undefined when the process is gone. The items a program reads most are status (whether the process waits for a message), message_queue_len (the backlog), memory and current_function. A process that waits in a receive is waiting, and a message that no receive has taken raises the queue length:

inspect.erl

-module(inspect). -export([main/0]). main() -> Pid = spawn(fun() -> receive stop -> ok end end), io:format("~p~n", [is_process_alive(Pid)]), {status, Status} = erlang:process_info(Pid, status), io:format("~p~n", [Status]), {message_queue_len, Empty} = erlang:process_info(Pid, message_queue_len), io:format("~p~n", [Empty]), Pid ! hello, timer:sleep(10), {message_queue_len, One} = erlang:process_info(Pid, message_queue_len), io:format("~p~n", [One]), exit(Pid, kill).

erlc inspect.erl

erl -noshell -eval 'inspect:main(), halt().'

true waiting 0 1

The first zero is a process with nothing to do; the one after the send is a message that arrived and no receive took, because the child only matches stop. A growing message_queue_len is the first symptom of a server that is slower than its clients, and a page that samples it is cheaper than a profiler.

Links and monitors

process_info(Pid, links) answers the pids this process is linked to, and monitors the processes it watches. A program uses them to prove that a link was made, as a test does when it checks a supervision tree:

links.erl

-module(links). -export([main/0]). main() -> Pid = spawn_link(fun() -> receive stop -> ok end end), {links, Links} = erlang:process_info(self(), links), io:format("~p~n", [lists:member(Pid, Links)]), exit(Pid, kill).

erlc links.erl

erl -noshell -eval 'links:main(), halt().'

true

The list is not printed because it holds pids; the program asks whether the child is in it. The monitors item is shaped differently: each entry is {process, Pid}, because the monitor is one-directional. The monitors lesson covers the messages those watches deliver.

Exit signals

exit(Pid, Reason) sends an exit signal to another process. A process that does not trap exits dies with the reason; one that traps them receives {'EXIT', From, Reason} as a message and stays alive. The signal is not a request: it does not return an answer, and the sender does not wait for the target to die.

Why kill cannot be caught

The reason kill is special: it is untrappable, and the target dies with reason killed. That is the escape hatch for a process that ignores its shutdown, and the reason a supervisor uses it only as the last step. This program shows a trapping child survive shutdown and die of kill:

kill.erl

-module(kill). -export([main/0]). main() -> Parent = self(), Pid = spawn(fun() -> process_flag(trap_exit, true), Parent ! ready, receive stop -> ok end end), receive ready -> ok end, exit(Pid, shutdown), timer:sleep(10), io:format("~p~n", [is_process_alive(Pid)]), exit(Pid, kill), timer:sleep(10), io:format("~p~n", [is_process_alive(Pid)]).

erlc kill.erl

erl -noshell -eval 'kill:main(), halt().'

true false

The handshake before the first signal is not decoration: without it the exit can arrive before the child has run process_flag/2, and the child dies for the wrong reason. A shutdown protocol in a real system sends a request, waits for the answer, and uses the signal only when the answer does not come.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does erlang:process_info(Pid, status) answer for a process waiting in a receive?

    Answer

    waiting

    A process blocked in a receive is reported as waiting.

  2. What does message_queue_len count?

    Answer

    The messages in its mailbox that no receive took

    It counts the messages waiting in the mailbox at that moment.

  3. What does process_info(Pid, links) answer?

    Answer

    The pids linked to Pid

    The item holds the pids on the other end of the links of that process.

  4. Why is the kill reason special?

    Answer

    It cannot be trapped, and the target dies as killed

    kill is the untrappable signal, so even a process that traps exits dies.

  5. What does this module print? -module(q1). -export([main/0]). main() -> Pid = spawn(fun() -> receive stop -> ok end end), Pid ! hello, timer:sleep(10), {message_queue_len, N} = erlang:process_info(Pid, message_queue_len), io:format("~p~n", [N]), exit(Pid, kill).

    Answer

    1

    The child matches only stop, so hello stays in the mailbox and the length is one.

  6. What does this module print? -module(q2). -export([main/0]). main() -> Pid = spawn(fun() -> ok end), timer:sleep(10), io:format("~p~n", [erlang:process_info(Pid, status)]).

    Answer

    undefined

    A dead process has no information left, so process_info/2 answers undefined.

  7. What does this module print? -module(q3). -export([main/0]). main() -> Pid = spawn_link(fun() -> receive stop -> ok end end), {links, Links} = erlang:process_info(self(), links), io:format("~p~n", [lists:member(Pid, Links)]), exit(Pid, kill).

    Answer

    true

    spawn_link linked the child before it ran, so its pid is in the link list.

  8. What does this module print? -module(q4). -export([main/0]). main() -> Pid = spawn(fun() -> receive stop -> ok end end), Ref = erlang:monitor(process, Pid), {monitors, Monitors} = erlang:process_info(self(), monitors), io:format("~p~n", [lists:member({process, Pid}, Monitors)]), exit(Pid, kill), receive {'DOWN', Ref, process, Pid, _} -> ok end.

    Answer

    true

    The watcher holds the monitor as {process, Pid} in its monitors list.

  9. What does a trapping process receive for exit(Pid, shutdown)?

    Answer

    {'EXIT', From, shutdown}

    A trapped exit signal becomes an EXIT message with the sender and the reason.

  10. Why does the program wait for ready before the first exit/2?

    Answer

    So the child has set trap_exit before the signal arrives

    Without the handshake the exit can arrive before the child traps it, and the child dies for the wrong reason.

The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Автор статьи: Arthur Isaev