2013-11-23 25 views
-1

我是LKD的新手,我正在閱讀羅伯特·洛夫的書。我堅持瞭解如下一個概念。需要澄清在羅伯特·愛的Linux內核中給出的內容

同樣,也可以遍歷進程的孩子

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的作品。

+0

您是否試過[查看](http://lxr.linux.no/linux+v3.12.1/include/linux/list.h#L375)? –

+0

@larsmans是的,我也試過這個http://stackoverflow.com/questions/5550404/list-entry-in-linux,但我仍然沒有得到它。 – user2958183

回答

1

list_for_each是宏defined as

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

由於C宏由文本替換膨脹時,片的代碼你引用變得

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 */ 
} 

此代碼行走循環鏈表的始節點(&current->children)->next,直到它回到(&current->children)->next