[erlang-questions] Some problem abount exit signal?

sky <>
Thu Dec 2 05:37:15 CET 2010


Hi guys,

In the Joe's book <<progamming erlang>>, page 163, there are some 
description about exit signal :

When an exit signal arrives at a process, then a number of different
things might happen. What happens depends upon the state of the
receiving process and upon the value of the exit signal and is deter-
mined by the following table:


but when use edemo1:start(true, {die, kill}), the result is:

edemo1:start(true, {die,kill}).
Process b received {'EXIT',<0.73.0>,kill}
process b (<0.72.0>) is alive
process c (<0.73.0>) is dead
ok

Why process b is alive? Cause as the descripion in this book,  when the 
exit signal

{'EXIT',<0.73.0>,kill}

arrived, the process b will die and broadcast the exit signal killed to 
the link set.

the souce code of edemo1 module as following:

%% ---
%%  Excerpted from "Programming Erlang",
%%  published by The Pragmatic Bookshelf.
%%  Copyrights apply to this code. It may not be used to create training material,
%%  courses, books, articles, and the like. Contact us if you are in doubt.
%%  We make no guarantees that this code is fit for any purpose.
%%  Visit http://www.pragmaticprogrammer.com/titles/jaerlang for more book information.
%%---

-module(edemo1).
-export([start/2]).

start(Bool, M) ->
     A = spawn(fun() ->  a() end),
     B = spawn(fun() ->  b(A, Bool) end),
     C = spawn(fun() ->  c(B, M) end),
     sleep(1000),
     status(b, B),
     status(c, C).


a() ->
     process_flag(trap_exit, true),
     wait(a).

b(A, Bool) ->
     process_flag(trap_exit, Bool),
     link(A),
     wait(b).

c(B, M) ->
     link(B),
     case M of
	{die, Reason} ->
	exit(Reason);
	{divide, N} ->
	1/N,
	    wait(c);
	normal ->
	true
     end.



wait(Prog) ->
     receive
	Any ->
	io:format("Process ~p received ~p~n",[Prog, Any]),
	    wait(Prog)
     end.



sleep(T) ->
     receive
     after T ->  true
     end.

status(Name, Pid) ->	
     case erlang:is_process_alive(Pid) of
	true ->
	io:format("process ~p (~p) is alive~n", [Name, Pid]);
	false ->
	io:format("process ~p (~p) is dead~n", [Name,Pid])
     end.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20101202/44868e5d/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: moz-screenshot.png
Type: image/png
Size: 21681 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20101202/44868e5d/attachment.png>


More information about the erlang-questions mailing list