简体   繁体   English

如何通过使用C / C ++在Linux中提供进程ID来获取父进程ID?

[英]How to get the Parent Process ID by giving a Process ID in Linux using C/C++?

I know it's possible to get ppid information from /proc/PID/status but I just want to use it as the backup plan. 我知道可以从/proc/PID/status获取ppid信息,但我只想将其用作备份计划。

So, is it possible to get the PPID by giving a PID number? 那么,是否可以通过提供PID号来获取PPID? getppid(2) can only get the PPID of current process so it's not helpful in this situation. getppid(2)只能获取当前进程的PPID,因此在这种情况下无济于事。

Any idea? 任何想法? Thank you. 谢谢。

I think you can use get_proc_stats function to do this. 我认为您可以使用get_proc_stats函数来执行此操作。 But you need to install the libproc-dev library. 但是您需要安装libproc-dev库。

Here is an example : 这是一个例子:

proc_t p_info;
get_proc_stats(pid_num, &p_info);
int parent_pid = p_info.ppid;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM