erlang sys学习

http://www.hoterran.info/erlang-otp-sys-sourcecode

-module(ch4).
-export([start_link/0]).
-export([alloc/0, free/1]).
-export([init/1]).
-export([system_continue/3, system_terminate/4,
         write_debug/3,system_code_change/4]).


start_link() ->
    proc_lib:start_link(ch4, init, [self()]).


alloc() ->
    ch4 ! {self(), alloc},
    receive
        {ch4, Res} ->
            Res
    end.


free(Ch) ->
    ch4 ! {free, Ch},
    ok.


init(Parent) ->
    register(ch4, self()),
    Chs = 10,
    Deb = sys:debug_options([trace,log]),
    proc_lib:init_ack(Parent, {ok, self()}),
    loop(Chs, Parent, Deb).


loop(Chs, Parent, Deb) ->
    receive
        {From, alloc} ->
            Deb2 = sys:handle_debug(Deb, fun  write_debug/3,
                                    ch4, {in, alloc, From}),
          
            From ! {ch4, "SS"},
            Deb3 = sys:handle_debug(Deb2,  fun  write_debug/3,
                                    ch4, {out, {ch4, "SS"}, From}),
            loop("SS", Parent, Deb3);
        {free, Ch} ->
            Deb2 = sys:handle_debug(Deb, fun write_debug/3,
                                    ch4, {in, {free, Ch}}),
            Chs2 = free(Ch),
            loop(Chs2, Parent, Deb2);


        {system, From, Request} ->
            sys:handle_system_msg(Request, From, Parent,
                                  ch4, Deb, Chs)
    end.


system_continue(Parent, Deb, Chs) ->
io:format("system continue~n"),
    loop(Chs, Parent, Deb).


system_terminate(_Reason, _Parent, _Deb, _Chs) ->
    exit(_Reason).


write_debug(Dev, Event, Name) ->

    io:format(Dev, "~p event = ~p~n", [Name, Event]).
system_code_change(_Misc, _Module, _OldSvn,_Extra) ->
io:format("change code: ~p~n",[_OldSvn]).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值