title: Print the Process Tree of Kernel
body: <p>I am newbie to C and Kernel modules. I am trying to print all the kernel processes and their children printed. However when I insert my module terminal it gives only part of processes. My algorithm is first getting to the root of process tree, then getting children recursively step by step.</p>
  
  <p>my code is below:</p>
  
  <pre><code>#include &lt;linux/init.h&gt;
  #include &lt;linux/module.h&gt;
  #include &lt;linux/kernel.h&gt;
  #include &lt;linux/sched.h&gt;
  #include &lt;asm/current.h&gt;
  #include &lt;linux/list.h&gt;
  
  
  void getProcInfo(struct task_struct* task);
  
  static int mod_init(void)
  {
      printk(KERN_ALERT "ProcInfo sucessfully loaded.\n");
      struct task_struct *task=current;
  
  
      while(task-&gt;pid != 1 ){
          task=task-&gt;parent;
      }
      //task = current-&gt;real_parent;
      getProcInfo(task);
      return 0; 
  }
  
  static void mod_exit(void)
  {
          printk(KERN_ALERT "ProcInfo sucessfully unloaded.\n");
  }
  
  void getProcInfo(struct task_struct* task)
  {
      struct list_head *children_tasks;
      children_tasks = &amp;(task-&gt;children);
  
      list_for_each(children_tasks, &amp;(task-&gt;children))
      {
  
          struct task_struct *child_task;
          child_task = list_entry(children_tasks, struct task_struct, sibling);
  
          printk(KERN_INFO "The parent process is \"%s\" (pid %i)\tThe process is \"%s\" (pid %i)\n",
          child_task-&gt;parent-&gt;comm, child_task-&gt;parent-&gt;pid,child_task-&gt;comm, child_task-&gt;pid);
          getProcInfo(child_task);
      }
  }
  
  module_init(mod_init);
  module_exit(mod_exit);
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("Nihad");
  </code></pre>