Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
file: g.erl
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
%% Public API
start(Name) ->
gen_server:start({local, Name}, ?MODULE, [], []).
stop(Name) ->
gen_server:call(Name, stop).
state(Name) ->
gen_server:call(Name, state).
crash(Name) ->
gen_server:call(Name, crash).
%% gen_server call backs
init([]) ->
say("init", []),
{ok, []}.
handle_call(stop, _From, State) ->
say("stopping by ~p, state was ~p.", [_From, State]),
{stop, normal, stopped, State};
handle_call(state, _From, State) ->
say("~p is asking for the state.", [_From]),
{reply, State, State};
handle_call(crash, _From, State) ->
say("~p is asking for the state.", [_From]),
{xxxreply, State, State};
handle_call(_Request, _From, State) ->
say("call ~p, ~p, ~p.", [_Request, _From, State]),
{reply, ok, State}.
handle_cast(_Msg, State) ->
say("cast ~p, ~p.", [_Msg, State]),
{noreply, State}.
handle_info(_Info, State) ->
say("info ~p, ~p.", [_Info, State]),
{noreply, State}.
terminate(_Reason, _State) ->
say("terminate ~p, ~p", [_Reason, _State]),
code_change(_OldVsn, State, _Extra) ->
say("code_change ~p, ~p, ~p", [_OldVsn, State, _Extra]),
{ok, State}.
%% Private functions
say(Format) ->
say(Format, []).
say(Format, Data) ->
io:format("~p:~p: ~s~n", [?MODULE, self(), io_lib:format(Format, Data)]).
file: sup.erl
%% External exports
-export([start_link/0, add_child/1]).
%% supervisor callbacks
%% @spec start_link() -> ServerRet
%% @doc API for starting the supervisor.
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
add_child(Name) ->
Spec = {Name,
{g, start, [Name]},
permanent, 1000, worker, dynamic},
supervisor:start_child(?MODULE, Spec).
%% @spec init([]) -> SupervisorTree
%% @doc supervisor callback.
init([]) ->
Names = [a, b, c],
Processes = [{N,
{g, start, [N]},
permanent, 1000, worker, dynamic} || N <- Names],
io:format("Processes: ~p~n", [Processes]),
{ok, {{one_for_one, 10, 10}, Processes}}.
file: a.erl
-export([ start/2, stop/1 ]).
start(_Type, _StartArgs) ->
case sup:start_link() of
{ok, Pid} -> {ok, Pid};
Other -> {error, Other}
stop(_State) -> ok.
{application, a,
[{description, "test"},
{vsn, "0.1.0"},
{modules, [a, sup, g]},
{registered, [sup]},
{applications, [kernel, stdlib]},
{mod, {a, []}} ]}.
console output:
Erlang R13B02 (erts-5.7.3) [source] [64-bit] [smp:4:4] [rq:4] [async-threads:0] [kernel-poll:false]
Eshell V5.7.3 (abort with ^G)
1> c(a).
2> c(sup).
3> c(g).
4> application:start(a).
Processes: [{a,{g,start,[a]},permanent,1000,worker,dynamic},
g:<0.54.0>: init
g:<0.55.0>: init
g:<0.56.0>: init
5> g:state(a).
g:<0.54.0>: {<0.33.0>,#Ref<>} is asking for the state.
6> supervisor:which_children(sup).
7> g:crash(a).
g:<0.54.0>: {<0.33.0>,#Ref<>} is asking for the state.
g:<0.54.0>: terminate {bad_return_value,{xxxreply,[],[]}}, []
=ERROR REPORT==== 27-Dec-2010::23:32:11 ===
** Generic server a terminating
** Last message in was crash
** When Server state == []
** Reason for termination ==
** {bad_return_value,{xxxreply,[],[]}}
** exception exit: {{bad_return_value,{xxxreply,[],[]}},
in function gen_server:call/2
8> supervisor:which_children(sup).
9> sup:add_child(d).
g:<0.63.0>: init
10> supervisor:which_children(sup).
11> g:state(d).
g:<0.63.0>: {<0.60.0>,#Ref<>} is asking for the state.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.