In the source for the ps command, there is a get_proc_stats function defined in proc/readproc.h that (among other things) returns the parent pid given pid . You need to install libproc-dev to get this function. Then you can:
#include <proc/readproc.h> void printppid(pid_t pid) { proc_t process_info; get_proc_stats(pid, &process_info); printf("Parent of pid=%d is pid=%d\n", pid, process_info.ppid); }
This is taken from here . I have never used this, but according to the author, it can be useful.
source share