Search code examples
erlangmonitor

Monitors in Erlang/OTP


I have a question about monitors.

1> Pid=spawn(fun() -> timer:sleep(500000) end).
2> exit(Pid, kill).
3> Ref=erlang:monitor(process, Pid).

4> flush().

The output of flush() in my shell is {'DOWN',#Ref<0.0.0.159>,process,<0.69.0>,noproc}

My question is: if process was killed before creating monitor, how come shell got the 'DOWN' message?


Solution

  • This is a feature which avoids a race condition. Keep in mind that for all the current process knows, the other process might die at any moment. Therefore, it might die just before or just after the call to erlang:monitor, and it would be very cumbersome to have to consider both cases for every monitor.

    That's why monitoring a dead process gives a message of the same form as the message you get when a monitored process dies. The only difference is that the exit reason is always given as noproc.