question about include/linux/list.h

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,
  This is the first time I am trying to understand the
Linux kernel. To do so, I have started to read the
following book by Robert Love

Linux Kernel Development, Second Edition. Since the
book refers to 2.6.10, I am browsing this version.

On Page 30 of the book, it says,
"Similarly, it is possible to iterate over a
processe'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 */
}"

>From what I understand, list_for_each above will
iterate through one of the current processes child (if
there was one), then that childs child(if there is
one) and so on. Am I right in concluding this? This
means only the first iteration will find the current
processes child.

Also with respect to the list_entry macro, since list
points to the children member of the child task and
list_entry uses macro container_of from
linux/kernel.h, task cannot point to the task_struct
of the child because  list_entry subtracts the offset
of sibling member from the address of the children
member. This will subtract more than is necessary. So,
the first argument of list_entry should point to a
list head member with the same member name within the
struct. Otherwise it will not return the address of
the containing struct. Am I right in concluding this?

>From what I understand browsing through the kernel
source, the only way to iterate through a processes
children is by asking a child to iterate through its
siblings. Am I right?

So the code snippet would have to be,

struct task_struct *task;
struct list_head *list;

if(current != (task =
list_entry((current->children).next, struct
task_struct, children))) 
{
   /* task one of current processes child. Find the
other children */
   list_for_each(list, &task->sibling)
   {
      task = list_entry(list, struct task_struct,
sibling);
      /* For each iteration of the loop, task contains
other children of the current process */
   }
}
else
{
   /* there are no children. task points to the
current  processes task_struct */
}

I would appreciate it if some body could clarify this.
Am I missing something here?

Thanks
Vish

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux