views:

773

answers:

3

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.

A: 

pstree knows how to do it, maybe look at the source and see how it does it

gnibbler
+7  A: 

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

Arkaitz Jimenez
well, what part of `/proc/<pid>/` holds the parent pid?
hasen j
The 4 parameter en /proc/pid/stat
Arkaitz Jimenez
Aha, awesome! Can you edit the answer to add that to it?
hasen j
+2  A: 

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

Aviator
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 j
It will work only for BSD and OS X..
Aviator