how "synchronous" is exit?

Raimo Niskanen <>
Wed May 31 08:49:45 CEST 2006


 (Damien Katz) writes:

> Ok, so it appears the code should do something like
> this:
> 
> Ref = erlang:monitor(process, Pid),
> exit(Pid, kill),
> receive
> {'DOWN', Ref, process, Pid, Reason} ->
>   do_something()
> end
>     
> Just to be sure, am I guaranteed the 'DOWN' message
> will only be received *after* the whole linked process
> tree under Pid is killed?

No, you are not.

When a process dies it sends exit signals to all it is
linked to. Its 'DOWN' message gets sent somewhere here.
Then the linked processes will do the exit when they get 
sheduled in themselves, could be much later...

You will have to monitor the critical process. Either
directly or indirectly; to get the property you assumed
you will have to write it a'la a supervisor, i.e a top-
level process that monitors its children and when
ordered to die kills its children, waits for the
'DOWN' message from them all, and then exits itself.
Perhaps a standard supervisor does this, I do not know.


> 
> The reason I ask, I have a process tree that modifies
> a file. The file sub-process is in the
> process tree.
> 
> My code wants to kill that process tree and start a
> new one that modifies the same file. Incomplete writes
> are not a problems, but race conditions are. I have to
> be sure another process isn't still making writes to
> the file, so I have to make sure the whole process
> tree is dead.
> 
> --- Richard Carlsson <> wrote:
> 
> > Damien Katz wrote:
> > > When issuing an exit(Pid, kill) to a another
> > process,
> > > is there any guarantee that the process, and all
> > its
> > > linked child processes, are dead before
> > continuing?
> > 
> > No. Signals (exit/2 sends a signal) are
> > asynchronous, just
> > like messages. In fact, messages are a special case
> > of signals.
> > 
> > > If not, can anyone tell me a good way to wait on a
> > > process tree to die before continuing an action?
> > 
> > Either using old-school bidirectional process links
> > and
> > setting the trap_exit process flag in the watching
> > process,
> > or the more modern, unidirectional process monitors.
> > See
> >
> http://www.erlang.org/doc/doc-5.5/lib/kernel-2.11/doc/html/erlang.html
> > and
> >
> http://www.erlang.org/doc/doc-5.5/doc/reference_manual/processes.html#10.6
> > (sections 10.6-10.8) for details.
> > 
> > 	/Richard
> > 
> > 
> 

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list