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.

  1. What does spawn/1 answer?

    Answer

    The pid of the new process

    It answers at once with the pid, and the caller keeps running.

  2. 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.

  3. 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.

  4. What does self() ! {greet, "Ada"} answer?

    Answer

    {greet,"Ada"}

    The send operator answers the message it sent, so the shell echoes the tuple.

  5. 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.

  6. 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.

  7. What does receive stop -> yes after 0 -> timeout end answer when no message matches?

    Answer

    timeout

    The after 0 branch runs at once when the mailbox has no matching message.

  8. What happens to messages that do not match a receive pattern?

    Answer

    They stay queued for a later receive

    receive is selective: skipped messages remain in the mailbox in their original order.

  9. What does echo:say(hello) answer?

    Answer

    hello

    The server answers with the message it received, and say/1 returns that reply.

  10. What does register(echo, Pid) do?

    Answer

    Binds the atom echo to the pid for sending by name

    After registration, echo ! Msg reaches that process, which is why say/1 sends 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