How to get the running process' parent process' ID in C / C++?

How to get the running process' parent process' ID in C / C++?

asked Jun 5 by dtivl (2,500 points)

1 Answer

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 Jun 5 by dtivl (2,500 points)

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...