2015-01-11 56 views
0

我試圖的問題是流程太多,爲什麼?

寫一個環基準。在環中創建N個進程。發送一條消息 M次循環,以便總共發送N * M個消息。 時間多久,這花費的N和M

不同的值

我嘗試看起來像

-module(ring). 
-author("harith"). 

%% API 
-export([message/2, test/0]). 

test() -> 
    Max = erlang:system_info(process_limit), 
    io:format("max processes: ~p~n", [Max]), 
    Time = [timer:tc(ring, message, [N, M]) || N <- lists:seq(10, 500, 10), M <- lists:seq(1000, 100000, 1000)], 
    [io:format("(~p)processes, (~p) messages, (~p) microseconds~n", [N, M, T]) || {T, {N, M}} <- Time]. 

% create ring of N processes and 
% send M messages between them 
message(N, M) when is_integer(N), is_integer(M), N > 0, M > 0 -> 
    Ring = create_ring(N), 
    [Start | T] = Ring, 
    Start ! {T, Ring, 1, M}, 
    {N, M}. 

create_ring(N) -> 
    Processes = [spawn(fun() -> loop() end) || _ <- lists:seq(1, N)], 
    [H | _] = Processes, 
    lists:append(Processes, [H]). 

loop() -> 
    receive 
    {[H | T], _L, CurrentMessage, M} -> 
     % io:format("~p received ~p~n", [self(), CurrentMessage]), 
     H ! {T, _L, CurrentMessage, M}, 
     loop(); 
    {[], Ring, CurrentMessage, M} -> 
     % io:format("~p received ~p with empty list~n", [self(), CurrentMessage]), 
     case CurrentMessage < M of 
     true -> 
      [_ | [Next | T]] = Ring, 
      NewMessage = CurrentMessage + 1, 
      % io:format("sending message ~p to ~p~n", [NewMessage, Next]), 
      Next ! {T, Ring, NewMessage, M}; 
     false -> void %io:format("done sending ~p messages in ~p ring, taking rest now.~n", [M, Ring]) 
     end, 
     loop() 
    end. 

問題?
當我測試此代碼時,出現以下錯誤

1> c(ring). 
{ok,ring} 
2> ring:test(). 
max processes: 262144 

=ERROR REPORT==== 11-Jan-2015::12:59:24 === 
Too many processes 

** exception error: a system limit has been reached 
    in function spawn/3 
     called as spawn(erlang,apply,[#Fun<ring.0.102056517>,[]]) 
    in call from spawn/1 
    in call from ring:'-create_ring/1-lc$^0/1-0-'/1 (ring.erl, line 30) 
    in call from ring:'-create_ring/1-lc$^0/1-0-'/1 (ring.erl, line 30) 
    in call from ring:create_ring/1 (ring.erl, line 30) 
    in call from ring:message/2 (ring.erl, line 24) 
    in call from timer:tc/3 (timer.erl, line 194) 
    in call from ring:'-test/0-lc$^1/1-1-'/3 (ring.erl, line 18) 
*** ERROR: Shell process terminated! *** 

=ERROR REPORT==== 11-Jan-2015::12:59:24 === 
Too many processes 


=ERROR REPORT==== 11-Jan-2015::12:59:24 === 
Error in process <0.26.0> with exit value: {system_limit,[{erlang,spawn_link,[erlang,apply,[#Fun<shell.1.95205691>,[]]],[]},{erlang,spawn_link,1,[]},{shell,get_command,5,[{file,"shell.erl"},{line,298}]},{shell,server_loop,7,[{file,"shell.erl"},{line,229}]}]} 

Eshell V6.2 (abort with ^G) 
*** ERROR: Shell process terminated! *** 

=ERROR REPORT==== 11-Jan-2015::12:59:25 === 
Too many processes 


=ERROR REPORT==== 11-Jan-2015::12:59:25 === 
Error in process <0.32.16> with exit value: {system_limit,[{erlang,spawn_link,[erlang,apply,[#Fun<shell.1.95205691>,[]]],[]},{erlang,spawn_link,1,[]},{shell,get_command,5,[{file,"shell.erl"},{line,298}]},{shell,server_loop,7,[{file,"shell.erl"},{line,229}]}]} 


User switch command 
--> 

嗯,我的數學不是很好,但我還是做了以下的數學,看看是否真的如此。
我想看看我的計劃是多少進程創建,它直接鏈接我什麼N

Time = [timer:tc(ring, message, [N, M]) || N <- lists:seq(10, 500, 10), M <- lists:seq(1000, 100000, 1000)], 

我試圖從

10, 20, 30, ...., 500 

找出整數的和用的區別10

我試圖序列中找出n = number of terms
enter image description here
這竟然是n=50a1 = 10, an = 500, d = 10

的,我發現和作爲
enter image description here

這竟然是(10 + 500)*50/2 ,並按照記錄,可以作爲

創建的最大進程
max processes: 262144 

有人可以幫我理解這個問題嗎?

感謝

回答

3

你與你的身材12750.正確的,但你打電話ring:message/2在這個列表理解:

[timer:tc(ring, message, [N, M]) || N <- lists:seq(10, 500, 10), M <- lists:seq(1000, 100000, 1000)] 

修改,這樣做沒什麼計數它的長度,然後是長度乘以由12750個收率:

1> length([x || N <- lists:seq(10, 500, 10), M <- lists:seq(1000, 100000, 1000)]). 
5000 
2> 12750*5000. 
63750000 

這當然是遠高於262144

默認最大進程限制
+0

笨笨:(謝謝@Steve – daydreamer

相關問題