title: How can I get the process descriptor from a PID in Linux kernel?
body: <p>I am trying to figure out how to get the process descriptor from a PID.</p>
  
  <p>From  <a href="http://www.linuxforums.org/forum/kernel/153873-getting-task_struct-process-using-its-pid.html" rel="nofollow noreferrer">http://www.linuxforums.org/forum/kernel/153873-getting-task_struct-process-using-its-pid.html</a>,  for Linux kernel 2.4</p>
  
  <pre class="lang-c prettyprint-override"><code>static inline struct task_struct *find_task_by_pid(int pid)
  {
      struct task_struct *p, **htable = &amp;pidhash[pid_hashfn(pid)];
  
      for(p = *htable; p &amp;&amp; p-&gt;pid != pid; p = p-&gt;pidhash_next)
          ;
  
      return p;
  }
  </code></pre>
  
  <p>The link seems to say that <code>pidhash[pid_hashfn(pid)]</code> is a pointer to a <code>task_struct</code> object whose PID is value <code>pid</code>.</p>
  
  <p>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 <code>pidhash[pid_hashfn(pid)]</code> has type <code>hlist_head</code>, which is a pointer to an <code>hlist_node</code> object. The <code>hlist_node</code> object is <code>pids[0].pid_chain</code> of a <code>task_struct</code> object. Then  how can I obtain the <code>task_struct</code> object from <code>pidhash[pid_hashfn(pid)]</code>?</p>
  
  <p>Note that </p>
  
  <ul>
  <li><p>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.</p></li>
  <li><p>I guess the difficulty I have here is related to my previous question <a href="https://stackoverflow.com/questions/52359598/whose-address-shall-the-node-of-a-linked-list-store-other-node-or-data-structu">Whose address shall the node of a linked list store: other node, or data structure having a node as a field?</a></p></li>
  </ul>
  
  <p>Thanks.</p>