by Example

Processes


Erlang was designed for massive concurrency.
A process is created and terminated extremelly
fast, that's why you can actually have thousands of them.


A process executes some piece of code, then it terminates.



It can also run forever.


Each Erlang process has a small memory footprint,
which can grow/shrink dynamically.


Use the built-in `spawn` function to create a process.
Once created, a process can access its own process id,
by calling the `self` function.







-module(processes).
-compile([export_all]).

proc() ->
    io:format("I'm a process with id ~p~n", [self()]).


loop() -> loop().








run() ->
    spawn(fun() -> proc() end),
    spawn(processes, proc, []),
    spawn(?MODULE, proc, []),
    ok.
1> c(processes).
{ok,processes}
2> processes:run().
I'm a process with id <0.166.0>
I'm a process with id <0.167.0>
I'm a process with id <0.168.0>
ok


Next: Pattern Matching