流程创建问题
我正在建设一个停车场,有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发布的示例在
spawn/3
调用中使用了错误的模块:如果您将其更改为:(
始终使用
?MODULE
,这是一个计算当前模块名称的宏,在做这样的事情时,因为它会避免使用错误的模块而不是预期的很多错误。该错误来自未注册
parkingLoop
进程。您正在尝试使用parking 向其发送消息! ...
但没有进程名为parking
。将第 33 行更改为:(即使在这里,您也可以使用
?MODULE
宏来避免将来出现问题:?MODULE ! ...
和register(?MODULE , ...)
因为您只有一个具有该名称的进程)此外,第 38 行的
if
语句错过了一个失败子句。让它看起来像这样来处理Slots
not 等于 0 的情况:(ok
表达式将不起作用,因为返回值未使用if
语句)The example you posted uses the wrong module in the
spawn/3
call:It should work better (or at least give a more up to date error) if you change this to:
(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 usingparking ! ...
but no process is namedparking
. Change line 33 to:(Even here you can use the
?MODULE
macro to avoid problems in the future:?MODULE ! ...
andregister(?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 whereSlots
is not equal to zero:(The
ok
expression will have no effect since the return value of theif
statement is not used)