-1

I am new in LKD and I was reading book by Robert Love. I stuck in understand one concept as follow.

Similarly, it is possible to iterate over a process’s children with

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

Also I would be great if somebody explain the list_entry arguments work?

I am having difficult in understanding above code snippet specially list_for_each works.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
user2958183
  • 41
  • 2
  • 6
  • Have you tried [looking it up](http://lxr.linux.no/linux+v3.12.1/include/linux/list.h#L375)? – Fred Foo Nov 23 '13 at 17:01
  • @larsmans yes and also I tried this http://stackoverflow.com/questions/5550404/list-entry-in-linux but still I didn't get it . – user2958183 Nov 23 '13 at 17:09

1 Answers1

1

list_for_each is a macro defined as

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

Since macros in C are expanded by textual substitution, the piece of code you cite becomes

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

This code walks a circular linked list starting at the node (&current->children)->next, until it comes back to (&current->children)->next.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836