[erlang-questions] Question on building a Parallel Server

Joe Armstrong erlang@REDACTED
Tue Jan 27 19:48:10 CET 2015


On Tue, Jan 27, 2015 at 2:43 PM, Harit Himanshu
<harit.subscriptions@REDACTED> wrote:
> Hello
>
> As I learn from Programming Erlang, the following is a way to create a
> parallel server
>
> start_parallel_server() ->
> {ok, Listen} = gen_tcp:listen(...), spawn(fun() -> par_connect(Listen) end).
>
> par_connect(Listen) ->
> {ok, Socket} = gen_tcp:accept(Listen), spawn(fun() -> par_connect(Listen)
> end), loop(Socket).
>
>
> I wanted to dissect this code to understand what's going on.
>
> Questions
>
> When we call start_parallel_server()  on a single machine, a separate
> process is started (which is controlling process?) executing par_connect,
> correct?
> When gen_tcp:accept accepts a new connection, immediately a new process is
> created executing par_connect(Listen). This makes sure that we can accept
> more requests from the client. Now who is the controlling process for this
> new process? is the same as Listen?

no

Listen is owned by the process that called gen_tcp:listen
Socket is owned by the process that called gen_tcp:accept

So you end up with one Listen process and N different values of
Socket (one per spawned process). Each parallel process has it's own socket

/Joe


>
>
> Thanks
> + Harit Himanshu
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list