0

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

Thanks.

ReAl
  • 1,231
  • 1
  • 8
  • 19
Tim
  • 1
  • 141
  • 372
  • 590

1 Answers1

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.

ReAl
  • 1,231
  • 1
  • 8
  • 19