Today’s Erlang Thursday is a short one on c:pid/3.
c:pid/3
takes the three different parts of a pid as its arguments, and returns a constructed Pid type corresponding to those values.
We’ll call self
to get a pid that we know is good, and we can use that to compare the result of calling c:pid/3
.
self(). % <0.42.0> c:pid(0, 42, 0). % <0.42.0> self() =:= c:pid(0, 42, 0). % true
Why is this useful? Sometimes when inspecting what is going on in a live system there are certain calls in Erlang that expect a pid()
type, and not just the pid numbers.
c:regs(). % % ** Registered procs on node nonode@nohost ** % Name Pid Initial Call Reds Msgs % application_controlle <0.7.0> erlang:apply/2 463 0 % code_server <0.19.0> erlang:apply/2 128774 0 % erl_prim_loader <0.3.0> erlang:apply/2 163760 0 % error_logger <0.6.0> gen_event:init_it/6 220 0 % file_server_2 <0.18.0> file_server:init/1 448 0 % global_group <0.17.0> global_group:init/1 59 0 % global_name_server <0.13.0> global:init/1 51 0 % inet_db <0.16.0> inet_db:init/1 206 0 % init <0.0.0> otp_ring0:start/2 3398 0 % kernel_safe_sup <0.27.0> supervisor:kernel/1 58 0 % kernel_sup <0.11.0> supervisor:kernel/1 49109 0 % rex <0.12.0> rpc:init/1 35 0 % standard_error <0.21.0> erlang:apply/2 9 0 % standard_error_sup <0.20.0> supervisor_bridge:standar 41 0 % user <0.24.0> group:server/3 36 0 % user_drv <0.23.0> user_drv:server/2 17940 0 % % ** Registered ports on node nonode@nohost ** % Name Id Command % ok erlang:is_process_alive(c:pid(0, 6, 0)). % true
So let’s see what happens when we try to feed it something to break it, but in a meaningful way.
c:pid(0, 0, 0). % <0.0.0> c:pid(111110, 0, 1111110). % ** exception error: bad argument % in function list_to_pid/1 % called as list_to_pid("<111110.0.1111110>") % in call from c:pid/3 (c.erl, line 424)
So it looks like the pid <0.0.0>
is a valid pid, but when we throw it something else, we see it is trying to call list_to_pid
.
So let’s take a quick look at list_to_pid
.
erlang:list_to_pid("<0.42.0>"). % <0.42.0> c:pid(0, 42, 0) =:= erlang:list_to_pid("<0.42.0>"). % true
So it looks like c:pid/3
is a wrapper function around list_to_pid
that builds the 3 parts of a pid into a string, and then gets a pid()
type from that call.
–Proctor