简体   繁体   English

Erlang产生问题

[英]Erlang spawn problems

I am having problem with 'spawn' in erlang, it seems processes just die after awhile. 我在erlang中遇到'spawn'问题,似乎进程只是在一段时间后死亡。 Here's the simple code: 这是简单的代码:

-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().

Here's an example I get when running it: 这是我运行时得到的一个例子:

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"]}

I don't get it. 我不明白。 The processes just die or something? 这些过程会死吗? It shouldnt terminate! 它不应该终止! I am running on windows 7 if it might be something windows-related. 如果它可能与Windows相关,我在Windows 7上运行。

Thanks 谢谢

EDIT: heres what happens by doing application:start(sasl). 编辑:继续执行应用程序会发生什么:启动(sasl)。 before: 之前:

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"]}

The other answer is correct, but it doesn't explain why. 另一个答案是正确的,但它没有解释原因。

It's a tricky question, and the answer is (sort of) in your log output: 这是一个棘手的问题,答案是(在你的日志输出中):

{error_logger,{{2011,6,27},{12,57,8}},"~s~n",["Error in process <0.28.0> with exit 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"]}

The first clue is that io:format/3 exits with 'terminated' and some data. 第一个线索是io:format / 3以“已终止”和一些数据退出。

Looking at the documentation and searching for terminated gives ... nothing. 查看文档并搜索已终止的内容...没有任何内容。 Looking inside io.erl gives us the answer though. 看看里面的io.erl给了我们答案。

Line 456 and 462 of io.erl returns {error, terminated} when the io device is down. 当io设备关闭时,io.erl的第456行和第462行返回{error,terminated}。 So now we know that io:format can potentially exit with status terminated. 所以现在我们知道io:format 可能会在状态终止时退出。 Looking further we can see that this return value turns into the above error message on line 74 (with context): 进一步看,我们可以看到这个返回值在第74行(带上下文)变成上面的错误消息:

case request(Io, Request) of
{error, Reason} ->
    [_Name | Args] = tuple_to_list(to_tuple(Request)),
    {'EXIT',{undef,[_Current|Mfas]}} = (catch erlang:error(undef)),
        MFA = {io, Func, [Io | Args]},
        exit({conv_reason(Func, Reason),[MFA|Mfas]});

If you follow the call path for request/2 you will find that one possible branch is at the {error, terminated} mentioned earlier. 如果您按照request / 2的调用路径进行操作,您会发现一个可能的分支位于前面提到的{error,terminated}。

So, long story short, standard out is down . 所以,长话短说, 标准出局是下降

Most likely since you killed your system with init:stop() . 很可能是因为你用init:stop()杀了你的系统。

You are calling init:stop() immediately after your spawns. 您在生成后立即调用init:stop() How are you going to make sure all your processes are properly finished? 您如何确保所有流程都已正确完成? You probably need to block on receive in main and notify main process when others are done. 您可能需要在main阻止接收,并在其他人完成时通知主进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM