When you see the registration process in Erlang, you do not understand registration, primarily because you do not understand the scope of the registered atom. Just now I suddenly want to understand:
Copy Code code as follows:
The atoms associated with a registration process have global scope
That is, the atoms that are associated with the registration process can be used globally.
The concurrency mechanism in Erlang is implemented through a message mailbox, in which only the message mailboxes are communicated between processes, and interprocess communications need to know the process numbers of the processes, while using spawn to produce new processes returns the process number of the new process for use.
One of the simplest procedures for interprocess communication is as follows
Copy Code code as follows:
-module (Test).
Start ()->
Spawn (? MODULE, Loop, []).
Loop ()->
Io:format ("Waiting for New Message.~n"),
Receive
M-> Io:format ("New message: ~p~n", [M])
End
Loop ().
This module is compiled using C (test) in the Erlang Shell and can be used simply.
In the following code, the comment in front of the statement represents an explanation, and the comment following the statement represents the output value, and the value following the% => represents the return value of the statement
Copy Code code as follows:
% compile this module
C (Test).
% Open Infinite loop
Pid = Test:start ().
% waiting for the new message. % new process runs immediately after Spawn
% => <0.35.0>% Returns the process number of the new process
% send Message to process
Pid! ' Message '.
% New message:message% received message
% waiting for the new message. % continue to receive messages
% => message% statement returns value, not process return messages
In order to users do not have to Pid every time! ' Message ', you can add a call method to wrap it.
Copy Code code as follows:
This allows you to send messages using Test:call (Pid, ' message ').
But there is an obvious disadvantage of this writing, call calls require Pid parameters, but can not be removed, because the process number required to communicate, so the use of users need to maintain a process number.
Erlang provides a mechanism for registering processes that can be used to relate atoms to a process to solve this problem
Using the register (Atom, PID), you can associate atom with the process number PID process, and the atom
Modify the start function above to
Copy Code code as follows:
Start ()->
Register (TESTP, spawn (?) MODULE, Loop, [])).
Thus, the new process will be associated with the atomic TESTP, at which point the atom can use the message-sending operator as a Pid!
Copy Code code as follows:
So you can modify the call function above, remove the Pid parameters, and use the associated atom, the associated atom is not only valid in the module, in the global scope is valid.
Copy Code code as follows:
Therefore, the following procedures have been modified using the registration process
Copy Code code as follows:
-module (Test).
Start ()->
Register (TESTP, spawn (?) MODULE, Loop, [])).
Loop ()->
Io:format ("Waiting for New Message.~n"),
Receive
M-> Io:format ("New message: ~p~n", [M])
End
Loop ().
Call (M)->
TESTP! M.
Registration-related BIF
Copy Code code as follows:
% register Atom to Pid
Register (Atom, Pid).
% to cancel Atom registration
Unregister (Atom).
% returns the process number associated with atom, if not associated, returns undefined
Whereis (Atom).
% returns all registered process names in the system
Registered ().