【问题标题】:How to get the Parent Process ID by giving a Process ID in Linux using C/C++?如何通过使用 C/C++ 在 Linux 中提供进程 ID 来获取父进程 ID?
【发布时间】:2013-07-25 19:11:35
【问题描述】:

我知道可以从/proc/PID/status 获取 ppid 信息,但我只是想将其用作备用计划。

那么,是否可以通过提供 PID 号来获取 PPID? getppid(2) 只能获取 当前 进程的 PPID,所以在这种情况下没有帮助。

有什么想法吗?谢谢。

【问题讨论】:

  • 访问库失败,你总是可以读取/proc/$pid/status,其中$pid当然是你感兴趣的PID。查找前缀为“PPid:”的行跨度>

标签: c++ c linux pid


【解决方案1】:

我认为您可以使用 get_proc_stats 函数来执行此操作。但是你需要安装 libproc-dev 库。

这是一个例子:

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

【讨论】:

    猜你喜欢
    • 2017-05-23
    • 2012-07-27
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 2013-02-28
    相关资源
    最近更新 更多