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.
-
What does
erlang:monitor(process, Pid)answer?Answer
A reference
It answers a monitor reference, a fresh value that identifies this monitor.
-
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.
-
What does the watcher receive when the monitored process dies?
Answer
{'DOWN', Ref, process, Pid, Reason}A monitor delivers a
DOWNtuple with the reference, the pid, and the reason. -
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.
-
What does
watcher:watch()answer?Answer
boom, the exit reasonThe child exits with reason
boom, andwatch/0returns that reason from the DOWN message. -
What does
erlang:demonitor(Ref, [flush])do?Answer
Stops the monitor and removes a waiting DOWN
The monitor is removed, and
flushalso clears a DOWN message already in the mailbox. -
What does
erlang:demonitor(Ref)answer?Answer
trueThe call answers
true. -
Why does
watcher:forget()leave the mailbox empty?Answer
It demonitors with
flushbefore the DOWN arrivesThe monitor is gone before the child finishes sleeping, so no DOWN message is delivered.
-
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.
-
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