Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given a child PID how can you get the parent PID

Tags:

c

fork

pid

I'm working on a project where I have a number of PIDs and I have to find out which of these are zombie processes and then kill their parent processes in order to kill the initial zombie process. I am not sure if there is any way to find out what the PPID of a given PID is. Any help would be appreciated.

like image 892
Husso Avatar asked Oct 31 '22 09:10

Husso


1 Answers

At the source for the ps command, there is a function called get_proc_stats defined in proc/readproc.h that (among other things) returns the parent pid of a given pid. You need to do install libproc-dev to get this function. You can then do:

#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 never used this but according to author this may be helpful.

like image 192
Shafi Avatar answered Nov 08 '22 04:11

Shafi