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.