- 论坛徽章:
- 27
|
本帖最后由 shijiang1130 于 2014-12-26 23:24 编辑
- -module(otp).
- -behaviour(gen_server).
- -define(SERVER, ?MODULE).
- -define(TABLE_ID,?MODULE).
- %% ------------------------------------------------------------------
- %% API Function Exports
- %% ------------------------------------------------------------------
- -export([start_link/0]).
- %% ------------------------------------------------------------------
- %% gen_server Function Exports
- %% ------------------------------------------------------------------
- -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
- terminate/2, code_change/3]).
- %% ------------------------------------------------------------------
- %% API Function Definitions
- %% ------------------------------------------------------------------
- start_link() ->
- gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
- %% ------------------------------------------------------------------
- %% gen_server Function Definitions
- %% ------------------------------------------------------------------
- init(Args) ->
- process_flag(trap_exit,true),
- ets:new(?TABLE_ID,[public,duplicate_bag,named_table]),
- {ok, Args}.
- handle_call(_Request, _From, State) ->
- {reply, ok, State}.
- handle_cast(_Msg, State) ->
- io:format("cast"),
- Pid=spawn(fun()->os:cmd("ping -n 3 127.0.0.1") end),
- ets:insert(?TABLE_ID,{pid,Pid}),
- erlang:monitor(process,Pid),
- {noreply, State}.
- handle_info(_Info, State) ->
- io:format("~p~n",[_Info]),
- {noreply, State}.
- terminate(_Reason, _State) ->
- ok.
- code_change(_OldVsn, State, _Extra) ->
- {ok, State}.
- %% ------------------------------------------------------------------
- %% Internal Function Definitions
- %% ------------------------------------------------------------------
复制代码 |
|