如何从Linux内核中的PID获取进程描述符?

问题描述 投票:0回答:1

我试图找出如何从PID获取进程描述符。

来自http://www.linuxforums.org/forum/kernel/153873-getting-task_struct-process-using-its-pid.html,适用于Linux内核2.4

static inline struct task_struct *find_task_by_pid(int pid)
{
    struct task_struct *p, **htable = &pidhash[pid_hashfn(pid)];

    for(p = *htable; p && p->pid != pid; p = p->pidhash_next)
        ;

    return p;
}

链接似乎说pidhash[pid_hashfn(pid)]是一个指向task_struct对象的指针,其PID值为pid

但是,从“理解Linux内核”一书中可以看出这一点,该内容涉及Linux内核2.6.11。我不确定2.6.11和2.4中的相关代码是否相同。从这本书中,我了解到pidhash[pid_hashfn(pid)]的类型为hlist_head,它是指向hlist_node对象的指针。 hlist_node对象是pids[0].pid_chain对象的task_struct。那我如何从task_struct获得pidhash[pid_hashfn(pid)]对象?

注意

谢谢。

linux process linux-kernel pid
1个回答
1
投票

在内核2.6.11中,task_struct包含数组pids[PIDTYPE_MAX],以便将给定的任务同时放在几个哈希表中。

pidhash包含指向PIDTYPE_MAX哈希表的指针。 pidhash[i]是指向第i个哈希表开头的指针。因此,pidhash[type][pid_hashfn(nr)]是指向链表的指针。

最好使用内核函数struct pid *找到pids[type]进入任务的[type]元素,使用给定的pid类型nr和pid find_pid(type, nr)

然后你可以使用基于NULL的宏struct pid将(非struct task_struct)指针转换为container-of指针指向pid_task

© www.soinside.com 2019 - 2024. All rights reserved.