以编程方式获取另一个进程的父进程pid?

发布于 2024-08-07 01:19:04 字数 140 浏览 4 评论 0原文

我尝试了 google,但发现 getppid() 可以获取当前进程的父 pid。

我需要像 getppid(some_other_pid) 这样的东西,有这样的东西吗?基本上获取某个进程的 pid 并返回父进程的 pid。

I tried google, but found getppid() which gets the parent pid of the current process.

I need something like getppid(some_other_pid), is there such a thing? Basically takes the pid of some process and returns the parent process' pid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

不即不离 2024-08-14 01:19:04

我认为最简单的事情是打开“/proc”并解析内容。

您会发现 ppid 作为 /proc/pid/stat 的第四个参数

在 C 中,libproc 有一个用于解析该文件的 get_proc_stats 函数:请参阅 给定一个子 PID,如何获取父 PID 作为示例。

I think the simplest thing would be to open "/proc" and parse the contents.

You'll find the ppid as the 4th parameter of /proc/pid/stat

In C, libproc has a get_proc_stats function for parsing that file: see Given a child PID how can you get the parent PID for an example.

囚你心 2024-08-14 01:19:04

或者在 unix shell 中,您可以尝试 ps -p; -o ppid=

or from a unix shell you can try ps -p <child_pid> -o ppid=

冰雪之触 2024-08-14 01:19:04

我迟到了 7 年,但对于任何可能偶然发现这个问题的人来说,这里有一个 OS X 上的替代解决方案。这里发布的其他答案是正确的,sysctl() 可以完成这项工作,但你还可以使用proc_pidinfo来获取有关进程的很多有用信息。

#include <libproc.h>

int getppid(const pid_t pid)
{
    proc_bsdinfo info;
    proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &info, sizeof(info));
    return info.pbi_ppid;
}

显然,需要额外的错误检查。

I am 7 years late to the party but for anyone who may stumble upon this question, here's an alternative solution on OS X. Other answers posted here are correct and sysctl() will do the job, but you can also use proc_pidinfo to obtain a lot of useful information about a process.

#include <libproc.h>

int getppid(const pid_t pid)
{
    proc_bsdinfo info;
    proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &info, sizeof(info));
    return info.pbi_ppid;
}

Obviously, additional error checking is required.

稚然 2024-08-14 01:19:04

您可以查看 sysctl() 系统调用和 this关联。

You can have a look at sysctl() system call and this link.

伏妖词 2024-08-14 01:19:04

从 proc 条目获取它的另一种方法:

cat /proc/<pid>/status | grep PPid:

one more way to get it from proc entry:

cat /proc/<pid>/status | grep PPid:
暮年 2024-08-14 01:19:04

我们也可以使用 pstree 命令。

pstree -p -s <pid of the process>

pstree -s 给出所有祖先的树。添加 -p 也会给你 pid。

示例:假设有一个 pid=6206 的进程。使用pstree命令

pstree -p -s 6206

您将获得进程树。

systemd(1)───lightdm(1066)───lightdm(1191)───upstart(1360)───gnome-terminal-(5222)───bash(5229)───cpu-print(6206)

这里父PID是5229

We can use pstree command also.

pstree -p -s <pid of the process>

pstree -s gives tree of all the ancestors. Adding -p will give you the pid as well.

Example :Assume there is a process with pid=6206. Using the pstree command

pstree -p -s 6206

You will get the process tree.

systemd(1)───lightdm(1066)───lightdm(1191)───upstart(1360)───gnome-terminal-(5222)───bash(5229)───cpu-print(6206)

Here the parent PID is 5229

莫多说 2024-08-14 01:19:04

仅使用标准库用纯 C 语言制作此内容的简单方法:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXBUF      (BUFSIZ * 2)

int pgetppid(int pid) {
    int ppid;
    char buf[MAXBUF];
    char procname[32];  // Holds /proc/4294967296/status\0
    FILE *fp;

    snprintf(procname, sizeof(procname), "/proc/%u/status", pid);
    fp = fopen(procname, "r");
    if (fp != NULL) {
        size_t ret = fread(buf, sizeof(char), MAXBUF-1, fp);
        if (!ret) {
            return 0;
        } else {
            buf[ret++] = '\0';  // Terminate it.
        }
    }
    fclose(fp);
    char *ppid_loc = strstr(buf, "\nPPid:");
    if (ppid_loc) {
        ppid = sscanf(ppid_loc, "\nPPid:%d", &ppid);
        if (!ppid || ppid == EOF) {
            return 0;
        }
        return ppid;
    } else {
        return 0;
    }

}

int main () {
    int ppid, pid = 373;  // my current cron pid
    ppid = pgetppid(pid);
    printf("PPid = %d\n", ppid);
}

An easy way to craft this in pure C with only standard libraries:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXBUF      (BUFSIZ * 2)

int pgetppid(int pid) {
    int ppid;
    char buf[MAXBUF];
    char procname[32];  // Holds /proc/4294967296/status\0
    FILE *fp;

    snprintf(procname, sizeof(procname), "/proc/%u/status", pid);
    fp = fopen(procname, "r");
    if (fp != NULL) {
        size_t ret = fread(buf, sizeof(char), MAXBUF-1, fp);
        if (!ret) {
            return 0;
        } else {
            buf[ret++] = '\0';  // Terminate it.
        }
    }
    fclose(fp);
    char *ppid_loc = strstr(buf, "\nPPid:");
    if (ppid_loc) {
        ppid = sscanf(ppid_loc, "\nPPid:%d", &ppid);
        if (!ppid || ppid == EOF) {
            return 0;
        }
        return ppid;
    } else {
        return 0;
    }

}

int main () {
    int ppid, pid = 373;  // my current cron pid
    ppid = pgetppid(pid);
    printf("PPid = %d\n", ppid);
}
年华零落成诗 2024-08-14 01:19:04

就我而言,终端中运行的进程的父 PID:

ps | grep -E -m 1 -o "[0-9]{4,}"

In my case, the parent PID of the process running in terminal:

ps | grep -E -m 1 -o "[0-9]{4,}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文