Reprinted: http://blog.csdn.net/cuiweican/archive/2010/07/14/5735476.aspx
The Erlang/OTP's global module can be used to name the processes in the Erlang distributed system in the form of atoms to ensure that each process in the distributed system has a unique name at the same time.
Now let's take a look at the performance of the global module in actual operations. register the same name in two Erlang nodes that are not connected at the beginning, and then connect the two nodes, the system will monitor the two nodes with the same name, and will cancel the name registration in one node.
First, register name a on two independent nodes (these two nodes are started with Erl-sname one and Erl-sname two respectively) and perform the following operations on one node,
Eshell v5.6.2 (abort with ^ g)
(One @ cuiweican) 1> Global: register_name (A, self ()).
Yes
(One @ cuiweican) 2> Global: whereis_name ().
From the above we can see that the name registration is successful (register_name returns yes). When whereis_name is used for query, as we expected, this function returns the value of self, that is, the PID of the Process shell.
Now the same operation is performed on the two node.
Eshell v5.6.2 (abort with ^ g)
(Two @ cuiweican) 1> Global: register_name (A, self ()).
Yes
(Two @ cuiweican) 2> Global: whereis_name ().
Similarly, the name is successfully registered. Note that both nodes have successfully registered the global name A, because there is no connection between the two nodes. Once these two nodes are connected, Erlang/OTP will automatically resolve this conflict. By default, one of them will be deregistered.
Now let's take a look at what will happen when two nodes are connected. We ping the node one to two to connect the two nodes.
(One @ cuiweican) 3> net_adm: Ping (two @ cuiweican ).
Pong
(One @ cuiweican) 4>
= Info report ==== 13-feb-2009: 03: 05: 22 ====
Global: Name Conflict terminating {,}
(One @ cuiweican) 4> Global: whereis_name ().
(One @ cuiweican) 5>
According to the report output from one node, the node two name is canceled. When node Two Queries name a, it will find that name a is registered in node one.