Processes Have Parents
Every process running on your system has a parent process. Each process knows its parent process identifier (hereby referred to as ‘ppid’).
In the majority of cases the parent process for a given process is the process that invoked it. For example, you’re an OSX user who starts up Terminal.app and lands in a bash prompt. Since everything is a process that action started a new Terminal.app process, which in turn started a bash process.
The parent of that new bash process will be the Terminal.app process. If you then invoke ls(1) from the bash prompt, the parent of that
ls process will be the
bash process. You get the picture.
Since the kernel deals only in pids there is a way to get the pid of the current parent process. Here’s how it’s done in Ruby:
# Notice that this is only one character different from getting the # pid of the current process. puts Process.ppid
irb session open run the following command at a terminal:
$ ps -p <ppid-of-irb-process>
That command should show a process called ‘bash’ (or ‘zsh’ or whatever) with a pid that matches the one that was printed in your
In the Real World
There aren’t a ton of uses for the ppid in the real world. It can be important when detecting daemon processes, something covered in a later chapter.
Process.ppid maps to getppid(2).