How to get the running process' parent process' ID?

How to get the running process' parent process' ID (ppid)?

asked Apr 1 by zma (2,200 points)

4 Answers

In Bash, you can get the parent process's pid from the variable PPID.

Note that in a (...) subshell, the $$ stores the subshell's parent shell pid actually.

answered Apr 3 by zma (2,200 points)
edited Apr 3 by zma

In Python, you can get the parent process' pid by calling os.getppid():

import os

ppid = os.getppid()

One example:

The shell's pid:

$ echo $$
21779

Start a python REPL and get its parent pid (the shell's):

$ python
Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.getppid()
21779
>>> 
answered Apr 12 by zma (2,200 points)

In Go, you may call the os.Getppid() func from the os package to get the parent process' pid.

func Getppid() int

Getppid returns the process id of the caller's parent.

One example is as follows.

$ gore
gore version 0.2.6  :help for help
gore> :import "fmt"
gore> :import "os"
gore> fmt.Printf("ppid: %d\n", os.Getppid())
ppid: 19824
12
nil
gore> 
answered Apr 14 by zma (2,200 points)
edited Apr 17 by zma

In C and C++, you can call the getppid() library function which is a function from the POSIX library.

#include <sys/types.h>
#include <unistd.h>

pid_t getppid(void);   

getppid() returns the process ID of the parent of the calling process.

Example usage:

getppid.c

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
  pid_t ppid = getppid();

  printf("ppid: %lu\n", ppid);
}

Build and run it:

$ gcc getppid.c -o s && ./s
ppid: 22312

Verify the pid of the parent process (the Bash shell):

$ echo $$
22312
answered Apr 18 by zma (2,200 points)
edited Apr 22 by zma

Please log in or register to answer this question.

Related questions

4 answers
5 answers
7 answers
asked Mar 31 by zma (2,200 points)
5 answers
asked Mar 31 by zma (2,200 points)
0 answers
Welcome to Do This In Various Langs (dtivl), where you can ask questions and receive solutions in various programming languages.
Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...