extend erlang:port_info/1,2 to show the OS pid of a spawned process

Matthias Lang <>
Thu Aug 19 16:10:51 CEST 2010


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"}, []).
  4> erlang:port_info(Port).
  [{name,"wc /dev/zero"},
   {os_pid,17736}]           %% <----- This line is new
  5> exit(Port, kill).
  ** exception exit: killed
  6> erlang:port_info(Port).
  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:


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


More information about the erlang-patches mailing list