Programmatically get parent pid of another process?

or from a unix shell you can try ps -p <child_pid> -o ppid=


I am 7 years late to the party but for anyone who may stumble upon this question, here's an alternative solution on OS X. Other answers posted here are correct and sysctl() will do the job, but you can also use proc_pidinfo to obtain a lot of useful information about a process.

#include <libproc.h>

int getppid(const pid_t pid)
{
    proc_bsdinfo info;
    proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &info, sizeof(info));
    return info.pbi_ppid;
}

Obviously, additional error checking is required.


I think the simplest thing would be to open "/proc" and parse the contents.

You'll find the ppid as the 4th parameter of /proc/pid/stat

In C, libproc has a get_proc_stats function for parsing that file: see Given a child PID how can you get the parent PID for an example.


You can have a look at sysctl() system call and this link.