erlang:list_to_pid/1 with remote pid

Yao Bao by@REDACTED
Sat Jun 20 08:28:24 CEST 2020


Hi,

By accident, I found erlang:list_to_pid/1 does not produce the same "thing" when putting remote pid string as the argument.

And, I found this mailing list post: http://erlang.org/pipermail/erlang-questions/2009-February/042100.html <http://erlang.org/pipermail/erlang-questions/2009-February/042100.html>

Quite interesting, but I still can't figure out why after reading the whole post.

I checked the source code, a function called list_to_pid_1() in erts/emulator/beam/bif.c handles this. Honestly speaking, I can't fully understand it. So I did some guess work.

I wrote a simple module:

%%
%% A dist demo.
%%
-module(a_dist_demo).

-export([start/1, loop/0]).

start(Node) ->
    spawn(Node, fun() -> loop() end).

loop() ->
    receive
    {From, {echo, Any}} ->
        From ! Any,
        loop()
    end.

And two nodes get started:

MacBookPro:code by$ erl -name yao1
Erlang/OTP 22 [erts-10.5.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]
Eshell V10.5.3  (abort with ^G)
(yao1@REDACTED)1> Pid = a_dist_demo:start('yao2@REDACTED').
<9172.91.0>
(yao1@REDACTED)2> Bin = erlang:term_to_binary(Pid).
<<131,103,100,0,21,121,97,111,50,64,77,97,99,66,111,111,
  107,80,114,111,46,108,111,99,97,108,0,0,0,...>>
(yao1@REDACTED)3> io:format("~p~n", [Bin]).
<<131,103,100,0,21,121,97,111,50,64,77,97,99,66,111,111,107,80,114,111,46,108,
  111,99,97,108,0,0,0,91,0,0,0,0,2>>
ok
(yao1@REDACTED)4> Pid ! {self(), {echo, abcd}}.
{<0.87.0>,{echo,abcd}}
(yao1@REDACTED)5> receive X -> X end.
abcd
(yao1@REDACTED)6>
(yao1@REDACTED)8> Pid1 = erlang:list_to_pid("<9172.91.0>").
<9172.91.0>
(yao1@REDACTED)9> Pid = Pid1.
** exception error: no match of right hand side value <9172.91.0>
(yao1@REDACTED)11> Bin1 = erlang:term_to_binary(Pid1).
<<131,103,100,0,21,121,97,111,50,64,77,97,99,66,111,111,
  107,80,114,111,46,108,111,99,97,108,0,0,0,…>>
(yao1@REDACTED)12> Bin = Bin1.
** exception error: no match of right hand side value 
                    <<131,103,100,0,21,121,97,111,50,64,77,97,99,66,111,111,
                      107,80,114,111,46,108,111,99,97,108,0,0,0,...>>
(yao1@REDACTED)13>

MacBookPro:code by$ erl -name yao2
Erlang/OTP 22 [erts-10.5.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]
Eshell V10.5.3  (abort with ^G)
(yao2@REDACTED)1> Bin = <<131,103,100,0,21,121,97,111,50,64,77,97,99,66,111,111,107,80,114,111,46,108,111,99,97,108,0,0,0,91,0,0,0,0,2>>.
<<131,103,100,0,21,121,97,111,50,64,77,97,99,66,111,111,
  107,80,114,111,46,108,111,99,97,108,0,0,0,...>>
(yao2@REDACTED)2> Pid = erlang:binary_to_term(Bin).
<0.91.0>
(yao2@REDACTED)3> Pid ! {self(), {echo, 1234}}.
{<0.87.0>,{echo,1234}}
(yao2@REDACTED)4> receive X -> X end.
1234
(yao2@REDACTED)5>

The result is: the same binary produces two different shapes of pid ( <9172.91.0> and <0.91.0> ) in two nodes. Apparently, they refer to the same process.

I guess the remote pid need some unique information came from the remote node, and erlang:list_to_pid/1 just do not have those information.

I hope some internal thoughts can be revealed for this.

Cheers,
Yao

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20200620/16613810/attachment.htm>


More information about the erlang-questions mailing list