How can I get the process descriptor from a PID in

2019-08-23 11:24发布

问题:

I am trying to figure out how to get the process descriptor from a PID.

From http://www.linuxforums.org/forum/kernel/153873-getting-task_struct-process-using-its-pid.html, for Linux kernel 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;
}

The link seems to say that pidhash[pid_hashfn(pid)] is a pointer to a task_struct object whose PID is value pid.

But that doesn't seem to be true from the book Understanding The Linux Kernel, which talks about Linux kernel 2.6.11. I am not sure if the relevant code is the same in 2.6.11 and in 2.4. From the book, I learned that pidhash[pid_hashfn(pid)] has type hlist_head, which is a pointer to an hlist_node object. The hlist_node object is pids[0].pid_chain of a task_struct object. Then how can I obtain the task_struct object from pidhash[pid_hashfn(pid)]?

Note that

  • I am asking this just for the purposes of reading Understanding the Linus Kernel (Linux kernel 2.6.11), so I am not asking about the recent Linux kernel versions, although it doesn't hurt that you can also mention how it is done in the recent Linux kernel versions.

  • I guess the difficulty I have here is related to my previous question Whose address shall the node of a linked list store: other node, or data structure having a node as a field?

Thanks.

回答1:

In kernel 2.6.11 task_struct contains array pids[PIDTYPE_MAX] so that given task placed in several hash-tables simultaniously.

pidhash contains pointers to PIDTYPE_MAX hash-tables. pidhash[i] is a pointer to beginning of i-th hash-table. Thus, pidhash[type][pid_hashfn(nr)] is a pointer to linked list.

It is better to find struct pid * to entry in task's [pids[type]] element with given pid type type and pid nr using kernel function find_pid(type, nr).

Then you can convert (non-NULL) pointer to struct pid into pointer to struct task_struct using container-of-based macro pid_task.