我错了,我想这让我更接近于理解为什么它没有杀死我的真实服务器。
-module(crash).
-behaviour(gen_server).
-export([start_link/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-export([crash/0]).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
init([]) ->
{ok, []}.
crash() ->
gen_server:cast(?MODULE, crash).
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
handle_cast(crash, State) ->
spawn_link(fun() ->
crash_loop(0)
end),
{noreply, State};
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
crash_loop(Counter) ->
case Counter =:= 10 of
true ->
exit(crash_loop);
false ->
ok
end,
timer:sleep(100),
crash_loop(Counter + 1).
...在 shell 中测试:
11> c(crash).
{ok,crash}
12> crash:start_link().
{ok,<0.67.0>}
13> crash:crash().
ok
14> ** exception error: crash_loop
14> whereis(crash).
undefined