erlang:sup监控进程实例

%% @author Administrator
%% @doc @todo Add description to users_sup.


-module(users_sup).
-behavior(supervisor).
-compile(export_all).
%% ====================================================================
%% API functions
%% ====================================================================
-export([]).

start() ->
    register(users_sup, spawn(?MODULE, start_monitor, [])).

%% 创建监控子进程,注册为monitor,并运行 init/1
start_monitor() ->
    io:format("sup started: ~p~n", [self()]),
    {ok, Pid} = supervisor:start_link({local, monitor}, ?MODULE, []),
    wait().

%% monitor进程运行init/1,启动所有被监控的子进程,并监控这些进程
%% 运行 users:start
%% 监控规范:被监控进程退出后,重启之,重启次数100,重启间隔10ms
init(FileName) ->
    io:format("enter init, ~s, ~p~n", [FileName, self()]),
    ChildSpec = {users_s, {users_s, start, []}, permanent, 100000, worker, [users_s]},
    {ok, {{one_for_one, 100, 10}, [ChildSpec]}}.

%% sup进程等待退出消息
%%users_sup进程退出,从而导致monitor及所有monitor的子进程都退出
wait() ->
    receive
        {quit} ->
            io:format("recv quit msg~n");
        {'EXIT', Pid, Reason} ->
            io:format("Child ~p exit~n", [Pid]),
            wait()
    end.

stop() ->
    users_sup ! {quit}.    

%% ====================================================================
%% Internal functions
%% ====================================================================
%% @author Administrator
%% @doc @todo Add description to db_s.


-module(users_s).
%%-export([start/0,stop/0,write/2,delete/1,read/1,match/1,all/0]).
%% ====================================================================
%% API functions
%% ====================================================================
-compile(export_all).
-behaviour(gen_server).
-include("user.hrl").
%% ====================================================================
%% External functions
%% ====================================================================
start() ->
    start_link().

stop() ->
    gen_server:cast(?MODULE, stop).

add(UserID, Username, UserPd) ->
    gen_server:call(?MODULE, {add,UserID, Username, UserPd}).

lookById(UserID) ->
    gen_server:call(?MODULE, {lookById,UserID}).

match(Pattern) ->
    gen_server:call(?MODULE, {match,Pattern}).

delete_tab() ->
    gen_server:call(?MODULE, delete_tab).

all() ->
    gen_server:call(?MODULE, all).

test() ->
    gen_server:call(?MODULE, test).

%% ====================================================================
%% Server functions
%% ====================================================================


start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

init([]) ->
    users:init(),
    {ok,[]}.

handle_cast(stop,LoopData) ->
    users:delete(),
    {stop,normal,LoopData}.

handle_call({add,UserID, Username, UserPd},_From,LoopData) ->
    Reply = users:add_user(UserID, Username, UserPd),
    {reply,Reply,LoopData};

handle_call({lookById,UserID},_From,LoopData) ->
    Reply=users:look_user_byID(UserID),
    {reply,Reply,LoopData};

handle_call({match,Pattern},_From,LoopData) ->
    Reply=users:look_user_byMatch(Pattern),
    {reply,Reply,LoopData};

handle_call(delete_tab,_From,LoopData) ->
    Reply = users:delete_tab(),
    {reply,Reply,LoopData};

handle_call(test,_From,LoopData) ->
    Reply = users:test(),
    {reply,Reply,LoopData};

handle_call(all,_From,LoopData) ->
    Reply = users:check_all(),
    {reply,Reply,LoopData}.

terminate(_Reason, _State) ->
    ok.
%% @author Administrator
%% @doc @todo Add description to usr.


-module(users).
-compile(export_all).
-include("user.hrl").
-define(TIMEOUT,30000).

%% ====================================================================
%% API functions
%% ====================================================================
init() ->
    ets:new(userTab,[{keypos,#user.id},named_table,public]).

add_user(UserID,Username,UserPd) ->
    ets:insert(userTab,#user{id = UserID,name = Username,pd = UserPd}).

look_user_byID(UserID) ->
    ets:lookup(userTab, UserID).

check_all() ->
    check_first().

check_first() ->
    case ets:first(userTab)of
        '$end_of_table' ->
            ok;
        First ->
            io:format("~w~n",ets:lookup(userTab, First)),
            check_next(First)
    end.
    
check_next(First) ->
    case ets:next(userTab, First) of
        '$end_of_table' ->
            ok;
        Next ->
            io:format("~w~n",ets:lookup(userTab, Next)),
            check_next(Next)
    end.
    
delete_tab() ->
    ets:delete(userTab).
%% ====================================================================
%% name:match_by_object
%% input:#user{name="yangxin",_='_'}
%% ====================================================================
look_user_byMatch(Pattern) ->
    ets:match_object(userTab, Pattern). 

test() ->
    init(),
    add_user(1,yangxin,123456),
    add_user(2,yangxin,12345678),
    %%look_user_byMatch(#user{name = "yangxin",_='_'}),
    check_all().

%% ====================================================================
%% Internal functions
%% ====================================================================

 

转载于:https://www.cnblogs.com/study-development/archive/2013/03/27/2984029.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值