[erlang-questions] how to know who is the parent pid of a child pid?

Fred Hebert <>
Wed Apr 3 01:21:52 CEST 2013


Short answer:
 There is no way. You need to have the parent give it to
 the child through a message or as an argument when spawning
 it.

Slightly longer answer:
 OTP hides the parent in the process dictionary (useful when handling
 shutdown messages from supervisors and trapping exits). OTP processes
 thus have the parent accessible.

Long answer:
  Do not use OTP internals to do your bidding in there. They belong to
  OTP libraries and you should stay out of its way (similarly, you
  shouldn't play with links set by OTP or other pdict values).

  For debugging purposes, however, given it's read only and short-lived
  in terms of how much you depend on the information, using the process
  dictionary's value is fine. You can do it by calling
  `process_info(Pid, dictionary)` and hoping nobody shoved a GB of data
  in there.

  For most production purposes outside of debugging, if you need to pass
  a parent, do it as an argument to a function that spawns the process.
  This will make it easier to test things and the dependency will be
  explicit, so fewer people get surprise breakage when they 'give away'
  the process to a new adoptive parent (think of how people do it with
  ports, sockets, or ETS tables).

  Being explicit is usually the safest option, and the most readable one
  too.

Regards,
Fred.


On 04/02, Brisa Jim�nez wrote:
> Hi everyone,
> I have a question:
> How to know who is the parent pid of a child pid?
> Thank you very much!

> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list