我正在尝试编写一个环形基准测试,其中我有 N 个进程,我通过它们发送消息 M 次。我想将进程的 pid 存储在 ETS 表中。
-module(ringmets).
-compile(export_all).
start_m(N, M, Msg) ->
ets:new(pid, [ordered_set, named_table]),
List = start_proc(N, M),
%tv:start(),
[ets:insert(pid, {Pid, X}) || {Pid,X} <- lists:zip(lists:seq(1,N), List)],
hd(List) ! {Msg, 1}.
start_proc(0, _M) ->
[];
start_proc(N, M) ->
[spawn(ring_m, loop, [M, N]) | start_proc(N-1, M)].
loop(-1, _N) ->
ok;
loop(M, N) ->
receive
{Msg, CurrPid} ->
case CurrPid == N of
true -> Next = 1;
false -> Next = CurrPid + 1
end,
LU = ets:lookup(pid, Next),
NextPid = element(2, hd(LU)),
NextPid ! {Msg, Next},
loop(M-1, N)
end.
叫:
2> ringmets:start_m(5,5,ok).
{ok,1}
3>
=ERROR REPORT==== 22-Nov-2012::19:51:43 ===
Error in process <0.38.0> with exit value: {undef,[{ring_m,loop,[5,5]}]}
=ERROR REPORT==== 22-Nov-2012::19:51:43 ===
Error in process <0.42.0> with exit value: {undef,[{ring_m,loop,[5,1]}]}
=ERROR REPORT==== 22-Nov-2012::19:51:43 ===
Error in process <0.39.0> with exit value: {undef,[{ring_m,loop,[5,4]}]}
=ERROR REPORT==== 22-Nov-2012::19:51:43 ===
Error in process <0.41.0> with exit value: {undef,[{ring_m,loop,[5,2]}]}
=ERROR REPORT==== 22-Nov-2012::19:51:43 ===
Error in process <0.40.0> with exit value: {undef,[{ring_m,loop,[5,3]}]}
loop/2 已导出,所以我不明白为什么会出现此错误。
你的模块是环形的,而不是ring_m