Kernel: how to iterate the children of the current

2020-02-07 02:45发布

问题:

In Linux Kernel Development, 3rd ed, this code was given for traversing the children of the current process.

list_for_each(list, &current->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?

回答1:

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.



回答2:

sibling is a field in the task_struct.



回答3:

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.