我如何计算该过程完成请求的时间

发布于 2025-02-13 07:57:32 字数 3588 浏览 1 评论 0原文

首先,感谢您查看我的问题。创建一个管理可用频率列表的服务器(例如流程[1,2,3,4,5,6,7,8,9,10]。客户端进程应能够请求任何可用频率并发布。当客户端发布频率时,服务器将根据客户端使用的频率计算数量(5 rs/sec),

即使您可以 将帐单发送给客户端。 T解决。老实说,我非常感谢您

-module(b).

-compile([export_all, nowarn_export_all]).

run() ->
    file:delete("example.dets"),
    case dets:info(example, owner) of
        undefined ->
            dets:open_file(example, [{file, "example.dets"}]);
        _ ->
            skip
    end,
    spawn(fun() -> init() end),
    timer:sleep(100),
    Client = self(),

    example_server ! {allocate, Client, aaa, 5},
    example_server ! {allocate, Client, bbb, 5},
    example_server ! {allocate, Client, aaa, 5},
    timer:sleep(1000),
    example_server ! {release, Client, aaa, 5},
    example_server ! {release, Client, aaa, 5},
    timer:sleep(100),
    io:format("~p~n", [receive_server_msg([])]),

    example_server ! {allocate, Client, aaa, 5},
    timer:sleep(100),
    exit(whereis(example_server), kill),
    spawn(fun() -> init() end),
    timer:sleep(100),
    example_server ! {allocate, Client, bbb, 5},
    example_server ! {allocate, Client, aaa, 5},
    timer:sleep(1000),
    example_server ! {release, Client, aaa, 5},
    example_server ! {release, Client, aaa, 5},
    timer:sleep(100),
    io:format("~p~n", [receive_server_msg([])]),
    example_server ! {stop},

    dets:close(example).

receive_server_msg(List) ->
    receive
        Reply ->
            receive_server_msg([Reply|List])
        after 0 ->
            lists:reverse(List)
    end.
            

init() ->
    erlang:register(example_server, self()),
    State = other_data_you_need,
    manager(State).

manager(State) ->
    receive
        {stop} -> ok;
        {allocate, Requestor, Name, Frequency} ->
            case check_allocate_request_and_modify_state(Name, Frequency, State) of
                {ok, State1} ->
                    Requestor ! {grant, Frequency},
                    manager(State1);
                {error, Reason} ->
                    Requestor ! {error, Reason},
                    manager(State)
            end;
        {release, Requestor, Name, Frequency} ->
            case check_release_request_and_modify_state(Name, Frequency, State) of
                {ok, Cost, State1} ->
                    Requestor ! {bill, Cost},
                    manager(State1);
                {error, Reason} ->
                    Requestor ! {error, Reason},
                    manager(State)
            end;
        UnknownMsg ->
            logger:error("unknown message: ~p", [UnknownMsg]),
            manager(State)
    end.

check_allocate_request_and_modify_state(Name, Frequency, State) ->
    case lists:member(Frequency, [1,2,3,4,5,6,7,8,9,10]) of
        true ->
            case dets:lookup(example, Frequency) of
                [] ->
                    dets:insert(example, {Frequency, Name, erlang:system_time(second)}),
                    {ok, State};
                [{_, Name, _}] ->
                    {error, using};
                _ ->
                    {error, other_using}
            end;
        _ ->
            {error, bad_request}
    end.

check_release_request_and_modify_state(Name, Frequency, State) ->
    case dets:lookup(example, Frequency) of
        [{_, Name, StartTime}] ->
            dets:delete(example, Frequency),
            Cost = (erlang:system_time(second) - StartTime),% * Price,
            {ok, Cost, State};
        _ ->
            {error, not_using}
    end.

First of all thank you for looking at my question. Create a server that manages a list of available frequencies(say PROCESS [1,2,3,4,5,6,7,8,9,10]. The client process should be able to request any available frequency and release it. When the client releases the frequency, the server would calculate the amount (5 Rs/Sec) based upon the time the frequency was used by the client and would send the bill to the client.

I really do appreciate your help even if you can’t solve. I have been trying this for 1 whole month and I’m honestly so down and hopeless. Thank you so much.

-module(b).

-compile([export_all, nowarn_export_all]).

run() ->
    file:delete("example.dets"),
    case dets:info(example, owner) of
        undefined ->
            dets:open_file(example, [{file, "example.dets"}]);
        _ ->
            skip
    end,
    spawn(fun() -> init() end),
    timer:sleep(100),
    Client = self(),

    example_server ! {allocate, Client, aaa, 5},
    example_server ! {allocate, Client, bbb, 5},
    example_server ! {allocate, Client, aaa, 5},
    timer:sleep(1000),
    example_server ! {release, Client, aaa, 5},
    example_server ! {release, Client, aaa, 5},
    timer:sleep(100),
    io:format("~p~n", [receive_server_msg([])]),

    example_server ! {allocate, Client, aaa, 5},
    timer:sleep(100),
    exit(whereis(example_server), kill),
    spawn(fun() -> init() end),
    timer:sleep(100),
    example_server ! {allocate, Client, bbb, 5},
    example_server ! {allocate, Client, aaa, 5},
    timer:sleep(1000),
    example_server ! {release, Client, aaa, 5},
    example_server ! {release, Client, aaa, 5},
    timer:sleep(100),
    io:format("~p~n", [receive_server_msg([])]),
    example_server ! {stop},

    dets:close(example).

receive_server_msg(List) ->
    receive
        Reply ->
            receive_server_msg([Reply|List])
        after 0 ->
            lists:reverse(List)
    end.
            

init() ->
    erlang:register(example_server, self()),
    State = other_data_you_need,
    manager(State).

manager(State) ->
    receive
        {stop} -> ok;
        {allocate, Requestor, Name, Frequency} ->
            case check_allocate_request_and_modify_state(Name, Frequency, State) of
                {ok, State1} ->
                    Requestor ! {grant, Frequency},
                    manager(State1);
                {error, Reason} ->
                    Requestor ! {error, Reason},
                    manager(State)
            end;
        {release, Requestor, Name, Frequency} ->
            case check_release_request_and_modify_state(Name, Frequency, State) of
                {ok, Cost, State1} ->
                    Requestor ! {bill, Cost},
                    manager(State1);
                {error, Reason} ->
                    Requestor ! {error, Reason},
                    manager(State)
            end;
        UnknownMsg ->
            logger:error("unknown message: ~p", [UnknownMsg]),
            manager(State)
    end.

check_allocate_request_and_modify_state(Name, Frequency, State) ->
    case lists:member(Frequency, [1,2,3,4,5,6,7,8,9,10]) of
        true ->
            case dets:lookup(example, Frequency) of
                [] ->
                    dets:insert(example, {Frequency, Name, erlang:system_time(second)}),
                    {ok, State};
                [{_, Name, _}] ->
                    {error, using};
                _ ->
                    {error, other_using}
            end;
        _ ->
            {error, bad_request}
    end.

check_release_request_and_modify_state(Name, Frequency, State) ->
    case dets:lookup(example, Frequency) of
        [{_, Name, StartTime}] ->
            dets:delete(example, Frequency),
            Cost = (erlang:system_time(second) - StartTime),% * Price,
            {ok, Cost, State};
        _ ->
            {error, not_using}
    end.

If possible could you help me correct this and help me with the right format or right answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文