关于过程:需要澄清Robert Love在Linux内核中给出的内容

Need clarification on the content given in the Linux Kernel by Robert Love

我是LKD的新手,我正在读Robert Love的书。我坚持理解以下一个概念。

类似地,可以使用

1
2
3
4
5
6
struct task_struct *task;
struct list_head *list;
list_for_each(list, &current->children) {
    task = list_entry(list, struct task_struct, sibling);
    /* task now points to one of current’s children */
}

如果有人解释list_entry参数是否有效,我也很棒?

我很难理解上面的代码片段,特别是list_for_each起作用。


list_for_each是定义为

的宏

1
2
#define list_for_each(pos, head) \\
        for (pos = (head)->next; pos != (head); pos = pos->next)

由于C语言中的宏是通过文本替换扩展的,因此您引用的代码变成了

1
2
3
4
for (list = (&current->children)->next; list != (&current->children); list = list->next) {
    task = list_entry(list, struct task_struct, sibling);
    /* task now points to one of current’s children */
}

此代码从节点(¤t->children)->next开始遍历循环链表,直到返回到(¤t->children)->next