Process registry
In Erlang a pid is the true address of a process, but nobody can guess it: a client that starts after the server has no way to name it. A registration binds an atom to a pid, so a caller can send to echo instead of to a value it received. This lesson uses the local registry, the global one above it, and the tuple that lets a library work with either.
register and whereis
register(Name, Pid) binds an atom to a live process and answers true. whereis(Name) answers the pid or undefined, and registered() lists every name of the node. Killing the process releases the name, which is why a server that crashes does not leave a stuck registration:
registry.erl
-module(registry). -export([main/0]). main() -> Pid = spawn(fun() -> receive stop -> ok end end), true = register(echo, Pid), io:format("~p~n", [whereis(echo) =:= Pid]), io:format("~p~n", [lists:member(echo, registered())]), true = unregister(echo), io:format("~p~n", [whereis(echo)]), exit(Pid, kill).
erlc registry.erl
erl -noshell -eval 'registry:main(), halt().'
true true undefined
The name is a local resource of one node. A second node has its own table, so echo on one node and echo on another are two different servers, and a caller must know which node it means.
A name that is taken
Registering a name that another live process owns raises badarg. A server that starts twice therefore fails on its second start, which is usually the right answer: two processes under one name would split the traffic. A program that wants to know instead of crash catches the class:
twice.erl
-module(twice). -export([main/0]). main() -> First = spawn(fun() -> receive stop -> ok end end), Second = spawn(fun() -> receive stop -> ok end end), true = register(echo, First), Result = try register(echo, Second) of true -> ok catch error:badarg -> already_registered end, io:format("~p~n", [Result]), exit(First, kill), exit(Second, kill).
erlc twice.erl
erl -noshell -eval 'twice:main(), halt().'
already_registered
The second process was started before the check and is killed at the end, so nothing leaks. A supervisor avoids the whole question by owning the name and starting the server once; the supervisors lesson shows the child spec.
global names
global keeps a name across the connected nodes. It answers yes when the name is free and no when another node owns it, which is a value and not an exception. On a single node it works as a local table with the same calls:
glob.erl
-module(glob). -export([main/0]). main() -> Pid = spawn(fun() -> receive stop -> ok end end), yes = global:register_name(shared, Pid), io:format("~p~n", [global:whereis_name(shared) =:= Pid]), ok = global:unregister_name(shared), io:format("~p~n", [global:whereis_name(shared)]), exit(Pid, kill).
erlc glob.erl
erl -noshell -eval 'glob:main(), halt().'
true undefined
The global table is slower than the local one and can block while the nodes agree, so a program uses it for the few names that must be unique across the cluster. The distributed Erlang lesson connects two nodes and sends the message, and a later lesson covers the partitions that make a global name hard.
The via tuple
Modern OTP functions accept {via, Module, Name} in place of a pid or a local name. The module answers whereis_name/1 and register_name/2, so the same call works with a local name, global or a registry such as pg. A gen_server started with {via, global, shared} is found by gen_server:call({via, global, shared}, Request) on any connected node, and the caller never learns the pid. The GenServer lesson uses the plain local form; the distribution lessons use the global one.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
register(Name, Pid)answer?Answer
trueA successful registration answers
true; a taken name raisesbadarg. -
What does
whereis(Name)answer when no process owns the name?Answer
undefinedwhereis/1answersundefinedfor an unregistered name. -
What happens to a registered name when its process dies?
Answer
It is released with the process
A registration lives only as long as the process that owns it.
-
What does
global:register_name/2answer when the name is taken?Answer
noglobalanswersyesorno, a value the caller matches. -
What does this module print?
-module(q1). -export([main/0]). main() -> Pid = spawn(fun() -> receive stop -> ok end end), true = register(echo, Pid), io:format("~p~n", [whereis(echo) =:= Pid]), exit(Pid, kill).Answer
truewhereis/1answers the registered pid, which is the one just spawned. -
What does this module print?
-module(q2). -export([main/0]). main() -> Pid = spawn(fun() -> receive stop -> ok end end), true = register(echo, Pid), true = unregister(echo), io:format("~p~n", [whereis(echo)]), exit(Pid, kill).Answer
undefinedThe name was removed while the process is still alive, so
whereis/1answersundefined. -
What does this module print?
-module(q3). -export([main/0]). main() -> A = spawn(fun() -> receive stop -> ok end end), B = spawn(fun() -> receive stop -> ok end end), true = register(echo, A), R = try register(echo, B) of true -> ok catch error:badarg -> taken end, io:format("~p~n", [R]), exit(A, kill), exit(B, kill).Answer
takenThe name already belongs to A, so the second registration raises badarg.
-
What does this module print?
-module(q4). -export([main/0]). main() -> Pid = spawn(fun() -> receive stop -> ok end end), yes = global:register_name(shared, Pid), io:format("~p~n", [global:whereis_name(shared) =:= Pid]), ok = global:unregister_name(shared), exit(Pid, kill).Answer
trueThe global name resolves to the pid on this node.
-
Which tuple lets a library use a local name or a global one without a change?
Answer
{via, Module, Name}The
viatuple names a module that resolves the name, so the caller does not care which registry it is. -
Why is the local registry preferred over
globalwhen one node is enough?Answer
It is faster and cannot block on another node
The global table coordinates nodes, which costs time and can block during a partition.
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.
Автор статьи: Arthur Isaev