Can someone explain the structure of a Pid in Erlang?
Pids looks like this: <A.B.C>
, e.g. <0.30.0>
, but I would like to know what is the meaning of these three "bits": A
, B
and C
.A
seems to be always 0
on a local node, but this value changes when the Pid's owner is located on another node.
Is it possible to directly send a message on a remote node using only the Pid? Something like that: <4568.30.0> ! Message
, without having to explicitly specify the name of the registered process and the node name ( {proc_name, Node} ! Message
)?
5条答案
按热度按时间ntjbwcob1#
Printed process ids < A.B.C > are composed of 6:
Internally, the process number is 28 bits wide on the 32 bit emulator. The odd definition of B and C comes from R9B and earlier versions of Erlang in which B was a 15bit process ID and C was a wrap counter incremented when the max process ID was reached and lower IDs were reused.
In the erlang distribution PIDs are a little larger as they include the node atom as well as the other information. (Distributed PID format)
When an internal PID is sent from one node to the other, it's automatically converted to the external/distributed PID form, so what might be
<0.10.0>
(inet_db
) on one node might end up as<2265.10.0>
when sent to another node. You can just send to these PIDs as normal.For more information see: Internal PID structure, Node creation information, Node creation counter interaction with EPMD
iklwldmw2#
如果我没记错的话,格式是
<nodeid,serial,creation>
。0是当前节点,就像一台计算机总是用主机名"localhost"来指代自己一样。这是旧内存,所以可能不是100%正确。但是是的,你可以用
list_to_pid/1
来构建pid。当然。你可以使用任何你需要的方法来构建你的PidString。也许你可以写一个函数来生成它,并使用它来代替PidString,如下所示:
biswetbf3#
进程标识< A.B.C >由以下内容组成:
2位的创建标记不显示在pid中,而是在内部使用,并在每次节点重新启动时增加。
llmtgqce4#
PID引用一个进程和一个节点表。因此,只有当PID在执行调用的节点中已知时,才能将消息直接发送到PID。
如果执行调用的节点已经是运行进程的节点,则此操作可能会起作用。
xnifntxz5#
Apart from what others have said, you may find this simple experiment useful to understand what is going on internally:
So, you can se that the node name is internally stored in the pid. More info in this section of Learn You Some Erlang.