<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2800.1528" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=171334806-23082006>Very
good question! </SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=171334806-23082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=171334806-23082006>Why
the program in the book works and why your program does not, depends upon the
behaviour</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=171334806-23082006>of the
Erlang shell.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=171334806-23082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=171334806-23082006>The
Erlang shell used when the book was written and the Erlang shell in the current
Erlang distribution</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=171334806-23082006>work
differently - this difference invalidates the example in the
book.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=171334806-23082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=171334806-23082006>When
the book was written the Erlang shell evaluated each new command in a
fresh</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=171334806-23082006>process. Today the shell appears to evaluate all
commands in a single evaluator process </SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=171334806-23082006>and
only restart the evaluator if it fails.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=171334806-23082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=171334806-23082006>When
you evaluate</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=171334806-23082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=171334806-23082006>
link_demo:demonstrate_normal() </SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=171334806-23082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=171334806-23082006>in the
shell it merely creates a link to the demo process and do </SPAN></FONT><FONT
face=Arial color=#0000ff size=2><SPAN class=171334806-23082006>nothing
else</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=171334806-23082006>so
there is no exit.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=171334806-23082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=171334806-23082006>In the
old Erlang shell (the one used when we wrote the book)
link_demo:demonstrate_normal()</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=171334806-23082006>was
evaluated within a process (ie we spawn one process per shell command) - this
process</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=171334806-23082006>did
two things, it first evaluated the link(whereis(demo)) command linking itself to
demo</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=171334806-23082006>then
it died - that is where the exit comes from.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=171334806-23082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=171334806-23082006>To
emulate the old behaviour you can try this:</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=171334806-23082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=171334806-23082006> >
link_demo:start()</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=171334806-23082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=171334806-23082006>Then
</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=171334806-23082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=171334806-23082006> > spawn(fun() ->
link_demo:demonstrate_normal() end)</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2></FONT> </DIV>
<DIV><SPAN class=171334806-23082006><FONT face=Arial color=#0000ff size=2>In
this case you *will* get the behaviour you expect.</FONT></SPAN></DIV>
<DIV><SPAN class=171334806-23082006><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=171334806-23082006><FONT face=Arial color=#0000ff
size=2>/Joe</FONT></SPAN></DIV>
<DIV><SPAN class=171334806-23082006><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV><FONT face=Arial color=#0000ff
size=2></FONT><FONT face=Arial color=#0000ff size=2></FONT><FONT face=Arial
color=#0000ff size=2></FONT><FONT face=Arial color=#0000ff size=2></FONT><FONT
face=Arial color=#0000ff size=2></FONT><FONT face=Arial color=#0000ff
size=2></FONT><FONT face=Arial color=#0000ff size=2></FONT><FONT face=Arial
color=#0000ff size=2></FONT><FONT face=Arial color=#0000ff size=2></FONT><FONT
face=Arial color=#0000ff size=2></FONT><FONT face=Arial color=#0000ff
size=2></FONT><BR>
<BLOCKQUOTE
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
<DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>From:</B> owner-erlang-questions@erlang.org
[mailto:owner-erlang-questions@erlang.org] <B>On Behalf Of </B>lang
er<BR><B>Sent:</B> den 22 augusti 2006 18:22<BR><B>To:</B>
erlang-questions@erlang.org<BR><B>Subject:</B> trap_exit cannot trap signal
when exit/1 doesn't called explicitly<BR></FONT><BR></DIV>
<DIV></DIV>I run the example link_demo(page 103) in Erlang
book:<BR><BR><BR>-module(link_demo).<BR><BR>%%<BR>%% Include
files<BR>%%<BR><BR>%%<BR>%% Exported
Functions<BR>%%<BR>-export([start/0,demo/0,demonstrate_normal/0,<BR>
demonstrate_exit/1, demonstrate_error/0,demonstrate_message/1]).
<BR><BR>%%<BR>%% API Functions<BR>%%<BR><BR>start()
-><BR> register(demo, spawn(link_demo, demo,
[])).<BR><BR>demo() -><BR> process_flag(trap_exit,
true),<BR> demo1().<BR><BR>demo1()
-><BR> receive <BR>
{'EXIT', From, normal}
-><BR>
io:format("Demo process received normal exit from ~w~n",
[From]),<BR>
demo1();<BR> {'EXIT', From, Reason}
-><BR>
io:format("Demo process received exit signal ~w from ~w~n", [Reason, From]),
<BR>
demo1();<BR> finished_demo
-><BR>
io:format("Demo finished ~n",
[]);<BR> Other
-><BR>
io:format("Demo process message ~w~n",
[Other]),<BR>
demo1() <BR> end.<BR><BR><BR>%%<BR>%% Local
Functions<BR>%%<BR><BR>demonstrate_normal() -><BR>
link(whereis(demo)).<BR><BR>demonstrate_exit(What) -><BR>
link(whereis(demo)),<BR>
exit(What).<BR>demonstrate_message(What) -> <BR> demo !
What.<BR>demonstrate_error()-><BR>
link(whereis(demo)),<BR> 1 = 2.<BR><BR>-----------<BR>After
start(), I call demonstrate_normal(), demo process should trap the
exit(normal) signal, but it doesn't, <BR> and if I call
demonstrate_exit(normal) , demo do trap the exit(normal).<BR><BR>The only
difference between theses two situations is the exiting process call exit/1
explicitly. <BR><BR>Must I call exit before existing ? <BR><BR>My environment:
MacBook(Intel) <BR>Erlang (BEAM) emulator version 5.5 [source]
[async-threads:0]<BR><BR>Eshell V5.5 (abort with ^G)<BR>1>
<BR><BR>Thanks!<BR>Best Regards<BR>James<BR></BLOCKQUOTE></BODY></HTML>