How to find out node id in erlang

When we run the node () command, we get a pid node. In the format <0.X.0>, if we are on the same node, and we get the result of the form <XY0> when executing the same command from another node. I want to know how to get the value of X from <XY0> on the same node.

+4
source share
2 answers

You mean the integer value of the node pid part or node name. There is a BIF node/1 name, which returns the name of the node to which this pid belongs. So

 node(self()) ==> ' mynode@my _host.com' node(RemotePid) ==> ' remote_node@remote _host.com' 

It also works for port and node specific links. The value of the first field is always 0 for the current node, and the value will change for remote nodes. The values ​​in the links to the same remote node will be different on different nodes.

NB What the pid <XYZ> representation actually means is undefined, so don't depend too much on it. Although this is unlikely to change.

+3
source

It definitely does not cause feelings. <0.X.0> is your local Pid , <DX0> is a distributed option, where D is the node number. Learn more about Pid strcuture . But D will be different for different nodes. Therefore, locally you cannot receive this information.

Of course, you can implement a special RPC call that will return to the distributed Pid calling it (the calling). But the results will differ in the answers. To do this, simply try:

Start three different nodes and register the shell as self .

First

 erl -sname one Erlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.9.1 (abort with ^G) ( one@Alexeys-MacBook-Pro )1> node(). ' one@Alexeys-MacBook-Pro ' ( one@Alexeys-MacBook-Pro )2> register(shell, self()). true 

Second

 erl -sname two Erlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.9.1 (abort with ^G) ( two@Alexeys-MacBook-Pro )1> node(). ' two@Alexeys-MacBook-Pro ' ( two@Alexeys-MacBook-Pro )2> register(shell, self()). true 

Third

 erl -sname three Erlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.9.1 (abort with ^G) ( three@Alexeys-MacBook-Pro )1> node(). ' three@Alexeys-MacBook-Pro ' ( three@Alexeys-MacBook-Pro )2> register(shell, self()). true 

Now go back to node one and try

 ( one@Alexeys-MacBook-Pro )6> net_kernel:connect(' two@Alexeys-MacBook-Pro '). true ( one@Alexeys-MacBook-Pro )7> net_kernel:connect('threeAlexeys-MacBook-Pro'). true ( one@Alexeys-MacBook-Pro )8> {shell, ' two@Alexeys-MacBook-Pro '} ! {hello, from, self()}. {hello,from,<0.147.0>} ( one@Alexeys-MacBook-Pro )82> {shell, ' three@Alexeys-MacBook-Pro '} ! {hello, from, self()}. {hello,from,<0.147.0>} 

Check result on nodes two

 ( two@Alexeys-MacBook-Pro )3> flush(). Shell got {hello,from,<6767.147.0>} ok 

and tree

 ( three@Alexeys-MacBook-Pro )3> flush(). Shell got {hello,from,<6803.147.0>} ok 

As you can see, the first part of Pid is different: <6767.147.0> and <6803.147.0> .

+2
source

All Articles