extend erlang:port_info/1,2 to show the OS pid of a spawned process
Matthias Lang
matthias@REDACTED
Thu Aug 19 16:10:51 CEST 2010
Hi,
git fetch git://github.com/matthiasl/otp.git add-os-pid-to-port-info
This patch extends erlang:process_info/1,2 so that it reports a new
item, 'os_pid'. Example:
3> Port = erlang:open_port({spawn, "wc /dev/zero"}, []).
#Port<0.475>
4> erlang:port_info(Port).
[{name,"wc /dev/zero"},
{links,[<0.35.0>]},
{id,475},
{connected,<0.35.0>},
{input,0},
{output,0},
{os_pid,17736}] %% <----- This line is new
5> exit(Port, kill).
** exception exit: killed
6> erlang:port_info(Port).
undefined
7> os:cmd("kill 17736").
Why I did this:
Knowing the operating system's pid for a spawned process is useful
for debugging and for killing processes, such as "wc /dev/zero",
which won't die just because you close stdin/stdout.
The same effect can be accomplished with a wrapper. Such wrappers
are considered "less than ideal" by some, e.g. in this discussion:
http://www.erlang.org/cgi-bin/ezmlm-cgi?4:mss:50223:201003:clkfnmmlhkikkegdgclb
Limitations and concerns:
- I've only tested the patch on linux. (I expect it to work, but
report 'undefined' as the os_pid on win32 and vxworks)
- I store the pid in the port struct, which costs pid_t extra
storage per spawned process (4 or 8 octets). An alternative
would be to step through ifd[] and ofd[] to find the right
entry, but that would involve exporting a new function from sys.c
Matt
More information about the erlang-patches
mailing list