У меня проблема с «порождением» в эрланге, кажется, что процессы через некоторое время просто умирают. Вот простой код:
-module(simple).
-export([server/1, client/1, owner/1, spawn_n/2, start/1, main/1]).
server(State) ->
receive
{request,Return_PID} ->
io:format("SERVER ~w: Client request recieved from ~w~n", [self(),Return_PID]),
NewState = State + 1,
Return_PID ! {hit_count,NewState},
server(NewState);
{server_owner,Owner_PID} ->
io:format("SERVER ~w: Owner request recieved from ~w~n", [self(), Owner_PID]),
Owner_PID ! {hit_count,State},
server(State);
reset ->
io:format("SERVER ~w: State reset to zero.~n", [self()]),
server(0)
end.
client(Server_Address) ->
Server_Address ! {request, self()},
receive
{hit_count,Number} ->
io:format("CLIENT ~w: Hit count was ~w~n", [self(),Number])
end.
owner(Server_PID) ->
timer:sleep(random:uniform(100)),
Server_PID ! {server_owner,self()},
receive
{hit_count,Number} when Number > 5 ->
io:format("OWNER: Hit count is ~w , reseting counter. ~n", [Number]),
Server_PID ! reset,
owner(Server_PID);
{hit_count,Number} when Number < 5 ->
io:format("OWNER ~w: Hit count was ~w~n", [self(), Number]),
owner(Server_PID)
end.
spawn_n(N, Server_PID) ->
if
N>0 ->
spawn(simple,client,[Server_PID]),
timer:sleep(random:uniform(100)),
spawn_n(N-1,Server_PID);
N==0 ->
io:format("Last client spawned. ~n")
end.
start(N) ->
Server_PID = spawn(simple,server,[0]),
spawn(simple,owner,[Server_PID]),
spawn(simple,spawn_n,[N,Server_PID]).
main([Arg]) ->
N = list_to_integer(atom_to_list(Arg)),
start(N),
init:stop().
Вот пример, который я получаю при запуске:
erl -noshell -s simple main 20
SERVER <0.28.0>: Client request recieved from <0.31.0>
CLIENT <0.31.0>: Hit count was 1
SERVER <0.28.0>: Owner request recieved from <0.29.0>
SERVER <0.28.0>: Client request recieved from <0.32.0>
OWNER <0.29.0>: Hit count was 1
CLIENT <0.32.0>: Hit count was 2
SERVER <0.28.0>: Owner request recieved from <0.29.0>
SERVER <0.28.0>: Client request recieved from <0.33.0>
OWNER <0.29.0>: Hit count was 2
CLIENT <0.33.0>: Hit count was 3
SERVER <0.28.0>: Owner request recieved from <0.29.0>
SERVER <0.28.0>: Client request recieved from <0.34.0>
OWNER <0.29.0>: Hit count was 3
CLIENT <0.34.0>: Hit count was 4
SERVER <0.28.0>: Owner request recieved from <0.29.0>
SERVER <0.28.0>: Client request recieved from <0.35.0>
OWNER <0.29.0>: Hit count was 4
CLIENT <0.35.0>: Hit count was 5
SERVER <0.28.0>: Owner request recieved from <0.29.0>
SERVER <0.28.0>: Client request recieved from <0.36.0>
CLIENT <0.36.0>: Hit count was 6
SERVER <0.28.0>: Client request recieved from <0.37.0>
CLIENT <0.37.0>: Hit count was 7
SERVER <0.28.0>: Client request recieved from <0.38.0>
CLIENT <0.38.0>: Hit count was 8
SERVER <0.28.0>: Client request recieved from <0.39.0>
CLIENT <0.39.0>: Hit count was 9
SERVER <0.28.0>: Client request recieved from <0.40.0>
CLIENT <0.40.0>: Hit count was 10
SERVER <0.28.0>: Client request recieved from <0.41.0>
CLIENT <0.41.0>: Hit count was 11
SERVER <0.28.0>: Client request recieved from <0.42.0>
CLIENT <0.42.0>: Hit count was 12
SERVER <0.28.0>: Client request recieved from <0.43.0>
CLIENT <0.43.0>: Hit count was 13
SERVER <0.28.0>: Client request recieved from <0.44.0>
CLIENT <0.44.0>: Hit count was 14
SERVER <0.28.0>: Client request recieved from <0.45.0>
CLIENT <0.45.0>: Hit count was 15
SERVER <0.28.0>: Client request recieved from <0.46.0>
CLIENT <0.46.0>: Hit count was 16
SERVER <0.28.0>: Client request recieved from <0.47.0>
CLIENT <0.47.0>: Hit count was 17
SERVER <0.28.0>: Client request recieved from <0.48.0>
CLIENT <0.48.0>: Hit count was 18
{error_logger,{{2011,6,27},{12,57,8}},"~s~n",["Error in process <0.28.0> with ex
it value: {terminated,[{io,format,[<0.22.0>,\"SERVER ~w: Client request recieved
from ~w~n\",[<0.28.0>,<0.49.0>]]},{simple,server,1}]}\n"]}
Я не понимаю. Процессы просто умирают или как? Это не должно прекращаться!
Я использую Windows 7, если это может быть связано с Windows.
Спасибо
РЕДАКТИРОВАТЬ: вот что происходит при выполнении приложения: запуск (sasl). раньше:
C:\Program Files\erl5.8.4\bin>erl
Eshell V5.8.4 (abort with ^G)
1> application:start(sasl).
ok
=PROGRESS REPORT==== 27-Jun-2011::16:03:55 ===
supervisor: {local,sasl_safe_sup}
started: [{pid,<0.37.0>},
{name,alarm_handler},
{mfargs,{alarm_handler,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
2>
=PROGRESS REPORT==== 27-Jun-2011::16:03:55 ===
supervisor: {local,sasl_safe_sup}
started: [{pid,<0.38.0>},
{name,overload},
{mfargs,{overload,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
2>
=PROGRESS REPORT==== 27-Jun-2011::16:03:55 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.36.0>},
{name,sasl_safe_sup},
{mfargs,
{supervisor,start_link,
[{local,sasl_safe_sup},sasl,safe
{restart_type,permanent},
{shutdown,infinity},
{child_type,supervisor}]
2>
=PROGRESS REPORT==== 27-Jun-2011::16:03:55 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.39.0>},
{name,release_handler},
{mfargs,{release_handler,start_link,[]}}
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
2>
=PROGRESS REPORT==== 27-Jun-2011::16:03:55 ===
application: sasl
started_at: nonode@nohost
2> simple:main(['20']).
ok
SERVER <0.42.0>: Client request recieved from <0.45.0>
3> CLIENT <0.45.0>: Hit count was 1
3> SERVER <0.42.0>: Owner request recieved from <0.43.0>
3> SERVER <0.42.0>: Client request recieved from <0.47.0>
3> OWNER <0.43.0>: Hit count was 1
3> CLIENT <0.47.0>: Hit count was 2
3> SERVER <0.42.0>: Owner request recieved from <0.43.0>
3> SERVER <0.42.0>: Client request recieved from <0.48.0>
3> OWNER <0.43.0>: Hit count was 2
3> CLIENT <0.48.0>: Hit count was 3
3> SERVER <0.42.0>: Owner request recieved from <0.43.0>
3> SERVER <0.42.0>: Client request recieved from <0.49.0>
3> OWNER <0.43.0>: Hit count was 3
3> CLIENT <0.49.0>: Hit count was 4
3> SERVER <0.42.0>: Owner request recieved from <0.43.0>
3> SERVER <0.42.0>: Client request recieved from <0.50.0>
3> OWNER <0.43.0>: Hit count was 4
3> CLIENT <0.50.0>: Hit count was 5
3> SERVER <0.42.0>: Owner request recieved from <0.43.0>
3> SERVER <0.42.0>: Client request recieved from <0.51.0>
3> CLIENT <0.51.0>: Hit count was 6
3> SERVER <0.42.0>: Client request recieved from <0.52.0>
3> CLIENT <0.52.0>: Hit count was 7
3> SERVER <0.42.0>: Client request recieved from <0.53.0>
3> CLIENT <0.53.0>: Hit count was 8
3> SERVER <0.42.0>: Client request recieved from <0.54.0>
3> CLIENT <0.54.0>: Hit count was 9
3> SERVER <0.42.0>: Client request recieved from <0.55.0>
3> CLIENT <0.55.0>: Hit count was 10
3> SERVER <0.42.0>: Client request recieved from <0.56.0>
3> CLIENT <0.56.0>: Hit count was 11
3> SERVER <0.42.0>: Client request recieved from <0.57.0>
3> CLIENT <0.57.0>: Hit count was 12
3> SERVER <0.42.0>: Client request recieved from <0.58.0>
3> CLIENT <0.58.0>: Hit count was 13
3> SERVER <0.42.0>: Client request recieved from <0.59.0>
3> CLIENT <0.59.0>: Hit count was 14
3> SERVER <0.42.0>: Client request recieved from <0.60.0>
3> CLIENT <0.60.0>: Hit count was 15
3> SERVER <0.42.0>: Client request recieved from <0.61.0>
3> CLIENT <0.61.0>: Hit count was 16
3> SERVER <0.42.0>: Client request recieved from <0.62.0>
3> CLIENT <0.62.0>: Hit count was 17
3> SERVER <0.42.0>: Client request recieved from <0.63.0>
3> CLIENT <0.63.0>: Hit count was 18
3> {error_logger,{{2011,6,27},{16,3,58}},"~s~n",["Error in proc
exit value: {terminated,[{io,format,[<0.23.0>,\"SERVER ~w: Cli
ved from ~w~n\",[<0.42.0>,<0.64.0>]]},{simple,server,1}]}\n"]}