Given the child PID, how can you get the parent PID

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

+5
source share
2 answers

The getppid () function does the following:

#include <unistd.h> int main() { pid_t ppid; ppid = getppid(); return (0); } 
+2
source

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.

+2
source

Source: https://habr.com/ru/post/1243009/


All Articles