0
list_for_each(list, &current->children) { 
      task = list_entry(list, struct task_struct, sibling); 
      /* task now points to one of current’s children */
}

According to this answer: list_entry in Linux, the list_entry() macro will cast to specified struct based on the offset of the supplied pointer (in this case, list) in the specified data structure (in this case, task_struct). However, in the above code, I think list is the pointer to the next task_struct, starts from the beginning. How come the code cast list back to task_struct from the offset sibling, which is not the start of the task_struct? Or, list points directly to the sibling member of its next task_struct?

Community
  • 1
  • 1
Amumu
  • 17,924
  • 31
  • 84
  • 131
  • Maybe this thread can help. http://stackoverflow.com/questions/8207160/kernel-how-to-iterate-the-children-of-the-current-process – Jinghao Shi Mar 28 '12 at 06:09
  • So, what does `sibling` actually point to? To the start of `task_struct`, or to the `sibling`? Since `children` is also a `list_head` which points to the next/prev `list_head`, I guess `&current->children` will point to the `sibling` (which is also a `list_head`) of the first child, and the `sibling` of the first child will point to the next `sibling`. Thus, we have to cast it back to the start of `task_struct` by `list_entry()`. Is this correct? – Amumu Mar 28 '12 at 06:15

0 Answers0