流程创建问题

发布于 2024-10-29 05:53:55 字数 1712 浏览 1 评论 0原文

我正在建设一个停车场,有 2 个入口门和 1 个可以离开公园的大门。对我来说,一切看起来都很好,但我收到了类似的错误

Error in process <0.84.0> with exit value: {badarg,[{parking,car,2},{random,uniform,0}]}

我的代码是:

-module (parking2).
-export ([start/3]).
-export ([car/2, parkingLoop/1]).

carsInit(0, _Iterations) ->
    ok;
carsInit(Number, Iterations) ->
    spawn(parking, car, [Number, Iterations]),
    carsInit(Number - 1, Iterations).

car(_ID, 0) ->
    ok;
car(ID, Iterations) ->
    Gate = random:uniform(2),
    parking ! {enter, self()},
    receive
        error ->
            io:format("Car ~B ncanot enter - there is no free place.~n", [ID]),
            Time = random:uniform(1000),
            timer:sleep(Time),
            car(ID, Iterations);
        ok ->
            io:format("Car ~B entered through the ~B gate~n", [ID, Gate])
    end,
    StopTime = random:uniform(500) + 500,
    timer:sleep(StopTime),
        parking ! {leave, self(), ID},
        FreerideTime = random:uniform(1000) + 500,
    timer:sleep(FreerideTime),
    car(ID, Iterations - 1).

parkingInit(Slots) ->
    spawn(parking, parkingLoop, [Slots]).

parkingLoop(Slots) ->
    receive
        {enter, Pid} ->
            if Slots =:= 0 ->
                Pid ! error
            end,
            Pid ! ok,
            parkingLoop(Slots - 1);
        {leave, Pid, ID} ->
            io:format("Car ~B left the car park.", [ID]),
            parkingLoop(Slots + 1);
        stop ->
            ok
    end.    


start(Cars, Slots, Iterations) ->
    parkingInit(Slots),
    carsInit(Cars, Iterations).

有人可以帮助我吗?我学习了 Erlang 几天了,不知道这里出了什么问题。

提前致谢, 拉德克

I am implementing a car park with 2 entry gates and 1 by which you can leave the park. For me, everything looks fine but I am getting errors like

Error in process <0.84.0> with exit value: {badarg,[{parking,car,2},{random,uniform,0}]}

My code is:

-module (parking2).
-export ([start/3]).
-export ([car/2, parkingLoop/1]).

carsInit(0, _Iterations) ->
    ok;
carsInit(Number, Iterations) ->
    spawn(parking, car, [Number, Iterations]),
    carsInit(Number - 1, Iterations).

car(_ID, 0) ->
    ok;
car(ID, Iterations) ->
    Gate = random:uniform(2),
    parking ! {enter, self()},
    receive
        error ->
            io:format("Car ~B ncanot enter - there is no free place.~n", [ID]),
            Time = random:uniform(1000),
            timer:sleep(Time),
            car(ID, Iterations);
        ok ->
            io:format("Car ~B entered through the ~B gate~n", [ID, Gate])
    end,
    StopTime = random:uniform(500) + 500,
    timer:sleep(StopTime),
        parking ! {leave, self(), ID},
        FreerideTime = random:uniform(1000) + 500,
    timer:sleep(FreerideTime),
    car(ID, Iterations - 1).

parkingInit(Slots) ->
    spawn(parking, parkingLoop, [Slots]).

parkingLoop(Slots) ->
    receive
        {enter, Pid} ->
            if Slots =:= 0 ->
                Pid ! error
            end,
            Pid ! ok,
            parkingLoop(Slots - 1);
        {leave, Pid, ID} ->
            io:format("Car ~B left the car park.", [ID]),
            parkingLoop(Slots + 1);
        stop ->
            ok
    end.    


start(Cars, Slots, Iterations) ->
    parkingInit(Slots),
    carsInit(Cars, Iterations).

May anybody help me ? I learn Erlang for a couple of days and have no idea, what's wrong here.

Thanks in advance,
Radek

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为你鎻心 2024-11-05 05:53:55

您发布的示例在 spawn/3 调用中使用了错误的模块:

spawn(parking, parkingLoop, [Slots]).

如果您将其更改为:(

spawn(?MODULE, parkingLoop, [Slots]).

始终使用 ?MODULE,这是一个计算当前模块名称的宏,在做这样的事情时,因为它会避免使用错误的模块而不是预期的很多错误。

该错误来自未注册 parkingLoop 进程。您正在尝试使用 parking 向其发送消息! ... 但没有进程名为 parking。将第 33 行更改为:(

register(parking, spawn(parking2, parkingLoop, [Slots])).

即使在这里,您也可以使用 ?MODULE 宏来避免将来出现问题:?MODULE ! ...register(?MODULE , ...) 因为您只有一个具有该名称的进程)

此外,第 38 行的 if 语句错过了一个失败子句。让它看起来像这样来处理 Slots not 等于 0 的情况:(

if 
    Slots =:= 0 ->Pid ! error;
    true -> ok
end,

ok 表达式将不起作用,因为返回值未使用 if 语句)

The example you posted uses the wrong module in the spawn/3 call:

spawn(parking, parkingLoop, [Slots]).

It should work better (or at least give a more up to date error) if you change this to:

spawn(?MODULE, parkingLoop, [Slots]).

(Always use ?MODULE, which is a macro that evaluates to the current module name, when doing such things since it will avoid a lot of mistakes using the wrong module than intended).

The bug comes from not registering the parkingLoop process. You're trying to send a message to it using parking ! ... but no process is named parking. Change line 33 to:

register(parking, spawn(parking2, parkingLoop, [Slots])).

(Even here you can use the ?MODULE macro to avoid problems in the future: ?MODULE ! ... and register(?MODULE, ...) since you only have one process with this name)

Also, your if statement on line 38 misses a fall-through clause. Make it look like this to handle the case where Slots is not equal to zero:

if 
    Slots =:= 0 ->Pid ! error;
    true -> ok
end,

(The ok expression will have no effect since the return value of the if statement is not used)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文