In Linux Kernel Development, 3rd ed, this code was given for traversing the children of the current process.
list_for_each(list, ¤t->children) {
task = list_entry(list, struct task_struct, sibling);
/* task now points to one of current’s children */
}
The "sibling" in this idiom looks out of place. What is its purpose?
sibling
is the name of the list_head
structure in struct task_struct
that corresponds to the parent's children list.
That is, in this loop list
always points to a sibling
member of a struct task_struct
, or the children
member of the parent.
sibling
is a field in the task_struct
.
Here is how I understood this:
The lists always point to sibling
member,
1) When iterating through children
list, we traverse sibling
members of current task's children
2) Iterating sibling
list will make traversing sibling
members of parent's children (or current task's siblings)
Using sibling
member to add to parent's list ensures that 2 lists - parent's children
and current task's sibling
list - are updated with same member.