Monitors and exits

monitor/2

The monitor function watches a pid from the outside. It is one-directional and needs no mutual agreement, unlike a link.

When the watched process dies, the watcher receives a DOWN message with a reference that identifies the monitor. That reference is a fresh value every time, so the session checks its type instead of printing it.

erl

1> is_reference(erlang:monitor(process, spawn(fun() -> receive stop -> ok end end))).
true

'DOWN' messages

The DOWN message carries the reference, the watched pid, and the exit reason. Match on the tag and the reference to tell monitors apart.

Two monitors on the same process are two separate DOWN messages with two references. This module starts a child, watches it die, and also shows a monitor that is dropped before it fires; save it as watcher.erl.

-module(watcher). -export([start/0, watch/0, forget/0]). start() -> Pid = spawn(fun() -> receive go -> exit(boom) end end), register(subject, Pid), ok. watch() -> Pid = whereis(subject), Ref = erlang:monitor(process, Pid), Pid ! go, receive {'DOWN', Ref, process, Pid, Reason} -> Reason end. forget() -> Pid = spawn(fun() -> timer:sleep(1000) end), Ref = erlang:monitor(process, Pid), erlang:demonitor(Ref, [flush]), ok.

erlc watcher.erl
erl

1> watcher:start().
ok
2> watcher:watch().
boom
3> watcher:forget().
ok
4> receive M -> M after 0 -> none end.
none

demonitor/1

The demonitor function stops a monitor before it fires. Use the flush option to also remove a DOWN message already waiting in the mailbox.

Monitoring a dead process is safe: the DOWN has already been delivered or is on its way.

erl

1> erlang:demonitor(erlang:monitor(process, self())).
true

Links or monitors

Links fail together: use them inside a supervision tree that should die as one unit. Monitors observe without being touched: use them for one-way watching.

When in doubt, monitor. Links belong to structure, monitors to observation.

Next: GenServer

Questions

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

  1. What does erlang:monitor(process, Pid) answer?

    Answer

    A reference

    It answers a monitor reference, a fresh value that identifies this monitor.

  2. How is a monitor different from a link?

    Answer

    It is one-directional and needs no agreement

    The watcher observes; the watched process is not affected by the monitor.

  3. What does the watcher receive when the monitored process dies?

    Answer

    {'DOWN', Ref, process, Pid, Reason}

    A monitor delivers a DOWN tuple with the reference, the pid, and the reason.

  4. Why match on the reference in a DOWN message?

    Answer

    To tell several monitors apart

    Each monitor has its own reference, so the reference says which watch has ended.

  5. What does watcher:watch() answer?

    Answer

    boom, the exit reason

    The child exits with reason boom, and watch/0 returns that reason from the DOWN message.

  6. What does erlang:demonitor(Ref, [flush]) do?

    Answer

    Stops the monitor and removes a waiting DOWN

    The monitor is removed, and flush also clears a DOWN message already in the mailbox.

  7. What does erlang:demonitor(Ref) answer?

    Answer

    true

    The call answers true.

  8. Why does watcher:forget() leave the mailbox empty?

    Answer

    It demonitors with flush before the DOWN arrives

    The monitor is gone before the child finishes sleeping, so no DOWN message is delivered.

  9. When should you use a link instead of a monitor?

    Answer

    Inside a supervision tree that must fail as one unit

    Links tie failures together, which is what a supervisor wants from its children.

  10. Is it safe to monitor a process that is already dead?

    Answer

    Yes, the DOWN message arrives at once

    The monitor is created and the DOWN is delivered immediately, with reason noproc.

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.

Article author: Arthur Isaev