我正在尝试编写一个环形基准测试,我有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]}]}
循环/ 2导出,所以我不明白为什么会出现这个错误。
答案 0 :(得分:1)
你的模块是ringmets,而不是ring_m