spawn without linking?

Joe Armstrong <>
Tue Apr 20 11:32:36 CEST 2004


> 
> 	Hi, I'm pretty new to erlang, but one thing that seems to have caused 
> me problems in my toys so far is spawning a process without linking.  
> When is it a good idea to do such a thing?  Every time I've thought it 
> was, it just ended up covering up a bug that ended up being difficult 
> to track down.


Things that you want to be independent should not be linked together.

If you have two entirely different applications A and B you absolutely
do not want A to be linked to B.

If you have some error logger L then you *do* want L to be linked to A
and L to be linked to B. Errors in A or B should be reported by L.

Links are in the language for error detection purposes. When a process
terminates other processes in the system might need to know about this
so they can do something about the situation.

Suppose X dies - which other processes should be told about this?

Answer: If X evaluates link(Y) then Y will told if X dies (this is symmetric
so Y might evaluate link(X) for the same effect).

spawn_link could *almost* be defined like this:

	spawn_link(F) ->
		Pid = spawn(Fun),
		link(Pid),
		Pid.

But this is not correct (Why? - the process might die *between* the spawn
and the link) - so spawn_link does spawn and link atomically.

If you're new to Erlang I'd use spawn_link - as you build sexier applications
you should think *carefully* about what should happen when things die and try to
stop errors propagating between independent components.

You might like to look at the documentation for erlang:monitor (do > erl -man man erlang)
- this sets up a asymmetric link - which is often very useful.

Cheers

/Joe



> 
> --
> SPY                      My girlfriend asked me which one I like better.
> pub  1024/3CAE01D5 1994/11/03 Dustin Sallings <>
> |    Key fingerprint =  87 02 57 08 02 D0 DA D6  C8 0F 3E 65 51 98 D8 BE
> L_______________________ I hope the answer won't upset her. ____________
> 




More information about the erlang-questions mailing list