Processes and message passing
spawn
The spawn function starts a new process and answers its process identifier, a pid. The process runs a fun and shares nothing with the caller.
The new process has its own heap and its own stack, so a crash inside it never corrupts the caller.
erl
1> is_pid(spawn(fun() -> ok end)).
true
2> spawn(fun() -> io:format("hi~n") end), ok.
hi
ok
The second call starts a process that prints, and the shell prints ok as soon as spawn returns. The pid itself changes on every run, so the examples test it with is_pid instead of printing it.
Send with !
The exclamation mark sends a message to a pid. The message is any term, and the sender never waits: send is a fire-and-forget step.
The message lands in the receiver's mailbox, in the order it arrived. Here the shell sends to itself, and the answer repeats the message.
erl
1> self() ! {greet, "Ada"}.
{greet,"Ada"}
receive ... end
The receive expression waits for a mailbox message that matches one of its patterns. The first match runs its body; the rest stay queued.
A timeout after the patterns answers after the milliseconds you give it, so receive never blocks forever. This session collects the message sent above, in a fresh shell, and then shows the timeout.
erl
1> self() ! {greet, "Ada"} , receive {greet, Who} -> Who end.
"Ada"
erl
1> receive stop -> yes after 0 -> timeout end.
timeout
The mailbox
Every process owns a mailbox. Sending never fails, even when the receiver is busy, and the message is copied to the receiver.
receive picks messages selectively, which means a process can skip over messages that do not match yet. A small server is a loop that receives a request, answers the sender, and calls itself again; save it as echo.erl.
-module(echo).
-export([start/0, say/1, stop/0]).
start() ->
Pid = spawn(fun loop/0),
register(echo, Pid),
ok.
say(Msg) ->
echo ! {say, Msg, self()},
receive
{echo, Reply} -> Reply
end.
stop() ->
echo ! stop,
ok.
loop() ->
receive
{say, Msg, From} ->
From ! {echo, Msg},
loop();
stop ->
ok
end.
erlc echo.erl
erl
1> echo:start().
ok
2> echo:say(hello).
hello
3> echo:stop().
ok
Next: Spawn and links
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
spawn/1answer?Answer
The pid of the new process
It answers at once with the pid, and the caller keeps running.
-
What does a spawned process share with its parent?
Answer
Nothing
Each process has its own heap, stack, and mailbox, and messages are copied between them.
-
Why does the lesson call
is_pid(spawn(...))instead of printing the pid?Answer
A pid changes on every run, so a fixed transcript would not replay
The pid is new on every node, so the transcript checks its type, not its value.
-
What does
self() ! {greet, "Ada"}answer?Answer
{greet,"Ada"}The send operator answers the message it sent, so the shell echoes the tuple.
-
Where does the message go?
Answer
Into the mailbox of the receiver
A message lands in the mailbox of the target process and waits for a matching receive.
-
Does the sender wait for the receiver to handle the message?
Answer
No, send is fire and forget
The sender continues immediately; a reply, if wanted, is a separate message.
-
What does
receive stop -> yes after 0 -> timeout endanswer when no message matches?Answer
timeoutThe
after 0branch runs at once when the mailbox has no matching message. -
What happens to messages that do not match a
receivepattern?Answer
They stay queued for a later receive
receive is selective: skipped messages remain in the mailbox in their original order.
-
What does
echo:say(hello)answer?Answer
helloThe server answers with the message it received, and
say/1returns that reply. -
What does
register(echo, Pid)do?Answer
Binds the atom
echoto the pid for sending by nameAfter registration,
echo ! Msgreaches that process, which is whysay/1sends to the atom.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev