[erlang-questions] ned function

Raimo Niskanen <>
Fri Sep 19 16:51:45 CEST 2008


On Fri, Sep 19, 2008 at 07:06:39AM -0700, Zvi wrote:
> 
> Hi,
> 
> I trying to implement some Fork-Join constructs in Erlang using links or
> monitors.
> I do not understand why when process finished executing function normally,
> it's (function's) return value isn't put into 'EXIT' or 'DOWN' messages?
> I.e. instead of atom 'normal' return {normal, ResultOfFun} or something
> similar.
> 

Nope. Functions return values, processes does not.

One dirty way is to end with exit({done,Value}) and wait
for {'DOWN',Ref,_,Pid,{done,Value}}.

> {'EXIT',Pid,normal}
> 
> {'DOWN',Ref,process,Pid,normal}
> 
> 
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> -module(fj).
> -compile(export_all).
> 
> -define(N, erlang:system_info(schedulers) ).
> 
> test_monitor() ->
> 	% fork
> 	PidRefs = [spawn_monitor(
> 			fun() -> io:format("pid: ~w~n",[self()]) end) ||
> 		_<-lists:seq(1,?N)],
> 	io:format("PidRefs: ~w~n", [PidRefs]),
> 	% join
> 	[receive {'DOWN',Ref,process,Pid,_}->ok end || {Pid,Ref}<-PidRefs ].
> 	
> test_link() ->
> 	% fork
> 	 process_flag(trap_exit, true),
> 	Pids = [spawn_link(
> 			fun() -> io:format("pid: ~w~n",[self()]) end) ||
> 		_<-lists:seq(1,?N)],
> 	io:format("Pids: ~w~n", [Pids]),
> 	% join
> 	[receive {'EXIT',Pid,_}->ok end || Pid<-Pids ].
> 
> P.S.
> if there is some better ways to implement fork join please let me know.
> 
> Zvi
> -- 
> View this message in context: http://www.nabble.com/value-of-spawned-function-tp19573059p19573059.html
> Sent from the Erlang Questions mailing list archive at Nabble.com.
> 
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list