[erlang-questions] A Question About Processes
Mon Mar 21 23:18:54 CET 2016
On Mon, Mar 21, 2016 at 11:35:04PM +0200, Dimitar Haralanov wrote:
> Hello everyone!
> I am new to Erlang, but I’ve been really enjoying almost every bit of it. I stumbled upon
> something I am not exactly sure about. So far, my understanding is that processes are
> created by spawn()-ing them, however I think this is not the complete truth. I have the
> following contrived example:
> bar() ->
> io:format("Hello from ~p~n", [self()]),
> spawn(fun loop/0).
> loop() ->
> Message ->
> io:format("PID ~p received ~p~n", [self(), Message]),
> and after running the code we get:
> 1> Pid = foo:bar().
> Hello from <0.33.0>
> 2> Pid ! hello.
> PID <0.35.0> received hello
> My question is - how is the process with PID <0.33.0> spawned? I’ve only read about
> using the erlang:spawn() function call, but I guess there are other implicit ways of starting
> new processes. Any further clarification would be really helpful. Thank you!
When you call foo:bar(), that's running in the context of the
process that is running the shell. When you run it, it prints the pid
of the shell (0.33.0), and then spawns a process running the loop
function. That then prints its own pid (0.35.0) and the hello message.
I think the thing you're missing is that the erlang shell is a
process in its own right, and that's the first pid you see in your
Hugo Mills | If the first-ever performance is the première, is
hugo@REDACTED carfax.org.uk | the last-ever performance the derrière?
PGP: E2AB1DE4 |
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 836 bytes
Desc: Digital signature
More information about the erlang-questions