Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know the id of a node in erlang

Tags:

erlang

pid

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

like image 603
user1640672 Avatar asked Sep 01 '12 15:09

user1640672


2 Answers

Do you mean the integer value of the node part of a pid or the name of the node. For the name there is the BIF node/1 which returns the name of the node to which that pid refers. So

node(self())    ==> 'mynode@my_host.com'
node(RemotePid) ==> 'remote_node@remote_host.com'

It also works for ports and refs which are node specific. The value of the first field is always 0 for the current node and the value will vary for remote nodes. The values in references to the same remote node will be different on different nodes.

N.B. What the representation of a pid <X.Y.Z> actually means is not defined so don't depend too much on it. Although it is unlikely to change.

like image 79
rvirding Avatar answered Nov 11 '22 05:11

rvirding


This definitely doesn't make sens. <0.X.0> is your local Pid, <D.X.0> - distributed variant, where D is a node's number. More information about Pid strcuture. But D will be different for different nodes. So locally you can't get this information.

Off course, you can implement special RPC call, that will return to caller his (caller's) distributed Pid. But results will differ in answers. To ensure this, try simple:

Start three different nodes and register 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 return 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, first part of Pid differs: <6767.147.0> and <6803.147.0>.

like image 44
Alexey Kachayev Avatar answered Nov 11 '22 06:11

Alexey Kachayev