Can someone explain the structure of Pid in Erlang?

Can someone explain the Pid structure in Erlang?

Pids looks like this: <ABC> , for example. <0.30.0>, but I would like to know what these three β€œbits” mean: A, B and C.

'A' is always always 0 on the local node, but this value changes when the Pid owner is on another node.

Is it possible to directly send a message to a remote node using only Pid? Something like this: <4568.30.0>! Message, without the need to explicitly indicate the name of the registered process and the name of the node ({proc_name, Node}!)?

+61
erlang pid
Oct 28 '08 at 13:42
source share
5 answers

The printed <ABC> process identifiers consist of 6 :

  • A, node number (0 - local node, arbitrary number for the remote node)
  • B, the first 15 bits of the process number (index in the process table) 7
  • C, bit 16-18 of the process number (same process number as B) 7

The internal process number is 32 bits in a 32-bit emulator. The odd definition of B and C comes from R9B and earlier versions of Erlang, in which B was the process identifier of 15 bits, and C was the bypass counter, increasing when the maximum process identifier was reached, and lower identifiers were reused.

In the erlang distribution, the PIDs are slightly larger, as they include the atom atom, as well as other information. ( Distributed PID format )

When an internal PID is sent from one node to another, it is automatically converted to an external / distributed PID form, so what can be <0.10.0> ( inet_db ) on one node can end up as <2265.10.0> when sent to another node. You can simply send these PIDs as usual.

 % get the PID of the user server on OtherNode RemoteUser = rpc:call(OtherNode, erlang,whereis,[user]), true = is_pid(RemoteUser), % send message to remote PID RemoteUser ! ignore_this, % print "Hello from <nodename>\n" on the remote node console. io:format(RemoteUser, "Hello from ~p~n", [node()]). 

For more information, see Internal PID Structure , Node Creation Information , Node Creation Counter Interaction with EPMD

+64
Nov 04 '08 at 15:29
source share

If I remember this correctly, the format is <nodeid,serial,creation> . 0 is the current node, as the computer always has the host name "localhost" to refer to itself. This is from the old memory, so it cannot be 100% correct.

But yes. You can build pid with list_to_pid/1 , for example.

 PidString = "<0.39.0>", list_to_pid(PidString) ! message. 

Sure. You simply use any method you need to create a PidString. Probably write a function that generates it, and use this instead of PidString as follows:

 list_to_pid( make_pid_from_term({proc_name, Node}) ) ! message 
+13
Oct 28 '08 at 15:09
source share

The process identifier <ABC> consists of:

  • A, node id, which is not an arbitrary, but an internal index for the node in dist_entry. (This is actually an atom slot integer for the name node.)
  • B, the index of the process that refers to the internal index in proctab, (0 -> MAXPROCS).
  • C, Serial, which increases with every MAXPROCS achievement.

The 2-bit creation tag is not displayed in pid, but is used internally and incremented with every restart of the node.

+7
Apr 30 '09 at 23:17
source share

The PID code refers to the process and the node table. Thus, you can only send a message to the PID if it is known in the node from which you are making a call.

It is possible that this will work if the node you are making the call already knows about the node on which the process is running.

+2
Oct 28 '08 at 14:24
source share

Besides what others have said, you may find this simple experiment useful in understanding what is going on inside:

 1> node(). nonode@nohost 2> term_to_binary(node()). <<131,100,0,13,110,111,110,111,100,101,64,110,111,104,111, 115,116>> 3> self(). <0.32.0> 4> term_to_binary(self()). <<131,103,100,0,13,110,111,110,111,100,101,64,110,111,104, 111,115,116,0,0,0,32,0,0,0,0,0>> 

So, you can indicate that the node name is internally stored in pid. Learn more in this section of Learn You Some Erlang.

0
Feb 16 '15 at 13:14
source share



All Articles