36

I tried google, but found getppid() which gets the parent pid of the current process.

I need something like getppid(some_other_pid), is there such a thing? Basically takes the pid of some process and returns the parent process' pid.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
hasen
  • 161,647
  • 65
  • 194
  • 231

7 Answers7

38

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Arkaitz Jimenez
  • 22,500
  • 11
  • 75
  • 105
17

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

Samuel Kerrien
  • 6,965
  • 2
  • 29
  • 32
  • 1
    Awesome simple answer. For a command you can copy/paste to see it in action try **`ps -p $$ -o ppid=`**. $$ is the current Process ID. – Jess Apr 18 '13 at 16:53
8

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.

Pejman
  • 1,328
  • 1
  • 18
  • 26
3

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

Patryk
  • 22,602
  • 44
  • 128
  • 244
vpram86
  • 5,860
  • 4
  • 28
  • 40
  • Trying it "blind-foldedly", not sure why it's not quite compiling. is it only for OS X? Is it supposed to work on linux? – hasen Oct 06 '09 at 13:51
  • 1
    you should create a friendlier hyperlink. https://www.w3.org/QA/Tips/noClickHere – Jeff Feb 15 '16 at 23:36
1

one more way to get it from proc entry:

cat /proc/<pid>/status | grep PPid:
Saurabh Sengar
  • 878
  • 2
  • 12
  • 20
0

We can use pstree command also.

pstree -p -s <pid of the process>

pstree -s gives tree of all the ancestors. Adding -p will give you the pid as well.

Example :Assume there is a process with pid=6206. Using the pstree command

pstree -p -s 6206

You will get the process tree.

systemd(1)───lightdm(1066)───lightdm(1191)───upstart(1360)───gnome-terminal-(5222)───bash(5229)───cpu-print(6206)

Here the parent PID is 5229

Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26
0

An easy way to craft this in pure C with only standard libraries:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXBUF      (BUFSIZ * 2)

int pgetppid(int pid) {
    int ppid;
    char buf[MAXBUF];
    char procname[32];  // Holds /proc/4294967296/status\0
    FILE *fp;

    snprintf(procname, sizeof(procname), "/proc/%u/status", pid);
    fp = fopen(procname, "r");
    if (fp != NULL) {
        size_t ret = fread(buf, sizeof(char), MAXBUF-1, fp);
        if (!ret) {
            return 0;
        } else {
            buf[ret++] = '\0';  // Terminate it.
        }
    }
    fclose(fp);
    char *ppid_loc = strstr(buf, "\nPPid:");
    if (ppid_loc) {
        ppid = sscanf(ppid_loc, "\nPPid:%d", &ppid);
        if (!ppid || ppid == EOF) {
            return 0;
        }
        return ppid;
    } else {
        return 0;
    }

}

int main () {
    int ppid, pid = 373;  // my current cron pid
    ppid = pgetppid(pid);
    printf("PPid = %d\n", ppid);
}
James Risner
  • 5,451
  • 11
  • 25
  • 47