title: spawn_monitor registered name in different module
body: <p>I'm trying to monitor a process with a registered name in a different module than where the monitor code is placed. This is an assignment for school, which is why I'm not going to post all of my code. However, here's the outline:</p>
  
  <p><code>module1:start()</code> spawns a process and registers its name:</p>
  
  <pre><code>register(name, Pid = spawn(?MODULE, loop, [])), Pid.
  </code></pre>
  
  <p>The loop waits for messages. If the message is of the wrong type it crashes.</p>
  
  <p><code>module2:start()</code> should start the registered process in <code>module1</code> and monitor it, restarting it if it's crashed. I've been able to get it working using:</p>
  
  <pre><code>spawn(?MODULE, loop, [module1:start()]).
  </code></pre>
  
  <p>Then in the loop function I use <code>erlang:monitor(process, Pid)</code>.</p>
  
  <p>This way of solving the problem means the registered process can crash before the monitoring starts. I've been looking at <code>spawn_monitor</code>, but haven't been able to get the monitoring to work. The latest I've tried is:</p>
  
  <pre><code>spawn(?MODULE, loop, [spawn_monitor(name, start, [])]).
  </code></pre>
  
  <p>It starts the registered process. I can send messages to it, but I can't seem to detect anything. In the loop function I have a <code>receive</code> block, where I try to pattern match <code>{'DOWN', Ref, process, Pid, _Why}</code>. I've tried using <code>spawn_monitor</code> in <code>module1</code> instead of simply <code>spawn</code>, but I noticed no change. I've also been trying to solve this using links (as in <code>spawn_link</code>), but I haven't gotten that to work either.</p>
  
  <p>Any suggestions? What am I monitoring, if I'm not monitoring the registered process?</p>