trap_exit cannot trap signal when exit/1 doesn't called explicitly

lang er <>
Tue Aug 22 18:22:29 CEST 2006


I run the example  link_demo(page 103) in Erlang book:


-module(link_demo).

%%
%% Include files
%%

%%
%% Exported Functions
%%
-export([start/0,demo/0,demonstrate_normal/0,
                         demonstrate_exit/1,
demonstrate_error/0,demonstrate_message/1]).

%%
%% API Functions
%%

start() ->
    register(demo, spawn(link_demo, demo,  [])).

demo() ->
    process_flag(trap_exit, true),
    demo1().

demo1() ->
    receive
        {'EXIT', From, normal} ->
            io:format("Demo process received normal exit from ~w~n",
[From]),
            demo1();
        {'EXIT', From, Reason} ->
            io:format("Demo process received exit signal ~w from ~w~n",
[Reason, From]),
            demo1();
        finished_demo ->
            io:format("Demo finished ~n", []);
        Other ->
            io:format("Demo process message ~w~n", [Other]),
            demo1()
    end.


%%
%% Local Functions
%%

demonstrate_normal() ->
    link(whereis(demo)).

demonstrate_exit(What) ->
    link(whereis(demo)),
    exit(What).
demonstrate_message(What) ->
    demo ! What.
demonstrate_error()->
    link(whereis(demo)),
    1 = 2.

-----------
After start(), I call  demonstrate_normal(), demo process should trap the
exit(normal) signal, but it doesn't,
 and if  I call  demonstrate_exit(normal) , demo do trap the exit(normal).

The only difference between theses two situations is the exiting process
call exit/1 explicitly.

Must I call exit before existing ?

My environment: MacBook(Intel)
Erlang (BEAM) emulator version 5.5 [source] [async-threads:0]

Eshell V5.5  (abort with ^G)
1>

Thanks!
Best Regards
James
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20060823/8c59918a/attachment.html>


More information about the erlang-questions mailing list